Spaces:
Configuration error
Configuration error
Download frontend/src/hooks/use-click-outside-element.ts from flyingdarkdev/README: direct link, hf CLI and curl.
- Browser
- Download file 679 Bytes
-
https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/src/hooks/use-click-outside-element.ts
- Command line
-
hf download hf://spaces/flyingdarkdev/README/frontend/src/hooks/use-click-outside-element.ts
-
curl -L -o use-click-outside-element.ts https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/src/hooks/use-click-outside-element.ts
679 Bytes
| import React from "react"; | |
| /** | |
| * Hook to call a callback function when an element is clicked outside | |
| * @param callback The callback function to call when the element is clicked outside | |
| */ | |
| export const useClickOutsideElement = <T extends HTMLElement>( | |
| callback: () => void, | |
| ) => { | |
| const ref = React.useRef<T>(null); | |
| React.useEffect(() => { | |
| const handleClickOutside = (event: MouseEvent) => { | |
| if (ref.current && !ref.current.contains(event.target as Node)) { | |
| callback(); | |
| } | |
| }; | |
| document.addEventListener("click", handleClickOutside); | |
| return () => document.removeEventListener("click", handleClickOutside); | |
| }, []); | |
| return ref; | |
| }; | |