Skip to content

Commit 2fde376

Browse files
committed
fix(charts): eliminate debounce flicker in useObserveXAxisHeights
1 parent 1a6e7ce commit 2fde376

1 file changed

Lines changed: 48 additions & 35 deletions

File tree

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,61 @@
1-
import { debounce } from '@ui5/webcomponents-react-base/internal/utils';
1+
import { useIsomorphicLayoutEffect } from '@ui5/webcomponents-react-base/internal/hooks';
22
import type { RefObject } from 'react';
33
import { useEffect, useRef, useState } from 'react';
44

55
const defaultAxisHeight = 30;
66

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+
725
export const useObserveXAxisHeights = (chartRef: RefObject<SVGElement>, axisCount) => {
826
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+
});
1036

37+
// MutationObserver to catch chart content appearing from ResponsiveContainer's
38+
// internal re-render and CartesianAxis's componentDidMount re-render.
1139
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;
4043
}
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]);
4659

4760
return xAxisHeights;
4861
};

0 commit comments

Comments
 (0)