|
1 | | -import { debounce } from '@ui5/webcomponents-react-base/internal/utils'; |
| 1 | +import { useIsomorphicLayoutEffect } from '@ui5/webcomponents-react-base/internal/hooks'; |
2 | 2 | import type { RefObject } from 'react'; |
3 | 3 | import { useEffect, useRef, useState } from 'react'; |
4 | 4 |
|
5 | 5 | const defaultAxisHeight = 30; |
6 | 6 |
|
| 7 | +function measure(container: Element | null, axisCount: number, prevHeightsRef: React.RefObject<number[]>) { |
| 8 | + const heights = Array(axisCount).fill(defaultAxisHeight); |
| 9 | + container?.querySelectorAll<SVGGraphicsElement>('.xAxis').forEach((xAxis, index) => { |
| 10 | + const height = xAxis?.getBBox()?.height; |
| 11 | + if (height > defaultAxisHeight) { |
| 12 | + heights[index] = height; |
| 13 | + } |
| 14 | + }); |
| 15 | + |
| 16 | + const prev = prevHeightsRef.current; |
| 17 | + const same = prev.length === heights.length && prev.every((v, i) => v === heights[i]); |
| 18 | + if (!same) { |
| 19 | + prevHeightsRef.current = heights; |
| 20 | + return heights; |
| 21 | + } |
| 22 | + return null; |
| 23 | +} |
| 24 | + |
7 | 25 | export const useObserveXAxisHeights = (chartRef: RefObject<SVGElement>, axisCount) => { |
8 | 26 | const [xAxisHeights, setXAxisHeights] = useState(Array(axisCount).fill(defaultAxisHeight)); |
9 | | - const mostRecentXAxisHeights = useRef<number[]>(xAxisHeights); |
| 27 | + const prevHeightsRef = useRef(xAxisHeights); |
| 28 | + |
| 29 | + // Synchronous measurement after each of our own commits. |
| 30 | + useIsomorphicLayoutEffect(() => { |
| 31 | + const result = measure(chartRef.current, axisCount, prevHeightsRef); |
| 32 | + if (result) { |
| 33 | + setXAxisHeights(result); |
| 34 | + } |
| 35 | + }); |
10 | 36 |
|
| 37 | + // MutationObserver to catch chart content appearing from ResponsiveContainer's |
| 38 | + // internal re-render and CartesianAxis's componentDidMount re-render. |
11 | 39 | useEffect(() => { |
12 | | - const debouncedObserverFn = debounce(() => { |
13 | | - const defaultHeights = Array(axisCount).fill(defaultAxisHeight); |
14 | | - chartRef.current?.querySelectorAll<SVGGraphicsElement>('.xAxis').forEach((xAxis, index) => { |
15 | | - const currentAxisHeight = xAxis?.getBBox()?.height; |
16 | | - if (currentAxisHeight > 30) { |
17 | | - defaultHeights[index] = currentAxisHeight; |
18 | | - } |
19 | | - }); |
20 | | - |
21 | | - const arraysHaveTheSameLength = mostRecentXAxisHeights.current.length === defaultHeights.length; |
22 | | - const arrayContentIsIdentical = mostRecentXAxisHeights.current.every( |
23 | | - (value, index) => defaultHeights[index] === value, |
24 | | - ); |
25 | | - if (!(arraysHaveTheSameLength && arrayContentIsIdentical)) { |
26 | | - mostRecentXAxisHeights.current = defaultHeights; |
27 | | - setXAxisHeights(defaultHeights); |
28 | | - } |
29 | | - }, 75); |
30 | | - const mutationObserver = new MutationObserver(debouncedObserverFn); |
31 | | - |
32 | | - if (chartRef.current) { |
33 | | - mutationObserver.observe(chartRef.current, { |
34 | | - characterData: false, |
35 | | - characterDataOldValue: false, |
36 | | - attributes: false, |
37 | | - childList: true, |
38 | | - subtree: true, |
39 | | - }); |
| 40 | + const container = chartRef.current; |
| 41 | + if (!container) { |
| 42 | + return; |
40 | 43 | } |
41 | | - return () => { |
42 | | - debouncedObserverFn.cancel(); |
43 | | - mutationObserver.disconnect(); |
44 | | - }; |
45 | | - }, [chartRef, setXAxisHeights, mostRecentXAxisHeights]); |
| 44 | + |
| 45 | + const observer = new MutationObserver(() => { |
| 46 | + const result = measure(container, axisCount, prevHeightsRef); |
| 47 | + if (result) { |
| 48 | + setXAxisHeights(result); |
| 49 | + } |
| 50 | + }); |
| 51 | + observer.observe(container, { |
| 52 | + attributes: false, |
| 53 | + childList: true, |
| 54 | + subtree: true, |
| 55 | + }); |
| 56 | + |
| 57 | + return () => observer.disconnect(); |
| 58 | + }, [chartRef, axisCount]); |
46 | 59 |
|
47 | 60 | return xAxisHeights; |
48 | 61 | }; |
0 commit comments