mirror of
https://github.com/spacedriveapp/spacedrive.git
synced 2026-10-20 08:04:33 -04: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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user