Skip to content

Commit ff126a0

Browse files
committed
fix(system): clamp extent:'parent' child to an unmeasured parent with an intrinsic size
calculateNodePosition built the parent extent from parentNode.measured directly, so a child with extent: 'parent' was not clamped when the parent declares an intrinsic size (width/initialWidth) but has not been measured yet - the child could be dragged outside the parent. The reconcile path (clampPositionToParent) already uses getNodeDimensions, so the two paths were inconsistent. Use getNodeDimensions (measured -> width -> initialWidth) for the parent extent as well. Behavior is unchanged for measured parents and for parents with no size at all. Adds a regression test.
1 parent dd308ab commit ff126a0

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@xyflow/system": patch
3+
---
4+
5+
Fix a child node with `extent: 'parent'` not being clamped to its parent when the parent declares an intrinsic size (`width`/`initialWidth`) but hasn't been measured yet. `calculateNodePosition` now uses the same `getNodeDimensions` fallback as `clampPositionToParent`.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { adoptUserNodes, calculateNodePosition, type NodeLookup, type ParentLookup } from '@xyflow/system';
2+
import type { Node } from '@xyflow/react';
3+
4+
describe('calculateNodePosition', () => {
5+
it("clamps a child with extent 'parent' to a parent that has an intrinsic size but is not measured yet", () => {
6+
const nodeLookup: NodeLookup = new Map();
7+
const parentLookup: ParentLookup = new Map();
8+
9+
const nodes: Node[] = [
10+
{
11+
id: 'parent',
12+
data: { label: 'parent' },
13+
position: { x: 0, y: 0 },
14+
// intrinsic size, but no measured dimensions yet (e.g. before measuring / SSR)
15+
initialWidth: 200,
16+
initialHeight: 100,
17+
},
18+
{
19+
id: 'child',
20+
data: { label: 'child' },
21+
position: { x: 0, y: 0 },
22+
measured: { width: 50, height: 50 },
23+
parentId: 'parent',
24+
extent: 'parent',
25+
},
26+
];
27+
28+
adoptUserNodes(nodes, nodeLookup, parentLookup);
29+
30+
const { positionAbsolute } = calculateNodePosition({
31+
nodeId: 'child',
32+
nextPosition: { x: 500, y: 500 },
33+
nodeLookup,
34+
});
35+
36+
// The child must stay inside the parent: max x = 200 - 50, max y = 100 - 50.
37+
// Before the fix the parent's unmeasured size was ignored and the child
38+
// escaped to { x: 500, y: 500 }.
39+
expect(positionAbsolute.x).to.equal(150);
40+
expect(positionAbsolute.y).to.equal(50);
41+
});
42+
});
43+
44+
export {};

‎packages/system/src/utils/graph.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,13 @@ export function calculateNodePosition<NodeType extends NodeBase>({
427427
if (!parentNode) {
428428
onError?.('005', errorMessages['error005']());
429429
} else {
430-
const parentWidth = parentNode.measured.width;
431-
const parentHeight = parentNode.measured.height;
430+
/*
431+
* use the measured -> width -> initialWidth fallback (via getNodeDimensions)
432+
* so a child with extent: 'parent' is still clamped to a parent that
433+
* declares an intrinsic size but hasn't been measured yet. This keeps the
434+
* drag path consistent with clampPositionToParent (the reconcile path).
435+
*/
436+
const { width: parentWidth, height: parentHeight } = getNodeDimensions(parentNode);
432437

433438
if (parentWidth && parentHeight) {
434439
extent = [

0 commit comments

Comments
 (0)