Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / calendar / src / app / hooks / useRect.ts
blob1ff7317f118ac2df0cbc2b6712908e22198b4acc
1 import { useLayoutEffect, useState } from 'react';
3 import type { Rect } from './observeRect';
4 import observeRect from './observeRect';
6 export function useRect(nodeRef: HTMLElement | null, observe = true, once = false, dependency?: any) {
7     const [rect, setRect] = useState<Rect | undefined>(undefined);
9     useLayoutEffect(() => {
10         if (!nodeRef || !observe) {
11             setRect(undefined);
12             return;
13         }
15         if (once) {
16             let onceState = false;
18             const stop = observeRect(nodeRef, (rect: Rect) => {
19                 if (!onceState) {
20                     onceState = true;
21                     stop();
22                     setRect(rect);
23                 }
24             });
26             return stop;
27         }
29         return observeRect(nodeRef, (rect) => {
30             setRect(rect);
31         });
32     }, [nodeRef, observe, once, dependency]);
34     return rect;