Download web/src/ui/useAppFullscreen.ts from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/ui/useAppFullscreen.ts
- Command line
-
hf download hf://spaces/fsanyoto/loopable@9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/ui/useAppFullscreen.ts
-
curl -L -o useAppFullscreen.ts https://huggingface.co/spaces/fsanyoto/loopable/resolve/9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/ui/useAppFullscreen.ts
1.92 kB
| import { useCallback, useEffect, useState } from "react"; | |
| import { useEscapeClose } from "./useEscapeClose"; | |
| const TABBABLE = "button:not([disabled]),input:not([disabled]),select:not([disabled])," | |
| + "textarea:not([disabled]),[href],[tabindex]:not([tabindex=\"-1\"])"; | |
| export function useAppFullscreen(rootRef: { current: HTMLElement | null }) { | |
| const [fullscreen, setFullscreen] = useState(false); | |
| const toggleFullscreen = useCallback(() => setFullscreen(value => !value), []); | |
| const exitFullscreen = useCallback(() => setFullscreen(false), []); | |
| const container = useCallback(() => rootRef.current, [rootRef]); | |
| useEscapeClose(fullscreen, exitFullscreen, { cancelEditFirst: true, container }); | |
| useEffect(() => { | |
| if (!fullscreen) return; | |
| const html = document.documentElement; | |
| html.classList.add("is-app-fullscreen"); | |
| const onTab = (event: KeyboardEvent) => { | |
| const root = rootRef.current; | |
| if (event.key !== "Tab" || !root) return; | |
| const active = document.activeElement; | |
| const outside = !active || active === document.body || active === html; | |
| if (!outside && !root.contains(active)) return; | |
| const stops = Array.from(root.querySelectorAll<HTMLElement>(TABBABLE)) | |
| .filter(element => element.getClientRects().length > 0); | |
| if (!stops.length) return; | |
| const first = stops[0]; | |
| const last = stops[stops.length - 1]; | |
| if (outside || (event.shiftKey && active === first) || (!event.shiftKey && active === last)) { | |
| event.preventDefault(); | |
| (event.shiftKey ? last : first).focus(); | |
| } | |
| }; | |
| document.addEventListener("keydown", onTab, true); | |
| return () => { | |
| document.removeEventListener("keydown", onTab, true); | |
| if (!document.querySelector("[data-app-fullscreen]")) html.classList.remove("is-app-fullscreen"); | |
| }; | |
| }, [fullscreen, rootRef]); | |
| return { fullscreen, toggleFullscreen }; | |
| } | |