|
1 | | -import { useStore, useStoreApi } from "@xyflow/react" |
2 | | -import { useEffect } from "react" |
3 | | -import { CANVAS } from "@/constants" |
4 | | - |
5 | | -/** |
6 | | - * Connection indicators keep a usable minimum on-screen size when zoomed out and |
7 | | - * grow with the node when zoomed in: |
8 | | - * scale = 1 / min(zoom, 1) — constant on screen for zoom <= 1, natural above. |
9 | | - */ |
10 | | -export const arcScaleForZoom = (zoom: number): number => |
11 | | - 1 / |
12 | | - Math.min( |
13 | | - Math.max( |
14 | | - Number.isFinite(zoom) && zoom > 0 ? zoom : 1, |
15 | | - CANVAS.MIN_SCALE_TO_ZOOM_OUT |
16 | | - ), |
17 | | - 1 |
18 | | - ) |
| 1 | +import { useStore } from "@xyflow/react" |
| 2 | +import { useLayoutEffect } from "react" |
| 3 | +import { getHandleScreenScale } from "@/utils/geometry/scalar" |
19 | 4 |
|
20 | 5 | /** |
21 | 6 | * Publishes `--arc-scale` once for the whole canvas. |
22 | 7 | * |
23 | | - * Every node's wrapper used to read `state.transform[2]` to write this custom |
24 | | - * property onto each handle's inline style. That subscribes every node to the |
25 | | - * zoom, so a single wheel gesture re-rendered every node on every frame — the |
26 | | - * exact pattern React Flow's performance guide warns about, and the reason |
27 | | - * zooming got heavier the more elements a diagram had. |
28 | | - * |
29 | | - * Custom properties inherit, so the value only has to exist on an ancestor. This |
30 | | - * writes it straight to the DOM from a store subscription instead of rendering: |
31 | | - * no node re-renders, and no React render at all per frame — just one style |
32 | | - * property set, and only when the value actually changes. |
| 8 | + * Custom properties inherit, so the value only has to exist on an ancestor. |
| 9 | + * Publishing it here rather than from each node's style keeps zoom out of every |
| 10 | + * node's subscription list, and reducing to the scale inside the selector means a |
| 11 | + * re-render only when the scale itself changes, not on every frame of a zoom. |
33 | 12 | */ |
34 | 13 | export const ArcScalePublisher = () => { |
35 | 14 | const domNode = useStore((state) => state.domNode) |
36 | | - const store = useStoreApi() |
37 | | - |
38 | | - useEffect(() => { |
39 | | - if (!domNode) return |
40 | | - |
41 | | - let published: number | undefined |
42 | | - const write = (zoom: number) => { |
43 | | - const scale = arcScaleForZoom(zoom) |
44 | | - // Zoom changes every frame of a gesture; the scale it maps to does not once |
45 | | - // zoomed past 1, and the DOM write is skipped whenever it has not moved. |
46 | | - if (scale === published) return |
47 | | - published = scale |
48 | | - domNode.style.setProperty("--arc-scale", String(scale)) |
49 | | - } |
| 15 | + const scale = useStore((state) => getHandleScreenScale(state.transform[2])) |
50 | 16 |
|
51 | | - write(store.getState().transform[2]) |
52 | | - return store.subscribe((state) => write(state.transform[2])) |
53 | | - }, [domNode, store]) |
| 17 | + // Layout effect: this is a style the same frame paints with. |
| 18 | + useLayoutEffect(() => { |
| 19 | + domNode?.style.setProperty("--arc-scale", String(scale)) |
| 20 | + }, [domNode, scale]) |
54 | 21 |
|
55 | 22 | return null |
56 | 23 | } |
0 commit comments