Skip to content

Commit 0cb000a

Browse files
authoredMar 24, 2023
Merge pull request xyflow#2955 from wbkd/feat/minimap-inverse-pan-and-zoom-step
Feat/minimap inverse pan and zoom step
2 parents 7e4d5af + c1448c2 commit 0cb000a

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed
 

‎.changeset/rich-papayas-hope.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@reactflow/minimap': minor
3+
---
4+
5+
add inversePan and zoomStep props

‎examples/vite-app/src/examples/InteractiveMinimap/index.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MouseEvent, useCallback } from 'react';
1+
import { MouseEvent, useCallback, useState } from 'react';
22
import ReactFlow, {
33
MiniMap,
44
Background,
@@ -87,6 +87,7 @@ const defaultEdgeOptions = { zIndex: 0 };
8787

8888
const BasicFlow = () => {
8989
const instance = useReactFlow();
90+
const [inverse, setInverse] = useState(false);
9091

9192
const updatePos = () => {
9293
instance.setNodes((nodes) =>
@@ -103,6 +104,7 @@ const BasicFlow = () => {
103104

104105
const logToObject = () => console.log(instance.toObject());
105106
const resetTransform = () => instance.setViewport({ x: 0, y: 0, zoom: 1 });
107+
const toggleInverse = () => setInverse(!inverse);
106108

107109
const toggleClassnames = () => {
108110
instance.setNodes((nodes) =>
@@ -137,7 +139,8 @@ const BasicFlow = () => {
137139
fitView
138140
>
139141
<Background variant={BackgroundVariant.Dots} />
140-
<MiniMap onClick={onMiniMapClick} onNodeClick={onMiniMapNodeClick} pannable zoomable />
142+
<MiniMap onClick={onMiniMapClick} onNodeClick={onMiniMapNodeClick} pannable zoomable
143+
inversePan={inverse}/>
141144
<Controls />
142145

143146
<div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}>
@@ -150,7 +153,12 @@ const BasicFlow = () => {
150153
<button onClick={toggleClassnames} style={{ marginRight: 5 }}>
151154
toggle classnames
152155
</button>
153-
<button onClick={logToObject}>toObject</button>
156+
<button onClick={logToObject} style={{ marginRight: 5 }}>
157+
toObject
158+
</button>
159+
<button onClick={toggleInverse} style={{ marginRight: 5 }}>
160+
{inverse ? 'un-inverse pan' : 'inverse pan'}
161+
</button>
154162
</div>
155163
</ReactFlow>
156164
);

‎packages/minimap/src/MiniMap.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ function MiniMap({
6767
pannable = false,
6868
zoomable = false,
6969
ariaLabel = 'React Flow mini map',
70+
inversePan = false,
71+
zoomStep = 10
7072
}: MiniMapProps) {
7173
const store = useStoreApi();
7274
const svg = useRef<SVGSVGElement>(null);
@@ -106,7 +108,7 @@ function MiniMap({
106108
const pinchDelta =
107109
-event.sourceEvent.deltaY *
108110
(event.sourceEvent.deltaMode === 1 ? 0.05 : event.sourceEvent.deltaMode ? 1 : 0.002) *
109-
10;
111+
zoomStep;
110112
const zoom = transform[2] * Math.pow(2, pinchDelta);
111113

112114
d3Zoom.scaleTo(d3Selection, zoom);
@@ -120,9 +122,10 @@ function MiniMap({
120122
}
121123

122124
// @TODO: how to calculate the correct next position? Math.max(1, transform[2]) is a workaround.
125+
const moveScale = viewScaleRef.current * Math.max(1, transform[2]) * (inversePan ? -1 : 1);
123126
const position = {
124-
x: transform[0] - event.sourceEvent.movementX * viewScaleRef.current * Math.max(1, transform[2]),
125-
y: transform[1] - event.sourceEvent.movementY * viewScaleRef.current * Math.max(1, transform[2]),
127+
x: transform[0] - event.sourceEvent.movementX * moveScale,
128+
y: transform[1] - event.sourceEvent.movementY * moveScale,
126129
};
127130
const extent: CoordinateExtent = [
128131
[0, 0],
@@ -147,7 +150,7 @@ function MiniMap({
147150
selection.on('zoom', null);
148151
};
149152
}
150-
}, [pannable, zoomable]);
153+
}, [pannable, zoomable, inversePan, zoomStep]);
151154

152155
const onSvgClick = onClick
153156
? (event: MouseEvent) => {

‎packages/minimap/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export type MiniMapProps<NodeData = any> = Omit<HTMLAttributes<SVGSVGElement>, '
2020
pannable?: boolean;
2121
zoomable?: boolean;
2222
ariaLabel?: string | null;
23+
inversePan?: boolean;
24+
zoomStep?: number;
2325
};
2426

2527
export interface MiniMapNodeProps {

0 commit comments

Comments
 (0)
Please sign in to comment.