Skip to content

Commit

Permalink
Merge pull request #204 from reaviz/fix/re-center-camera-on-nodes-update
Browse files Browse the repository at this point in the history
Re-center camera when nodes update
  • Loading branch information
amcdnl committed Mar 18, 2024
2 parents b3df262 + 36c0e34 commit 5ad4804
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/CameraControls/useCenterGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,21 @@ export const useCenterGraph = ({

useLayoutEffect(() => {
async function load() {
// Center the graph once nodes are loaded on mount
if (controls && nodes?.length && !mounted.current) {
await centerNodes(nodes, false);
mounted.current = true;
// Once we've loaded controls and we have nodes, let's recenter
if (controls && nodes?.length) {
if (!mounted.current) {
// Center the graph once nodes are loaded on mount
await centerNodes(nodes, false);
mounted.current = true;
} else {
// If node positions have changed and some aren't in view, center the graph
await centerNodes(nodes, animated);
}
}
}

load();
}, [controls, centerNodes, nodes]);
}, [controls, centerNodes, nodes, animated]);

useHotkeys([
{
Expand Down

0 comments on commit 5ad4804

Please sign in to comment.