Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / drive / src / app / components / FolderTree / hooks / useFloatingEllipsisContext.ts
blobeb8bad57376fde110b4ccd4398963840c3b2aefe
1 import type { RefObject } from 'react';
2 import { useEffect, useState } from 'react';
4 export type FloatingEllipsisEventType = 'resize' | 'scroll';
6 export interface FloatingEllipsisContextValue {
7     events: EventTarget;
8     getDimensions: () => FloatingEllipsisContainerDimensions;
11 interface FloatingEllipsisContainerDimensions {
12     scrollWidth: number;
13     scrollLeft: number;
16 export const useFloatingEllipsisContext = ({
17     containerRef,
18     resizableRef,
19 }: {
20     containerRef: RefObject<HTMLDivElement>;
21     resizableRef?: RefObject<HTMLElement>;
22 }): FloatingEllipsisContextValue => {
23     const [events] = useState<EventTarget>(new EventTarget());
24     const getDimensions = (): FloatingEllipsisContainerDimensions => ({
25         scrollWidth: containerRef.current?.clientWidth ?? 0,
26         scrollLeft: containerRef.current?.scrollLeft ?? 0,
27     });
28     const [context] = useState<FloatingEllipsisContextValue>({
29         events,
30         getDimensions,
31     });
33     const triggerScroll = () => events.dispatchEvent(new Event('scroll' as FloatingEllipsisEventType));
35     const resizeObserver = new ResizeObserver(() => {
36         events.dispatchEvent(new Event('resize' as FloatingEllipsisEventType));
37     });
39     useEffect(() => {
40         if (!containerRef.current) {
41             return;
42         }
43         resizeObserver.observe(containerRef.current);
44         containerRef.current.addEventListener('scroll', triggerScroll);
46         if (resizableRef?.current) {
47             resizeObserver.observe(resizableRef.current);
48         }
50         return () => {
51             if (!containerRef.current) {
52                 return;
53             }
54             resizeObserver.disconnect();
55             containerRef.current.removeEventListener('scroll', triggerScroll);
56         };
57     }, []);
59     return context;