MCPcopy
hub / github.com/tinyplex/tinybase / useLayout

Function useLayout

src/ui-react-dom-charts/common/svg.ts:17–50  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15const DEFAULT_STYLE: Style = [0.5, 0.5, 0.25, 3.5, 4, 1, 1];
16
17export const useLayout = (): RefAndLayout => {
18 const svgRef = useRef<SVGSVGElement>(null);
19 const chartLayoutRef = useRef<readonly [Size, Style]>([
20 DEFAULT_SIZE,
21 getDefaultStyle(DEFAULT_FONT_SIZE),
22 ]);
23 const [chartLayout, setChartLayout] = useState<readonly [Size, Style]>([
24 DEFAULT_SIZE,
25 getDefaultStyle(DEFAULT_FONT_SIZE),
26 ]);
27
28 useLayoutEffect(() => {
29 const svg = svgRef.current;
30 if (isNull(svg)) {
31 return;
32 }
33
34 const updateLayout = ({contentRect}: ResizeObserverEntry) => {
35 const style = svg.ownerDocument.defaultView?.getComputedStyle(svg);
36 const nextLayout = [getSvgSize(contentRect), getStyle(style)] as const;
37 if (!isLayoutEqual(chartLayoutRef.current, nextLayout)) {
38 chartLayoutRef.current = nextLayout;
39 setChartLayout(nextLayout);
40 }
41 };
42
43 const observer = new ResizeObserver(([entry]) => updateLayout(entry));
44 observer.observe(svg);
45
46 return () => observer.disconnect();
47 }, []);
48
49 return [svgRef, ...chartLayout];
50};
51
52export const getPlotSize = ([, chartSize, chartStyle]: RefAndLayout) => {
53 const [, , width, height] = getPlotFrame(chartSize, chartStyle);

Callers 1

CartesianChartFunction · 0.90

Calls 4

isNullFunction · 0.90
disconnectMethod · 0.80
getDefaultStyleFunction · 0.70
updateLayoutFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…