Skip to content

Commit f44725f

Browse files
committed
cleanup
1 parent d8f393e commit f44725f

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

packages/charts/src/hooks/useObserveXAxisHeights.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { useIsomorphicLayoutEffect } from '@ui5/webcomponents-react-base/interna
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
56
const defaultAxisHeight = 30;
67

7-
function measure(container: Element | null, axisCount: number, prevHeightsRef: React.RefObject<number[]>) {
8+
/**
9+
* Measure x-axis height(s) - defaults to `defaultAxisHeight`
10+
*/
11+
function measure(container: Element | null, axisCount: number, prevHeightsRef: RefObject<number[]>) {
812
const heights = Array(axisCount).fill(defaultAxisHeight);
913
container?.querySelectorAll<SVGGraphicsElement>('.xAxis').forEach((xAxis, index) => {
1014
const height = xAxis?.getBBox()?.height;
@@ -13,29 +17,29 @@ function measure(container: Element | null, axisCount: number, prevHeightsRef: R
1317
}
1418
});
1519

16-
const prev = prevHeightsRef.current;
17-
const same = prev.length === heights.length && prev.every((v, i) => v === heights[i]);
20+
const prevHeights = prevHeightsRef.current;
21+
const same = prevHeights.length === heights.length && prevHeights.every((value, index) => value === heights[index]);
1822
if (!same) {
1923
prevHeightsRef.current = heights;
2024
return heights;
2125
}
26+
// no change
2227
return null;
2328
}
2429

2530
export const useObserveXAxisHeights = (chartRef: RefObject<SVGElement>, axisCount) => {
2631
const [xAxisHeights, setXAxisHeights] = useState(Array(axisCount).fill(defaultAxisHeight));
2732
const prevHeightsRef = useRef(xAxisHeights);
2833

29-
// Synchronous measurement after each of our own commits.
34+
// check on every render if height changed
3035
useIsomorphicLayoutEffect(() => {
3136
const result = measure(chartRef.current, axisCount, prevHeightsRef);
3237
if (result) {
3338
setXAxisHeights(result);
3439
}
3540
});
3641

37-
// MutationObserver to catch chart content appearing from ResponsiveContainer's
38-
// internal re-render and CartesianAxis's componentDidMount re-render.
42+
// check on every component DOM subtree change (catches internal rerender)
3943
useEffect(() => {
4044
const container = chartRef.current;
4145
if (!container) {

0 commit comments

Comments
 (0)