Files
unkernel/web/src/docs.ts
Adam H 0c92b2370c web: Light / Dark / Auto theme (#53)
* web: add a terminal colour theme option (Auto, Dark, Light)

* web: add an option to turn off the Terminal window's see-through glass

* web: default the terminal colours to Auto

* web: add a page colour theme option (Auto, Dark, Light)

* web: give the chrome's materials in style.css a light variant

* web: give the components' chrome classes a light variant

* web: follow the theme in the charts, the alerts' caution badge and the Docs window

* web: fix the light theme's Dock glyphs, Resume card veil and Browser toolbar hover

* web: read the shown theme from the store and type the option field names

* web: darken the light theme's faint and error colours to 4.5:1 on tints

* web: give the light terminal mid-tone colours that read under black text

* web: lighten the light terminal's green, yellow and cyan for black text on them

* web: use the dark terminal's green in the light terminal

* web: give the light terminal's green, yellow and cyan 4.5:1 on white, as its red, blue and magenta

* web: give the light theme a white status bar on iOS

* web: make the Docs window opaque

* web: post a theme change to the Docs window instead of reloading it

* docs: upgrade undocs nightly, for the Docs window's theme messages

---------

Co-authored-by: Pooya Parsa <pooya@pi0.io>
2026-11-28 22:34:15 +01:00

76 lines
4.2 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// The Docs window (components/Docs.tsx is its body): unkernel's documentation
// site, DOCS_URL, in an iframe in a desktop window (wm.ts), a singleton like
// Settings. Opened from its launcher app (apps/system/docs.ts), which is
// pinned in the Dock by default (pins.ts).
//
// - Embedding: the page is cross-origin isolated (COEP require-corp), so a
// cross-origin iframe loads only if its document sends COEP and
// `Cross-Origin-Resource-Policy: cross-origin`, which the site does
// (docs/.config/docs.yaml crossOriginIsolation), or the iframe is
// credentialless (Chromium only). The iframe is credentialless too, so
// Chromium shows the site even from a deploy without those headers.
// - The site is another origin: the window cannot read where its page is,
// so Home goes back to DOCS_URL and the new tab opens there.
// - Theme: the iframe's URL carries an undocs theme fragment (`#~` and
// base64url JSON of design tokens, https://undocs.unjs.io/guide/embedding),
// THEME: the page's colours (style.css), the colour mode pinned to the
// page's. A theme change posts the other THEME to the site (undocs'
// `undocs:embed-theme` message), so the page shown stays. The new tab gets
// the site as it is.
import { createElement } from "react";
import type { AppMeta } from "./apps/types.ts";
import { Docs } from "./components/Docs.tsx";
import { DocsIcon } from "./components/icons.tsx";
import { hideDisplay } from "./display.ts";
import { createStore } from "./store.ts";
import { wm, wm$ } from "./wm.ts";
/** The documentation site. */
export const DOCS_URL = "https://docs.unkernel.dev/";
/**
* The page's look as undocs tokens (style.css: bg, fg, hover, muted, line,
* accent); the colour mode pinned to the page's (`m`).
*/
const THEME = {
dark: { m: "d", bg: "#000000", fg: "#ededed", c: "rgb(255 255 255 / 4%)", mu: "#1a1a1a", muf: "#a1a1a1", bd: "#242424", p: "#52a8ff", pf: "#000000", br: "#52a8ff", r: "0.5rem" },
light: { m: "l", bg: "#ffffff", fg: "#171717", c: "rgb(0 0 0 / 4%)", mu: "#f2f2f2", muf: "#525252", bd: "#ebebeb", p: "#0068d6", pf: "#ffffff", br: "#0068d6", r: "0.5rem" },
};
const payload = (theme: object) => btoa(JSON.stringify(theme)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
/** Each THEME as undocs' base64url payload: the fragment after `#~`, and the message's `theme`. */
export const DOCS_THEME = { dark: payload(THEME.dark), light: payload(THEME.light) };
/** DOCS_URL with a THEME as its fragment, one per page theme: what the iframe loads. */
export const DOCS_EMBED_URL = { dark: `${DOCS_URL}#~${DOCS_THEME.dark}`, light: `${DOCS_URL}#~${DOCS_THEME.light}` };
/** The window's WM id (its body's root is #docs). */
export const DOCS_WIN = "docs";
/** Its Dock tile's element id while it is open. */
const DOCS_DOCK = "dock-docs";
/** The window's descriptor (wm.ts meta), and the Docs page app's (apps/system/docs.ts). */
export const DOCS: AppMeta = { id: "docs", title: "Docs", glyph: DocsIcon };
/** Bumped by Home: the iframe is made again (its key) on DOCS_URL. */
export const docsNav$ = createStore<{ nav: number }>({ nav: 0 });
/** Its first geometry: centred, most of the workspace; the WM places it where the workspace is not measured yet. */
function firstGeom(): { w: number; h: number; x?: number; y?: number } {
const { area } = wm$.get();
if (!area.w || !area.h) return { w: 1280, h: 820 };
const w = Math.round(Math.min(1440, area.w - 32, Math.max(560, area.w * 0.8)));
const h = Math.round(Math.min(area.h - 16, Math.max(400, area.h * 0.94)));
return { w, h, x: Math.max(0, Math.round((area.w - w) / 2)), y: Math.max(0, Math.round((area.h - h) / 2)) };
}
/** Open the Docs window, or show and focus it where it is (the guest's Display window goes under it, as for the Browser). */
export function openDocs(): void {
hideDisplay();
if (wm.get(DOCS_WIN)) { wm.focus(DOCS_WIN); return; }
wm.open({
id: DOCS_WIN, meta: DOCS, dockId: DOCS_DOCK, minW: 320, minH: 220, ...firstGeom(),
content: createElement(Docs, { id: DOCS_WIN }),
});
}
/** Back to the site's first page. */
export const docsHome = (): void => docsNav$.set((s) => ({ nav: s.nav + 1 }));