Skip to content

Commit 3319ef4

Browse files
committed
fix(react): keep minimap bounds at the viewport when all nodes are hidden
The minimap selector only fell back to the viewport rect when nodeLookup was empty, but the bounds it merges come from a traversal filtered to the visible nodes. getInternalNodesBounds returns a rect at the origin when nothing passes that filter, so a flow whose nodes are all hidden merged a phantom rect at (0, 0) into the viewport rect. With the viewport panned away from the origin the minimap then zoomed out to span everything between the origin and the viewport, shrinking the viewport mask to a speck. Check for a visible node instead of any node. The behavior is unchanged as soon as one node is visible, and for an empty flow.
1 parent 31fa38e commit 3319ef4

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@xyflow/react": patch
3+
---
4+
5+
Fix the `MiniMap` zooming out to include the origin when every node is hidden. The bounds are calculated from the visible nodes only, so the minimap now falls back to the viewport rect when no node is visible, like it already does for an empty flow.

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,25 @@ const selector = (s: ReactFlowState) => {
2424
height: s.height / s.transform[2],
2525
};
2626

27+
/*
28+
* the bounds only cover the visible nodes, so we have to check for a visible node
29+
* here as well. getInternalNodesBounds returns a rect at the origin when nothing
30+
* passes the filter, which would stretch the bounds to include (0, 0).
31+
*/
32+
let hasVisibleNode = false;
33+
34+
for (const node of s.nodeLookup.values()) {
35+
if (!node.hidden) {
36+
hasVisibleNode = true;
37+
break;
38+
}
39+
}
40+
2741
return {
2842
viewBB,
29-
boundingRect:
30-
s.nodeLookup.size > 0
31-
? getBoundsOfRects(getInternalNodesBounds(s.nodeLookup, { filter: filterHidden }), viewBB)
32-
: viewBB,
43+
boundingRect: hasVisibleNode
44+
? getBoundsOfRects(getInternalNodesBounds(s.nodeLookup, { filter: filterHidden }), viewBB)
45+
: viewBB,
3346
rfId: s.rfId,
3447
panZoom: s.panZoom,
3548
translateExtent: s.translateExtent,

0 commit comments

Comments
 (0)