Skip to content

Commit b235ea0

Browse files
committed
fix: respect flow nodeOrigin in root node intersections
1 parent 8523d8d commit b235ea0

8 files changed

Lines changed: 189 additions & 3 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@xyflow/react': patch
3+
'@xyflow/svelte': patch
4+
---
5+
6+
Respect the flow's `nodeOrigin` when checking intersections with root node objects.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useState } from 'react';
2+
import { useReactFlow } from '@xyflow/react';
3+
4+
export default function IntersectionOriginNode() {
5+
const { getNode, getIntersectingNodes, isNodeIntersecting } = useReactFlow();
6+
const [result, setResult] = useState('');
7+
8+
const checkIntersections = () => {
9+
const root = getNode('root')!;
10+
const area = { x: 55, y: 55, width: 5, height: 5 };
11+
12+
setResult(
13+
JSON.stringify({
14+
rootObject: isNodeIntersecting(root, area),
15+
rootId: isNodeIntersecting({ id: root.id }, area),
16+
explicitTargets: getIntersectingNodes(area, true, [root]).map((node) => node.id),
17+
implicitTargets: getIntersectingNodes(area).map((node) => node.id),
18+
ownOrigin: isNodeIntersecting(getNode('own-origin')!, { x: 295, y: 295, width: 2, height: 2 }),
19+
child: isNodeIntersecting(getNode('child')!, { x: 426, y: 426, width: 5, height: 5 }),
20+
})
21+
);
22+
};
23+
24+
return (
25+
<div>
26+
<button className="nodrag" data-testid="check-intersections" onClick={checkIntersections}>
27+
Check intersections
28+
</button>
29+
<output data-testid="intersection-result">{result}</output>
30+
</div>
31+
);
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import IntersectionOriginNode from './components/IntersectionOriginNode';
2+
3+
export default {
4+
flowProps: {
5+
fitView: true,
6+
nodeOrigin: [0.5, 0.5],
7+
nodeTypes: { intersectionOrigin: IntersectionOriginNode },
8+
nodes: [
9+
{ id: 'root', position: { x: 100, y: 100 }, width: 100, height: 100, data: { label: 'root' } },
10+
{
11+
id: 'own-origin',
12+
position: { x: 300, y: 300 },
13+
width: 100,
14+
height: 100,
15+
origin: [0, 0],
16+
data: { label: 'own origin' },
17+
},
18+
{ id: 'parent', position: { x: 500, y: 500 }, width: 200, height: 200, data: { label: 'parent' } },
19+
{
20+
id: 'child',
21+
parentId: 'parent',
22+
position: { x: 50, y: 50 },
23+
width: 50,
24+
height: 50,
25+
data: { label: 'child' },
26+
},
27+
{
28+
id: 'probe',
29+
type: 'intersectionOrigin',
30+
position: { x: 0, y: 700 },
31+
width: 300,
32+
data: {},
33+
},
34+
],
35+
edges: [],
36+
},
37+
} satisfies FlowConfig;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<script lang="ts">
2+
import { useSvelteFlow } from '@xyflow/svelte';
3+
4+
const { getNode, getIntersectingNodes, isNodeIntersecting } = useSvelteFlow();
5+
let result = $state('');
6+
7+
function checkIntersections() {
8+
const root = getNode('root')!;
9+
const area = { x: 55, y: 55, width: 5, height: 5 };
10+
11+
result = JSON.stringify({
12+
rootObject: isNodeIntersecting(root, area),
13+
rootId: isNodeIntersecting({ id: root.id }, area),
14+
explicitTargets: getIntersectingNodes(area, true, [root]).map((node) => node.id),
15+
implicitTargets: getIntersectingNodes(area).map((node) => node.id),
16+
ownOrigin: isNodeIntersecting(getNode('own-origin')!, {
17+
x: 295,
18+
y: 295,
19+
width: 2,
20+
height: 2
21+
}),
22+
child: isNodeIntersecting(getNode('child')!, { x: 426, y: 426, width: 5, height: 5 })
23+
});
24+
}
25+
</script>
26+
27+
<div>
28+
<button class="nodrag" data-testid="check-intersections" onclick={checkIntersections}>
29+
Check intersections
30+
</button>
31+
<output data-testid="intersection-result">{result}</output>
32+
</div>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import IntersectionOriginNode from './components/IntersectionOriginNode.svelte';
2+
3+
export default {
4+
flowProps: {
5+
fitView: true,
6+
nodeOrigin: [0.5, 0.5],
7+
nodeTypes: { intersectionOrigin: IntersectionOriginNode },
8+
nodes: [
9+
{
10+
id: 'root',
11+
position: { x: 100, y: 100 },
12+
width: 100,
13+
height: 100,
14+
data: { label: 'root' }
15+
},
16+
{
17+
id: 'own-origin',
18+
position: { x: 300, y: 300 },
19+
width: 100,
20+
height: 100,
21+
origin: [0, 0],
22+
data: { label: 'own origin' }
23+
},
24+
{
25+
id: 'parent',
26+
position: { x: 500, y: 500 },
27+
width: 200,
28+
height: 200,
29+
data: { label: 'parent' }
30+
},
31+
{
32+
id: 'child',
33+
parentId: 'parent',
34+
position: { x: 50, y: 50 },
35+
width: 50,
36+
height: 50,
37+
data: { label: 'child' }
38+
},
39+
{
40+
id: 'probe',
41+
type: 'intersectionOrigin',
42+
position: { x: 0, y: 700 },
43+
width: 300,
44+
data: {}
45+
}
46+
],
47+
edges: []
48+
}
49+
} satisfies FlowConfig;

‎packages/react/src/hooks/useReactFlow.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge
9191
height: nodeToUse.measured?.height ?? nodeToUse.height,
9292
};
9393

