Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not break if mouse moves to x=0 when using rangeslider #6780

Merged
merged 3 commits into from Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/6780_fix.md
@@ -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
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