Skip to content

Commit bfcd11a

Browse files
committed
Revert accidental commits from fix/use-observe-xaxis-heights-feedback-loop
1 parent f44725f commit bfcd11a

1 file changed

Lines changed: 35 additions & 52 deletions

File tree

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

5-
// recharts default axis height -> is changed when labels are rotated
65
const defaultAxisHeight = 30;
76

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-
307
export const useObserveXAxisHeights = (chartRef: RefObject<SVGElement>, axisCount) => {
318
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);
4110

42-
// check on every component DOM subtree change (catches internal rerender)
4311
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);
5328
}
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]);
6346

6447
return xAxisHeights;
6548
};

0 commit comments

Comments
 (0)