{"version":3,"file":"useMenuWideEvents.cjs","sources":["../../../../src/components/menu/useMenuWideEvents.ts"],"sourcesContent":["import type { FloatingTreeType } from \"@floating-ui/react\";\nimport { useEffect, useState } from \"react\";\n\nexport const useMenuWideEvents = (\n    tree: FloatingTreeType | null,\n    nodeId: string | undefined,\n    parentId: string | null,\n): {\n    allowHover: boolean;\n    isOpen: boolean;\n    setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;\n} => {\n    const [allowHover, setAllowHover] = useState(false);\n    const [isOpen, setIsOpen] = useState(false);\n\n    // Event emitter allows you to communicate across tree components.\n    // This effect closes all menus when an item gets clicked anywhere\n    // in the tree.\n    useEffect(() => {\n        if (!tree) return;\n\n        function handleTreeClick() {\n            setIsOpen(false);\n        }\n\n        function onSubMenuOpen(event: { nodeId: string; parentId: string }) {\n            if (\n                nodeId &&\n                event.nodeId !== nodeId &&\n                event.parentId === parentId\n            ) {\n                setIsOpen(false);\n            }\n        }\n\n        tree.events.on(\"click\", handleTreeClick);\n        tree.events.on(\"menuopen\", onSubMenuOpen);\n\n        return () => {\n            tree.events.off(\"click\", handleTreeClick);\n            tree.events.off(\"menuopen\", onSubMenuOpen);\n        };\n    }, [tree, nodeId, parentId]);\n\n    useEffect(() => {\n        if (isOpen && tree && nodeId) {\n            tree.events.emit(\"menuopen\", { parentId, nodeId });\n        }\n    }, [tree, isOpen, nodeId, parentId]);\n\n    // Determine if \"hover\" logic can run based on the modality of input. This\n    // prevents unwanted focus synchronization as menus open and close with\n    // keyboard navigation and the cursor is resting on the menu.\n    // biome-ignore lint/correctness/useExhaustiveDependencies:\n    useEffect(() => {\n        function onPointerMove({ pointerType }: PointerEvent) {\n            if (pointerType !== \"touch\") {\n                setAllowHover(true);\n            }\n        }\n\n        function onKeyDown() {\n            setAllowHover(false);\n        }\n\n        window.addEventListener(\"pointermove\", onPointerMove, {\n            once: true,\n            capture: true,\n        });\n        window.addEventListener(\"keydown\", onKeyDown, true);\n        return () => {\n            window.removeEventListener(\"pointermove\", onPointerMove, {\n                capture: true,\n            });\n            window.removeEventListener(\"keydown\", onKeyDown, true);\n        };\n    }, [allowHover]);\n\n    return { allowHover, isOpen, setIsOpen };\n};\n"],"names":["tree","nodeId","parentId","allowHover","setAllowHover","useState","isOpen","setIsOpen","useEffect","events","on","handleTreeClick","onSubMenuOpen","off","event","emit","onPointerMove","pointerType","onKeyDown","addEventListener","once","capture","window","removeEventListener"],"mappings":"mIAGiC,CAC7BA,EACAC,EACAC,KAMA,MAAOC,EAAYC,GAAiBC,EAAAA,UAAS,IACtCC,EAAQC,GAAaF,EAAAA,UAAS,GAKrCG,OAAAA,EAAAA,UAAU,KACN,GAAKR,EAgBL,OAAAA,EAAKS,OAAOC,GAAG,QAASC,GACxBX,EAAKS,OAAOC,GAAG,WAAYE,GAEpB,KACHZ,EAAKS,OAAOI,IAAI,QAASF,GACzBX,EAAKS,OAAOI,IAAI,WAAYD,IAnBhC,SAASD,IACLJ,GAAU,EACd,CAEA,SAASK,EAAcE,GAEfb,GACAa,EAAMb,SAAWA,GACjBa,EAAMZ,WAAaA,GAEnBK,GAAU,EAElB,GASD,CAACP,EAAMC,EAAQC,IAElBM,EAAAA,UAAU,KACFF,GAAUN,GAAQC,GAClBD,EAAKS,OAAOM,KAAK,WAAY,CAAEb,SAAAA,EAAUD,OAAAA,KAE9C,CAACD,EAAMM,EAAQL,EAAQC,IAM1BM,EAAAA,UAAU,KACN,SAASQ,GAAgBC,YAAAA,IACD,UAAhBA,GACAb,GAAc,EAEtB,CAEA,SAASc,IACLd,GAAc,EAClB,CAEA,cAAOe,iBAAiB,cAAeH,EAAe,CAClDI,MAAM,EACNC,SAAS,IAEbC,OAAOH,iBAAiB,UAAWD,GAAW,GACvC,KACHI,OAAOC,oBAAoB,cAAeP,EAAe,CACrDK,SAAS,IAEbC,OAAOC,oBAAoB,UAAWL,GAAW,KAEtD,CAACf,IAEG,CAAEA,WAAAA,EAAYG,OAAAA,EAAQC,UAAAA"}