// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The Docs window's body (docs.ts opens the window, wm.ts draws its frame): // a compact toolbar under the title bar (Home: the site's first page again; // open the site in a new tab) over a credentialless iframe on DOCS_URL in // the page's theme (docs.ts DOCS_EMBED_URL). Ids: #docs (the root), #docs-home, #docs-tab, #docs-frame. // - Under the toolbar it is glass, as a Terminal window's content // (style.css): the iframe's page has a transparent background. // - Pointer and keyboard as in the Browser (Preview.tsx): a click in the // page (another origin: seen as the page's blur with the iframe focused) // raises and focuses the window; the WM's focus moves the DOM focus to the // root, and losing it gives it up, so keys reach no terminal under it. import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react"; import { DOCS_EMBED_URL, DOCS_THEME, DOCS_URL, docsHome, docsNav$ } from "../docs.ts"; import { useStore } from "../store.ts"; import { useWm, wm } from "../wm.ts"; import { DocsIcon, ExternalIcon } from "./icons.tsx"; /** A press in the page this recently (ms) gave the window the focus: the page keeps the DOM focus. */ const PRESS_MS = 1000; const tool = "grid h-7 w-7 flex-none place-items-center rounded-md text-muted transition-colors hover:bg-ink/8 hover:text-fg disabled:pointer-events-none disabled:opacity-40 pointer-coarse:h-10 pointer-coarse:w-10"; const HOME_SVG = ; /** React passes an unknown attribute through as it is; the DOM types lack this one. */ const CREDENTIALLESS = { credentialless: "" } as Record; export function Docs({ id }: { id: string }) { const { nav } = useSyncExternalStore(docsNav$.subscribe, docsNav$.get); const theme = useStore((s) => s.shownTheme); const focused = useWm((s) => s.focused === id); const [loading, setLoading] = useState(true); const root = useRef(null); const frame = useRef(null); const pressed = useRef(0); // The theme the iframe loads with, taken again only with Home: a new src // navigates the site, so a theme change is posted to it instead (docs.ts). const srcTheme = useMemo(() => theme, [nav]); const retheme = () => frame.current?.contentWindow?.postMessage({ type: "undocs:embed-theme", theme: DOCS_THEME[theme] }, new URL(DOCS_URL).origin); useEffect(() => { const r = root.current, at = document.activeElement; if (!r) return; if (focused) { if (at && r.contains(at)) return; if (performance.now() - pressed.current < PRESS_MS) return; r.focus({ preventScroll: true }); } else if (at && r.contains(at)) { r.focus({ preventScroll: true }); r.blur(); } }, [focused]); useEffect(() => { const onBlur = () => setTimeout(() => { if (frame.current && document.activeElement === frame.current) { pressed.current = performance.now(); wm.focus(id); } }, 0); addEventListener("blur", onBlur); return () => removeEventListener("blur", onBlur); }, [id]); useEffect(() => { setLoading(true); }, [nav]); useEffect(() => { if (theme !== srcTheme) retheme(); }, [theme]); return (
{new URL(DOCS_URL).host} {loading &&