94-
return nodeToRect(nodeWithPosition);
94+
// Child positions already account for the flow origin above.
95+
return nodeToRect(nodeWithPosition, nodeToUse.parentId ? undefined : nodeOrigin);
9596
};
9697

9798
const updateNode: GeneralHelpers<NodeType, EdgeType>['updateNode'] = (
@@ -216,14 +217,15 @@ export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge
216217
return [];
217218
}
218219

220+
const { nodeOrigin } = store.getState();
219221
return (nodes || store.getState().nodes).filter((n) => {
220222
const internalNode = store.getState().nodeLookup.get(n.id);
221223

222224
if (internalNode && !isRect && (n.id === nodeOrRect.id || !internalNode.internals.positionAbsolute)) {
223225
return false;
224226
}
225227

226-
const currNodeRect = nodeToRect(hasNodesOption ? n : internalNode!);
228+
const currNodeRect = nodeToRect(hasNodesOption ? n : internalNode!, nodeOrigin);
227229
const overlappingArea = getOverlappingArea(currNodeRect, nodeRect);
228230
const partiallyVisible = partially && overlappingArea > 0;
229231

‎packages/svelte/src/lib/hooks/useSvelteFlow.svelte.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ export function useSvelteFlow<NodeType extends Node = Node, EdgeType extends Edg
297297
height: nodeToUse.measured?.height ?? nodeToUse.height
298298
};
299299

300-
return nodeToRect(nodeWithPosition);
300+
// Child positions already account for the flow origin above.
301+
return nodeToRect(nodeWithPosition, nodeToUse.parentId ? undefined : store.nodeOrigin);
301302
};
302303

303304
function updateNode(
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
import { FRAMEWORK } from './constants';
4+
5+
const expectedResults = {
6+
rootObject: true,
7+
rootId: true,
8+
explicitTargets: ['root'],
9+
implicitTargets: ['root'],
10+
ownOrigin: false,
11+
child: true,
12+
};
13+
14+
test.describe('intersection helpers with a non-default node origin', () => {
15+
test.beforeEach(async ({ page }) => {
16+
await page.goto('/tests/generic/nodes/intersection-origin');
17+
await expect(page.locator(`.${FRAMEWORK}-flow__node[data-id="child"]`)).toHaveCSS('visibility', 'visible');
18+
await page.getByTestId('check-intersections').click();
19+
});
20+
21+
for (const [name, expected] of Object.entries(expectedResults)) {
22+
test(name, async ({ page }) => {
23+
const result = JSON.parse((await page.getByTestId('intersection-result').textContent())!);
24+
expect(result[name]).toEqual(expected);
25+
});
26+
}
27+
});

0 commit comments

Comments
 (0)