Skip to content

Commit

Permalink
Merge pull request #6780 from david-bezero/range-0
Browse files Browse the repository at this point in the history
Do not break if mouse moves to x=0 when using rangeslider
  • Loading branch information
archmoj authored Nov 14, 2023
2 parents b735a81 + 769158d commit 5231272
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions draftlogs/6780_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix `Cannot read properties of undefined (reading '0')` when the mouse moves to x=0 while dragging a rangeslider [[#6780](https://github.com/plotly/plotly.js/pull/6780)], with thanks to @david-bezero for the contribution!
14 changes: 12 additions & 2 deletions src/components/rangeslider/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ module.exports = function(gd) {
});
};

function eventX(event) {
if(typeof event.clientX === 'number') {
return event.clientX;
}
if(event.touches && event.touches.length > 0) {
return event.touches[0].clientX;
}
return 0;
}

function setupDragElement(rangeSlider, gd, axisOpts, opts) {
if(gd._context.staticPlot) return;

Expand All @@ -234,7 +244,7 @@ function setupDragElement(rangeSlider, gd, axisOpts, opts) {
function mouseDownHandler() {
var event = d3.event;
var target = event.target;
var startX = event.clientX || event.touches[0].clientX;
var startX = eventX(event);
var offsetX = startX - rangeSlider.node().getBoundingClientRect().left;
var minVal = opts.d2p(axisOpts._rl[0]);
var maxVal = opts.d2p(axisOpts._rl[1]);
Expand All @@ -247,7 +257,7 @@ function setupDragElement(rangeSlider, gd, axisOpts, opts) {
dragCover.addEventListener('mouseup', mouseUp);

function mouseMove(e) {
var clientX = e.clientX || e.touches[0].clientX;
var clientX = eventX(e);
var delta = +clientX - startX;
var pixelMin, pixelMax, cursor;

Expand Down

0 comments on commit 5231272

Please sign in to comment.