Skip to content

Commit b7186f3

Browse files
committed
fix(react): minimap still works after reactflow remounts #5971
1 parent d8edc77 commit b7186f3

3 files changed

Lines changed: 59 additions & 7 deletions

File tree

‎examples/react/src/App/routes.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import DetachedHandle from '../examples/DetachedHandle';
6363
import ZIndexMode from '../examples/ZIndexMode';
6464
import Middlewares from '../examples/Middlewares';
6565
import NodeSelectionBug from '../examples/NodeSelectionBug';
66+
import Remount from '../examples/Remount';
6667

6768
export interface IRoute {
6869
name: string;
@@ -291,6 +292,11 @@ const routes: IRoute[] = [
291292
path: 'provider',
292293
component: Provider,
293294
},
295+
{
296+
name: 'Remount',
297+
path: 'remount',
298+
component: Remount,
299+
},
294300
{
295301
name: 'Save/Restore',
296302
path: 'save-restore',
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useState } from 'react';
2+
import { ReactFlow, ReactFlowProvider, MiniMap, Background, Controls } from '@xyflow/react';
3+
import '@xyflow/react/dist/style.css';
4+
5+
const defaultNodes = [
6+
{ id: '1', position: { x: 0, y: 0 }, data: { label: 'Root' } },
7+
{ id: '2', position: { x: 0, y: 120 }, data: { label: 'Child A' } },
8+
{ id: '3', position: { x: 180, y: 120 }, data: { label: 'Child B' } },
9+
{ id: '4', position: { x: 360, y: 120 }, data: { label: 'Child C' } },
10+
];
11+
12+
const defaultEdges = [
13+
{ id: 'e1-2', source: '1', target: '2' },
14+
{ id: 'e1-3', source: '1', target: '3' },
15+
{ id: 'e1-4', source: '1', target: '4' },
16+
];
17+
18+
function Flow({ remountKey }: { remountKey: number }) {
19+
return (
20+
<ReactFlow key={remountKey} defaultNodes={defaultNodes} defaultEdges={defaultEdges} fitView>
21+
<Background />
22+
<Controls />
23+
<MiniMap pannable zoomable />
24+
</ReactFlow>
25+
);
26+
}
27+
28+
export default function App() {
29+
const [remountKey, setRemountKey] = useState(0);
30+
return (
31+
<div style={{ display: 'flex', flexDirection: 'column', height: '100vh' }}>
32+
<button onClick={() => setRemountKey((k) => k + 1)} style={{ padding: 8 }}>
33+
Remount ReactFlow
34+
</button>
35+
<div style={{ flex: 1 }}>
36+
<ReactFlowProvider>
37+
<Flow remountKey={remountKey} />
38+
</ReactFlowProvider>
39+
</div>
40+
</div>
41+
);
42+
}

‎packages/react/src/additional-components/MiniMap/MiniMap.tsx‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function MiniMapComponent<NodeType extends Node = Node>({
8585
}: MiniMapProps<NodeType>) {
8686
const store = useStoreApi<NodeType>();
8787
const svg = useRef<SVGSVGElement>(null);
88-
const { boundingRect, viewBB, rfId, panZoom, translateExtent, flowWidth, flowHeight, ariaLabelConfig } = useStore(
88+
const { boundingRect, panZoom, viewBB, rfId, translateExtent, flowWidth, flowHeight, ariaLabelConfig } = useStore(
8989
selector,
9090
areEqual
9191
);
@@ -108,10 +108,11 @@ function MiniMapComponent<NodeType extends Node = Node>({
108108
viewScaleRef.current = viewScale;
109109

110110
useEffect(() => {
111-
if (svg.current && panZoom) {
111+
const currentPanZoom = store.getState().panZoom;
112+
if (svg.current && currentPanZoom) {
112113
minimapInstance.current = XYMinimap({
113114
domNode: svg.current,
114-
panZoom,
115+
panZoom: currentPanZoom,
115116
getTransform: () => store.getState().transform,
116117
getViewScale: () => viewScaleRef.current,
117118
});
@@ -141,10 +142,13 @@ function MiniMapComponent<NodeType extends Node = Node>({
141142
}
142143
: undefined;
143144

144-
const nodeClickHandler = useCallback((event: MouseEvent, nodeId: string) => {
145-
const node: NodeType = store.getState().nodeLookup.get(nodeId)!.internals.userNode;
146-
onNodeClick?.(event, node);
147-
}, [onNodeClick]);
145+
const nodeClickHandler = useCallback(
146+
(event: MouseEvent, nodeId: string) => {
147+
const node: NodeType = store.getState().nodeLookup.get(nodeId)!.internals.userNode;
148+
onNodeClick?.(event, node);
149+
},
150+
[onNodeClick]
151+
);
148152

149153
const onSvgNodeClick = onNodeClick ? nodeClickHandler : undefined;
150154

0 commit comments

Comments
 (0)