Download web/src/hooks/useModalBehavior.ts from SaylorTwift/hermes-agent: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/SaylorTwift/hermes-agent/resolve/main/web/src/hooks/useModalBehavior.ts
- Command line
-
hf download hf://SaylorTwift/hermes-agent/web/src/hooks/useModalBehavior.ts
-
curl -L -o useModalBehavior.ts https://huggingface.co/SaylorTwift/hermes-agent/resolve/main/web/src/hooks/useModalBehavior.ts
1.1 kB
| import { useEffect, useRef } from "react"; | |
| /** | |
| * Hook that adds standard modal behaviors when `open` is true: | |
| * - Escape key calls `onClose` | |
| * - Body scroll is locked | |
| * - Focus is restored to the previously focused element on close | |
| * | |
| * Returns a ref to attach to the modal container (for optional future focus trapping). | |
| */ | |
| export function useModalBehavior({ | |
| open, | |
| onClose, | |
| }: { | |
| open: boolean; | |
| onClose: () => void; | |
| }) { | |
| const containerRef = useRef<HTMLDivElement>(null); | |
| useEffect(() => { | |
| if (!open) return; | |
| const prevActive = document.activeElement as HTMLElement | null; | |
| const onKey = (e: KeyboardEvent) => { | |
| if (e.key === "Escape") { | |
| e.preventDefault(); | |
| onClose(); | |
| } | |
| }; | |
| document.addEventListener("keydown", onKey); | |
| const prevOverflow = document.body.style.overflow; | |
| document.body.style.overflow = "hidden"; | |
| return () => { | |
| document.removeEventListener("keydown", onKey); | |
| document.body.style.overflow = prevOverflow; | |
| prevActive?.focus?.(); | |
| }; | |
| }, [open, onClose]); | |
| return containerRef; | |
| } | |