Skip to content

Commit

Permalink
Add wheel scroll zoom with drag pan
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsteff committed Jul 3, 2024
1 parent da53b40 commit e24c56c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,28 @@ const InternalCanvas: FC<CanvasProps & { ref?: Ref<CanvasRef> }> = forwardRef(({
}
}, [createDragNodeChildren, dragNodeData, layout?.children]);

useEffect(() => {
const zoomOnWheelScroll = (e: WheelEvent) => {
e.preventDefault();
if (e.deltaY > 0) {
zoomOut();
} else {
zoomIn();
}
};

// only zoom with the scroll wheel when the pan type is drag
if (containerRef.current && panType === 'drag') {
containerRef.current.addEventListener('wheel', zoomOnWheelScroll, { passive: false });
}

return () => {
if (containerRef.current) {
containerRef.current.removeEventListener('wheel', zoomOnWheelScroll);
}
};
}, [panType, containerRef, zoomIn, zoomOut]);

return (
<div
style={{ height, width }}
Expand Down

0 comments on commit e24c56c

Please sign in to comment.