()
| 15 | const DEFAULT_STYLE: Style = [0.5, 0.5, 0.25, 3.5, 4, 1, 1]; |
| 16 | |
| 17 | export 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 | |
| 52 | export const getPlotSize = ([, chartSize, chartStyle]: RefAndLayout) => { |
| 53 | const [, , width, height] = getPlotFrame(chartSize, chartStyle); |
no test coverage detected
searching dependent graphs…