~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
99 lines · 4.0 KB Raw History
// An app's look, as its <domain>.json gives it: a palette for light and
// one for dark, each in place.hud.defs#palette's fields as HUD's themes
// (themes/*.json) name them, and its fonts. Each palette colour is written
// on the root, in the page's <head> as the server sends it, as the
// renderer's variable it is drawn as (the MCP Apps host variables of
// @hud/renderer/theme.css, mapped as web/workspace/app.css maps HUD's
// tokens to them), a `light-dark()` of the two, so the page follows the
// system's scheme as the app does; signed in, the account's own choice of
// scheme (`Scheme`, Grain's theme preference), where it makes one, forces
// light or dark on the root's `color-scheme`. The app's server keeps that
// choice (Grain's in a cookie) as it reads it (`frameData`), so a page is
// drawn in it from its first paint.

import type { Call } from '@hud/shared/result';
import { at } from './app';

/** The colours of one scheme, as HUD's palettes name them. */
export type Palette = {
	canvas: string;
	background: string;
	panel: string;
	soft: string;
	border: string;
	borderStrong: string;
	text: string;
	textMuted: string;
	textFaint: string;
	primary: string;
	onPrimary: string;
	accent: string;
	accentSoft: string;
	danger: string;
	notice: string;
	onNotice: string;
};

export type Look = {
	light: Palette;
	dark: Palette;
	/** A stylesheet that loads them, the body's font and the display font titles take. */
	fonts?: { href: string; sans: string; display: string };
};

/** Each renderer variable, by the palette colour it is drawn in. */
const VARIABLES: Record<string, keyof Palette> = {
	'--color-background-primary': 'background',
	'--color-background-secondary': 'soft',
	'--color-background-tertiary': 'border',
	'--color-background-inverse': 'text',
	'--color-background-info': 'accentSoft',
	'--color-background-danger': 'soft',
	'--color-background-warning': 'notice',
	'--color-text-primary': 'text',
	'--color-text-secondary': 'textMuted',
	'--color-text-tertiary': 'textFaint',
	'--color-text-inverse': 'background',
	'--color-text-info': 'accent',
	'--color-text-danger': 'danger',
	'--color-text-warning': 'onNotice',
	'--color-border-primary': 'borderStrong',
	'--color-border-secondary': 'border',
	'--color-border-danger': 'danger',
	'--color-ring-primary': 'primary',
	'--color-background-action': 'primary',
	'--color-text-on-action': 'onPrimary',
	'--app-canvas': 'canvas',
	'--app-panel': 'panel',
	'--app-primary': 'primary',
	'--app-on-primary': 'onPrimary',
	'--app-accent': 'accent',
	'--app-accent-soft': 'accentSoft'
};

/** The stylesheet that draws the page in `look`: its variables on the root. */
export function lookCss(look: Look): string {
	const values = Object.entries(VARIABLES).map(([variable, field]) => `${variable}:light-dark(${look.light[field]},${look.dark[field]})`);
	if (look.fonts) values.push(`--font-sans:${look.fonts.sans}`, `--app-font-display:${look.fonts.display}`);
	// The app's own file, but a stylesheet all the same: nothing in it ends the <style>.
	return `html:root{${values.join(';')}}`.replaceAll('<', '');
}

/** Where the account's own choice of scheme is, signed in: a call (`call`),
 * the path to it in its answer (`at`, a path as app.ts's `at` reads one),
 * and the procedure after which it is asked again (`after`). */
export type Scheme = { call: Call; at: string; after?: string };

/** The scheme an answer chooses at `path`: light or dark; anything else, none (the system's). */
export function schemeIn(answer: unknown, path: string): 'light' | 'dark' | null {
	const found = at(answer, path);
	return found === 'light' || found === 'dark' ? found : null;
}

/** Draw the page in `scheme`'s palette, or with none, the system's. */
export function applyScheme(scheme: 'light' | 'dark' | null) {
	document.documentElement.style.colorScheme = scheme ?? '';
}

/** What the scheme cookie holds, as a scheme. */
export const schemeOf = (value: string | undefined): 'light' | 'dark' | null => (value === 'light' || value === 'dark' ? value : null);