-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#10070 - Fix: Scroll bars for legend widgets won't move coherently wi…
- Loading branch information
Showing
2 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
import React from 'react'; | ||
import isNil from 'lodash/isNil'; | ||
|
||
/** | ||
* Handler to deal with legend mouse wheel sensitivity issue | ||
* with chart widget has multiple legend items | ||
* NOTE: This is a workaround for issue in plotly.js https://github.com/plotly/plotly.js/issues/6737 | ||
* until fix on plotly is implemented | ||
*/ | ||
const withLegendScrollHandler = (WrappedComponent) => { | ||
return (props) => { | ||
/** | ||
* Event listener captures mouse wheel event of legend element | ||
* and modifies the deltaY such that the sensitivity is exhibited in a controlled manner | ||
*/ | ||
const legendMouseWheelEventHandler = (graphDiv) => { | ||
const legend = graphDiv.querySelector('.infolayer .legend'); | ||
if (legend) { | ||
const legendBg = legend.querySelector('.bg'); | ||
|
||
// add listener in capture mode | ||
legend.addEventListener('wheel', function(event) { | ||
if (!event.isTrusted) return; | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
const maxScrollDistance = legend.getBBox()?.height; | ||
const scrollHeight = legendBg?.getBBox()?.height; | ||
|
||
let deltaY = 0.30; // fallback fixed value | ||
if (!isNil(scrollHeight) && !isNil(maxScrollDistance)) { | ||
deltaY = (scrollHeight / maxScrollDistance) * 100; | ||
} | ||
if (Number(event.deltaY) < 0) { | ||
deltaY *= -1; // negate on upward scroll | ||
} | ||
const newEvent = new WheelEvent('wheel', { | ||
clientX: event.clientX, | ||
clientY: event.clientY, | ||
deltaY, | ||
view: window, | ||
bubbles: true, | ||
cancelable: true | ||
}); | ||
event.target.dispatchEvent(newEvent); | ||
}, true); | ||
} | ||
}; | ||
return ( | ||
<WrappedComponent | ||
{...props} | ||
onInitialized={(figure, graphDiv) => { | ||
legendMouseWheelEventHandler(graphDiv); | ||
props.onInitialized(figure, graphDiv); | ||
}} | ||
/>); | ||
}; | ||
}; | ||
|
||
export default withLegendScrollHandler; |