[ENG-1662] Fix thumb black bars (#2167)

fix resize loop
This commit is contained in:
nikec authored and GitHub committed 2024-03-05 18:37:53 +00:00
1 parent a1c5639b03
commit eeea776292
1 file changed
+20 -2
@@ -1,10 +1,28 @@
import { useMemo, useState, type CSSProperties, type RefObject } from 'react';
import { useEffect, useMemo, useRef, useState, type CSSProperties, type RefObject } from 'react';
import { useCallbackToWatchResize } from '~/hooks';
import { useExplorerContext } from '../Context';
export function useSize(ref: RefObject<Element>) {
const explorerSettings = useExplorerContext().useSettingsSnapshot();
const initialized = useRef(false);
const [size, setSize] = useState({ width: 0, height: 0 });
useCallbackToWatchResize(({ width, height }) => setSize({ width, height }), [], ref);
useEffect(() => {
initialized.current = false;
}, [explorerSettings.gridItemSize]);
useCallbackToWatchResize(
({ width, height }) => {
if (initialized.current) return;
setSize({ width, height });
initialized.current = true;
},
[],
ref
);
return size;
}