|
1 | | -import { useIsomorphicLayoutEffect } from '@ui5/webcomponents-react-base/internal/hooks'; |
| 1 | +import { debounce } from '@ui5/webcomponents-react-base/internal/utils'; |
2 | 2 | import type { RefObject } from 'react'; |
3 | 3 | import { useEffect, useRef, useState } from 'react'; |
4 | 4 |
|
5 | | -// recharts default axis height -> is changed when labels are rotated |
6 | 5 | const defaultAxisHeight = 30; |
7 | 6 |
|
8 | | -/** |
9 | | - * Measure x-axis height(s) - defaults to `defaultAxisHeight` |
10 | | - */ |
11 | | -function measure(container: Element | null, axisCount: number, prevHeightsRef: RefObject<number[]>) { |
12 | | - const heights = Array(axisCount).fill(defaultAxisHeight); |
13 | | - container?.querySelectorAll<SVGGraphicsElement>('.xAxis').forEach((xAxis, index) => { |
14 | | - const height = xAxis?.getBBox()?.height; |
15 | | - if (height > defaultAxisHeight) { |
16 | | - heights[index] = height; |
17 | | - } |
18 | | - }); |
19 | | - |
20 | | - const prevHeights = prevHeightsRef.current; |
21 | | - const same = prevHeights.length === heights.length && prevHeights.every((value, index) => value === heights[index]); |
22 | | - if (!same) { |
23 | | - prevHeightsRef.current = heights; |
24 | | - return heights; |
25 | | - } |
26 | | - // no change |
27 | | - return null; |
28 | | -} |
29 | | - |
30 | 7 | export const useObserveXAxisHeights = (chartRef: RefObject<SVGElement>, axisCount) => { |
31 | 8 | const [xAxisHeights, setXAxisHeights] = useState(Array(axisCount).fill(defaultAxisHeight)); |
32 | | - const prevHeightsRef = useRef(xAxisHeights); |
33 | | - |
34 | | - // check on every render if height changed |
35 | | - useIsomorphicLayoutEffect(() => { |
36 | | - const result = measure(chartRef.current, axisCount, prevHeightsRef); |
37 | | - if (result) { |
38 | | - setXAxisHeights(result); |
39 | | - } |
40 | | - }); |
| 9 | + const mostRecentXAxisHeights = useRef<number[]>(xAxisHeights); |
41 | 10 |
|
42 | | - // check on every component DOM subtree change (catches internal rerender) |
43 | 11 | useEffect(() => { |
44 | | - const container = chartRef.current; |
45 | | - if (!container) { |
46 | | - return; |
47 | | - } |
48 | | - |
49 | | - const observer = new MutationObserver(() => { |
50 | | - const result = measure(container, axisCount, prevHeightsRef); |
51 | | - if (result) { |
52 | | - setXAxisHeights(result); |
| 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); |
53 | 28 | } |
54 | | - }); |
55 | | - observer.observe(container, { |
56 | | - attributes: false, |
57 | | - childList: true, |
58 | | - subtree: true, |
59 | | - }); |
60 | | - |
61 | | - return () => observer.disconnect(); |
62 | | - }, [chartRef, axisCount]); |
| 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 | + } |
| 41 | + return () => { |
| 42 | + debouncedObserverFn.cancel(); |
| 43 | + mutationObserver.disconnect(); |
| 44 | + }; |
| 45 | + }, [chartRef, setXAxisHeights, mostRecentXAxisHeights]); |
63 | 46 |
|
64 | 47 | return xAxisHeights; |
65 | 48 | }; |
0 commit comments