From a232cf15e410116fa3571921faa97e077a98dca6 Mon Sep 17 00:00:00 2001 From: CRT Date: Mon, 28 Sep 2026 14:47:09 +0000 Subject: [PATCH] runtime: plugin UI kit (api.ui: toast, alert, confirm, prompt, dialog, modal) and theme API (api.theme, --wb-* vars); theme JSON Schema; waybetter 0.2.0 The kit draws in one shadow DOM host in the active theme's colors, corners and font and repaints live; accessible dialogs (focus trap and return, Esc/Enter rules, alertdialog for alerts and confirms), stacking with inert, toasts with actions, reduced motion, text-only content (ui.h), and scope.ui closes a plugin's dialogs on dispose. api.toast routes to it. --- README.md | 4 +- packages/runtime/src/pluginApi.ts | 75 ++- packages/runtime/src/themeApi.ts | 226 +++++++ packages/runtime/src/ui.ts | 908 +++++++++++++++++++++++++++ packages/runtime/tests/ui.test.ts | 200 ++++++ packages/waybetter/README.md | 66 +- packages/waybetter/package.json | 17 +- packages/waybetter/theme.schema.json | 156 +++++ 8 files changed, 1627 insertions(+), 25 deletions(-) create mode 100644 packages/runtime/src/themeApi.ts create mode 100644 packages/runtime/src/ui.ts create mode 100644 packages/runtime/tests/ui.test.ts create mode 100644 packages/waybetter/theme.schema.json diff --git a/README.md b/README.md index 7dcf9ea..3d3ed3b 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,9 @@ crates/waybetter-host/ Rust: security policy, profiles, plugin store, hotfi packages/runtime/src/ page runtime core (TypeScript, no dependencies) site.ts SiteAdapter (the page half of the adapter) + configureRuntime domHub.ts netHub.ts one MutationObserver/frame, one fetch/XHR hook per page - pluginApi.ts core of window..api for plugins + pluginApi.ts core of window..api for plugins (incl. ui and theme) + ui.ts plugin UI kit: toasts, alert/confirm/prompt, dialogs (shadow DOM, themed) + themeApi.ts the active theme as values + --wb-* CSS variables (api.theme) themeTokens.ts OKLab color model: maps the site's palette onto a theme's axis paneNav.ts split view: park the site's navigation rail in redundant panes linkRouting.ts divert in-page clicks to a pane/tab/browser per setting diff --git a/packages/runtime/src/pluginApi.ts b/packages/runtime/src/pluginApi.ts index 388f1ad..b722f36 100644 --- a/packages/runtime/src/pluginApi.ts +++ b/packages/runtime/src/pluginApi.ts @@ -2,6 +2,14 @@ // re-implement DOM watching, route tracking, local storage and cleanup. Everything here is local to // the page: no network requests, no account actions. A site adapter adds its own readers (e.g. a // post/message parser) on top of this core. +// +// `ui` (toasts, alerts, confirmations, prompts, dialogs; ui.ts) and `theme` (the active theme as +// values; themeApi.ts) are part of the core: the site adapter only supplies its theme source. + +import { createThemeApi, type ThemeApi, type ThemeSource } from './themeApi.ts'; +import { createUiKit, type UiKit } from './ui.ts'; + +export type { ThemeApi, ThemeSource, UiKit }; export type Dispose = () => void; @@ -24,6 +32,10 @@ export type PluginScope = { add(node: T): T; /** Runs `fn` on dispose (restore attributes, clear timers, ...). */ defer(fn: Dispose): void; + /** Calls `callback(theme)` after the theme changes, until dispose. */ + onThemeChange(callback: Parameters[0]): void; + /** The UI kit, scoped: dialogs and toasts it opened are closed on dispose. */ + readonly ui: UiKit; dispose(): void; }; @@ -43,10 +55,20 @@ export type CorePluginApi = { */ onKey(callback: (event: KeyboardEvent, item: Element | null) => void): Dispose; copyText(text: string): Promise; + /** A short status message (the UI kit's toast; `ms` is how long it stays). */ toast(message: string, ms?: number): void; + /** Toasts, alerts, confirmations, prompts and dialogs in the active theme (see ui.ts). */ + ui: UiKit; + /** The active theme as values: `current()` and `onChange(fn)` (see themeApi.ts). */ + theme: ThemeApi; scope(): PluginScope; }; +/** What the site adapter adds to the core: its theme engine, when it has one. */ +export type CorePluginExtras = { + theme?: ThemeSource; +}; + export type PluginApiSite = { /** The runtime global, for error messages (`.waitFor timed out`). */ name: string; @@ -56,7 +78,9 @@ export type PluginApiSite = { storagePrefix: string; /** `data-*` key (camelCase) marking plugin `