Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / useDragMove.ts
blobe2e206da1ddac88ee2126a3c9fbfe5f2ed9298fa
1 import type { DragEvent, ReactNode } from 'react';
2 import { useEffect, useMemo, useRef } from 'react';
3 import { createPortal } from 'react-dom';
5 interface DragMoveContentProps<T> {
6     data?: T;
7     dragging?: boolean;
8     children: ReactNode;
11 interface UseDragMoveParams {
12     dragging: boolean;
13     setDragging: (value: boolean) => void;
14     format?: string;
15     formatter?: (value: any) => string;
18 function useDragMove({ dragging, setDragging, format = 'text/plain', formatter = (str) => str }: UseDragMoveParams) {
19     const container = useRef<HTMLDivElement>();
20     const transferData = useRef<any>();
22     const handleDragStart = (event: DragEvent<HTMLTableRowElement>) => {
23         if (!container.current) {
24             container.current = document.createElement('div');
25             container.current.className = 'absolute';
26         }
27         document.body.appendChild(container.current);
28         event.dataTransfer.setDragImage(container.current, 0, 0);
29         event.dataTransfer.setData(format, formatter(transferData.current));
30         setDragging(true);
31     };
33     const handleDragEnd = () => {
34         if (container.current) {
35             document.body.removeChild(container.current);
36         }
38         setDragging(false);
39     };
41     const DragMoveContent = useMemo(
42         () =>
43             function Component<T>({ children, data, dragging }: DragMoveContentProps<T>) {
44                 transferData.current = data;
46                 useEffect(() => {
47                     return () => {
48                         transferData.current = undefined;
49                     };
50                 }, []);
52                 if (dragging && container.current) {
53                     return createPortal(children, container.current);
54                 }
55                 return null;
56             },
57         []
58     );
60     return { dragging, handleDragEnd, handleDragStart, DragMoveContent };
63 export default useDragMove;