-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[charts] Add pressAndDrag
pan gesture
#19779
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9ce1512
gesture
JCQuintas dd24715
usage
JCQuintas aa3c819
docs
JCQuintas 3b782cc
fixes
JCQuintas 0f80642
fix tests
JCQuintas 42bb10b
init gesture
JCQuintas 537e5dd
remove
JCQuintas 64d3c43
add integration test
JCQuintas 276604b
Merge commit 'af1fc437dad6f81c930cb77d36d74ea8a59da0cd' into press-an…
JCQuintas 98e8ab4
scripts
JCQuintas 539be2a
fix typo
JCQuintas c271a08
fix android issues
JCQuintas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
121 changes: 121 additions & 0 deletions
121
...s/x-charts-pro/src/internals/plugins/useChartProZoom/gestureHooks/usePanOnPressAndDrag.ts
This file contains hidden or 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,121 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { | ||
ChartPlugin, | ||
useSelector, | ||
selectorChartDrawingArea, | ||
ZoomData, | ||
selectorChartZoomOptionsLookup, | ||
} from '@mui/x-charts/internals'; | ||
import { rafThrottle } from '@mui/x-internals/rafThrottle'; | ||
import { PressAndDragEvent } from '@mui/x-internal-gestures/core'; | ||
import { UseChartProZoomSignature } from '../useChartProZoom.types'; | ||
import { translateZoom } from './useZoom.utils'; | ||
import { selectorPanInteractionConfig } from '../ZoomInteractionConfig.selectors'; | ||
|
||
export const usePanOnPressAndDrag = ( | ||
{ | ||
store, | ||
instance, | ||
svgRef, | ||
}: Pick<Parameters<ChartPlugin<UseChartProZoomSignature>>[0], 'store' | 'instance' | 'svgRef'>, | ||
setZoomDataCallback: React.Dispatch<ZoomData[] | ((prev: ZoomData[]) => ZoomData[])>, | ||
) => { | ||
const drawingArea = useSelector(store, selectorChartDrawingArea); | ||
const optionsLookup = useSelector(store, selectorChartZoomOptionsLookup); | ||
const startRef = React.useRef<readonly ZoomData[]>(null); | ||
const config = useSelector(store, selectorPanInteractionConfig, ['pressAndDrag' as const]); | ||
|
||
const isPanOnPressAndDragEnabled = React.useMemo( | ||
() => (Object.values(optionsLookup).some((v) => v.panning) && config) || false, | ||
[optionsLookup, config], | ||
); | ||
|
||
React.useEffect(() => { | ||
if (!isPanOnPressAndDragEnabled) { | ||
return; | ||
} | ||
|
||
instance.updateZoomInteractionListeners('zoomPressAndDrag', { | ||
requiredKeys: config!.requiredKeys, | ||
pointerMode: config!.pointerMode, | ||
pointerOptions: { | ||
mouse: config!.mouse, | ||
touch: config!.touch, | ||
}, | ||
}); | ||
}, [isPanOnPressAndDragEnabled, config, instance]); | ||
|
||
// Add event for chart panning with press and drag | ||
React.useEffect(() => { | ||
const element = svgRef.current; | ||
|
||
if (element === null || !isPanOnPressAndDragEnabled) { | ||
return () => {}; | ||
} | ||
|
||
const handlePressAndDragStart = (event: PressAndDragEvent) => { | ||
if (!(event.detail.target as SVGElement)?.closest('[data-charts-zoom-slider]')) { | ||
startRef.current = store.value.zoom.zoomData; | ||
} | ||
}; | ||
|
||
const handlePressAndDragEnd = () => { | ||
startRef.current = null; | ||
}; | ||
|
||
const throttledCallback = rafThrottle( | ||
(event: PressAndDragEvent, zoomData: readonly ZoomData[]) => { | ||
const newZoomData = translateZoom( | ||
zoomData, | ||
{ x: event.detail.activeDeltaX, y: -event.detail.activeDeltaY }, | ||
{ | ||
width: drawingArea.width, | ||
height: drawingArea.height, | ||
}, | ||
optionsLookup, | ||
); | ||
|
||
setZoomDataCallback(newZoomData); | ||
}, | ||
); | ||
|
||
const handlePressAndDrag = (event: PressAndDragEvent) => { | ||
const zoomData = startRef.current; | ||
if (!zoomData) { | ||
return; | ||
} | ||
throttledCallback(event, zoomData); | ||
}; | ||
|
||
const pressAndDragHandler = instance.addInteractionListener( | ||
'zoomPressAndDrag', | ||
handlePressAndDrag, | ||
); | ||
const pressAndDragStartHandler = instance.addInteractionListener( | ||
'zoomPressAndDragStart', | ||
handlePressAndDragStart, | ||
); | ||
const pressAndDragEndHandler = instance.addInteractionListener( | ||
'zoomPressAndDragEnd', | ||
handlePressAndDragEnd, | ||
); | ||
|
||
return () => { | ||
pressAndDragStartHandler.cleanup(); | ||
pressAndDragHandler.cleanup(); | ||
pressAndDragEndHandler.cleanup(); | ||
throttledCallback.clear(); | ||
}; | ||
}, [ | ||
instance, | ||
svgRef, | ||
isPanOnPressAndDragEnabled, | ||
optionsLookup, | ||
drawingArea.width, | ||
drawingArea.height, | ||
setZoomDataCallback, | ||
store, | ||
startRef, | ||
]); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of topic for this PR but it' would be nice to have a
knob: "title"
to do the following. Otherwise it's a bit herder to know if the event impacts panning or zooming