hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { createSignal, onCleanup } from "solid-js"; |
| 2 | |
| 3 | type FsElement = HTMLElement & { webkitRequestFullscreen?: () => Promise<void> | void }; |
| 4 | type FsDocument = Document & { |
| 5 | webkitFullscreenElement?: Element | null; |
| 6 | webkitExitFullscreen?: () => Promise<void> | void; |
| 7 | }; |
| 8 | |
| 9 | // Ancestor styles that turn an ancestor into the containing block of a |
| 10 | // `position: fixed` child (a SidePanel's slide-in `translate`, for one) and |
| 11 | // would trap the CSS fallback overlay inside it. Neutralised while it is up. |
| 12 | const TRAPPING_STYLES = ["transform", "translate", "scale", "rotate", "filter", "backdrop-filter", "perspective", "contain", "will-change"] as const; |
| 13 | |
| 14 | const fullscreenElement = () => { |
| 15 | const d = document as FsDocument; |
| 16 | return d.fullscreenElement ?? d.webkitFullscreenElement ?? null; |
| 17 | }; |
| 18 | |
| 19 | /** |
| 20 | * Full-screen state for one element. Uses the Fullscreen API (with the webkit |
| 21 | * prefix for Safari) where the browser offers it on arbitrary elements; iPhone |
| 22 | * Safari does not, so there `active()` still flips and the host renders itself |
| 23 | * as a fixed, inset-0 overlay instead (`native()` tells the two apart). Either |
| 24 | * way the host styles itself off `active()`. |
| 25 | * |
| 26 | * Escape and the host's toggle exit. While active, an Escape keydown is |
| 27 | * swallowed in the capture phase so it doesn't also reach a surrounding |
| 28 | * Kobalte dialog (a SidePanel would otherwise close with the drawing in it). |
| 29 | */ |
| 30 | export function createFullscreen(target: () => HTMLElement | undefined) { |
| 31 | const [active, setActive] = createSignal(false); |
| 32 | const [native, setNative] = createSignal(false); |
| 33 | let restore: (() => void) | undefined; |
| 34 | |
| 35 | const pinOverlay = (el: HTMLElement) => { |
| 36 | const saved: Array<[HTMLElement, string, string, string]> = []; |
| 37 | for (let node = el.parentElement; node && node !== document.body; node = node.parentElement) { |
| 38 | // A dialog's entry animation (fill-mode both) keeps a transform "in effect" |
| 39 | // after it finishes, and Chromium still treats that as a containing block |
| 40 | // even under an !important override. Its end state is the resting layout, |
| 41 | // so cancelling it changes nothing visible. |
| 42 | for (const anim of node.getAnimations?.() ?? []) anim.cancel(); |
| 43 | for (const prop of TRAPPING_STYLES) { |
| 44 | saved.push([node, prop, node.style.getPropertyValue(prop), node.style.getPropertyPriority(prop)]); |
| 45 | node.style.setProperty(prop, prop === "will-change" ? "auto" : "none", "important"); |
| 46 | } |
| 47 | } |
| 48 | const root = document.documentElement; |
| 49 | const overflow = root.style.overflow; |
| 50 | root.style.overflow = "hidden"; |
| 51 | return () => { |
| 52 | for (const [node, prop, value, priority] of saved) node.style.setProperty(prop, value, priority); |
| 53 | root.style.overflow = overflow; |
| 54 | }; |
| 55 | }; |
| 56 | |
| 57 | const enter = async () => { |
| 58 | const el = target() as FsElement | undefined; |
| 59 | if (!el || active()) return; |
| 60 | const request = el.requestFullscreen ?? el.webkitRequestFullscreen; |
| 61 | if (request && (document.fullscreenEnabled ?? true)) { |
| 62 | try { |
| 63 | await request.call(el); |
| 64 | setNative(true); |
| 65 | setActive(true); |
| 66 | return; |
| 67 | } catch { |
| 68 | // Refused (no gesture, iframe policy…) — fall through to the overlay. |
| 69 | } |
| 70 | } |
| 71 | restore = pinOverlay(el); |
| 72 | setNative(false); |
| 73 | setActive(true); |
| 74 | }; |
| 75 | |
| 76 | const exit = async () => { |
| 77 | if (!active()) return; |
| 78 | if (native()) { |
| 79 | const d = document as FsDocument; |
| 80 | if (fullscreenElement()) { |
| 81 | const leave = d.exitFullscreen ?? d.webkitExitFullscreen; |
| 82 | try { |
| 83 | await leave?.call(d); |
| 84 | } catch { |
| 85 | // Already left; the change listener below settles the state. |
| 86 | } |
| 87 | } |
| 88 | } else { |
| 89 | restore?.(); |
| 90 | restore = undefined; |
| 91 | } |
| 92 | setNative(false); |
| 93 | setActive(false); |
| 94 | }; |
| 95 | |
| 96 | const toggle = () => void (active() ? exit() : enter()); |
| 97 | |
| 98 | // When the browser itself leaves native full screen on Escape, Chromium can |
| 99 | // still hand that same keydown to the page *after* fullscreenchange — by then |
| 100 | // active() is false and the Escape would close the dialog around the canvas. |
| 101 | let leftNativeAt = -Infinity; |
| 102 | const ESC_GRACE_MS = 500; |
| 103 | |
| 104 | // The browser leaves native full screen on its own (Escape, a system gesture). |
| 105 | const onChange = () => { |
| 106 | if (native() && fullscreenElement() !== target()) { |
| 107 | leftNativeAt = performance.now(); |
| 108 | setNative(false); |
| 109 | setActive(false); |
| 110 | } |
| 111 | }; |
| 112 | const onKey = (e: KeyboardEvent) => { |
| 113 | if (e.key !== "Escape") return; |
| 114 | if (!active() && performance.now() - leftNativeAt > ESC_GRACE_MS) return; |
| 115 | e.preventDefault(); |
| 116 | e.stopPropagation(); |
| 117 | void exit(); |
| 118 | }; |
| 119 | document.addEventListener("fullscreenchange", onChange); |
| 120 | document.addEventListener("webkitfullscreenchange", onChange); |
| 121 | window.addEventListener("keydown", onKey, true); |
| 122 | onCleanup(() => { |
| 123 | document.removeEventListener("fullscreenchange", onChange); |
| 124 | document.removeEventListener("webkitfullscreenchange", onChange); |
| 125 | window.removeEventListener("keydown", onKey, true); |
| 126 | void exit(); |
| 127 | }); |
| 128 | |
| 129 | return { active, native, enter, exit, toggle }; |
| 130 | } |
| 131 | |
| 132 | /** Classes a host adds to its root while `active()` — covers the viewport in both modes. */ |
| 133 | export const FULLSCREEN_ROOT_CLASS = |
| 134 | "fixed inset-0 z-[100] m-0 flex flex-col gap-2 space-y-0 bg-background p-2 pt-[max(0.5rem,env(safe-area-inset-top))] pr-[max(0.5rem,env(safe-area-inset-right))] pb-[max(0.5rem,env(safe-area-inset-bottom))] pl-[max(0.5rem,env(safe-area-inset-left))]"; |