chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
// The theme the page is drawn in (place.hud.defs#theme), as the person
// chose it, kept on the server (place.hud.setTheme) so their other pages
// and apps draw alike: one of HUD's, by its key, or a person's
// place.hud.theme, by its uri. Every colour of tokens.css is a
// `light-dark()`, so light and dark set only the root's colour scheme; a
// palette writes its colours on the root too, as the tokens they are, and
// marks it `data-palette`, from which tokens.css mixes the rest. None
// leaves the root to the system's light or dark. The theme editor draws
// itself in the palette it makes the same way (`styleOf`).
//
// The browser keeps what it drew last (`localStorage`, `hud-theme`), which
// workspace.html's own script draws before the page is, so it opens in the
// theme rather than in the system's first.
import type * as HudDefs from './lexicons/place/hud/defs.defs.ts';
export type Theme = HudDefs.Theme;
export type Palette = Omit<HudDefs.Palette, '$type'>;
export type Scheme = 'light' | 'dark';
/** What names a theme: its key, one of HUD's; else its uri, a person's. */
export const themeId = (theme: Theme) => theme.key ?? theme.uri ?? '';
/** A palette's colours as the editor groups and names them, by what each colours. */
export const COLOURS: { group: string; colours: { field: keyof Palette; name: string }[] }[] = [
{
group: 'Surfaces',
colours: [
{ field: 'canvas', name: 'Canvas, behind the tiles' },
{ field: 'background', name: 'Tiles and the bar' },
{ field: 'panel', name: 'Headers and menus' },
{ field: 'soft', name: 'Fields and hovered rows' },
{ field: 'border', name: 'Lines' },
{ field: 'borderStrong', name: 'Strong lines' }
]
},
{
group: 'Text',
colours: [
{ field: 'text', name: 'Text' },
{ field: 'textMuted', name: 'Muted text' },
{ field: 'textFaint', name: 'Faint text' }
]
},
{
group: 'Actions',
colours: [
{ field: 'primary', name: 'Main buttons' },
{ field: 'onPrimary', name: 'Text on main buttons' },
{ field: 'accent', name: 'Links and the tab you’re on' },
{ field: 'accentSoft', name: 'Behind a picked row' }
]
},
{
group: 'States',
colours: [
{ field: 'success', name: 'Open and passed' },
{ field: 'danger', name: 'Failed and removed' },
{ field: 'done', name: 'Merged' },
{ field: 'attention', name: 'Waiting and running' },
{ field: 'neutral', name: 'Closed and skipped' },
{ field: 'onSolid', name: 'Text on a state’s pill' }
]
},
{
group: 'Notices',
colours: [
{ field: 'notice', name: 'Notices' },
{ field: 'onNotice', name: 'Text on notices' }
]
}
];
/** What each colour is called, for saying which reads too faintly on which. */
export const colourName = (field: string) => COLOURS.flatMap((g) => g.colours).find((c) => c.field === field)?.name.toLowerCase() ?? field;
/** What the browser keeps: the root's style, and whether it is a palette's. */
type Kept = { style: string; palette: boolean };
const KEPT = 'hud-theme';
/** Each of a palette's colours, by the token of tokens.css it is drawn as. */
const TOKENS: Record<keyof Palette, string> = {
canvas: 'canvas',
background: 'bg',
panel: 'panel',
soft: 'soft',
border: 'line',
borderStrong: 'line-strong',
text: 'fg',
textMuted: 'mute',
textFaint: 'faint',
primary: 'primary',
onPrimary: 'on-primary',
accent: 'accent',
accentSoft: 'accent-soft',
success: 'success',
danger: 'danger',
done: 'done',
attention: 'attention',
neutral: 'neutral',
onSolid: 'on-solid',
notice: 'notice',
onNotice: 'notice-fg'
};
const HEX = /^#[0-9a-f]{6}$/i;
/** `theme`'s scheme and palette as an element's style: its colours, as the tokens they are, that are #rrggbb. */
export function styleOf(theme: Pick<Theme, 'colorScheme' | 'palette'>): string {
const scheme = theme.colorScheme === 'dark' ? 'dark' : 'light';
const palette = theme.palette ?? {};
const colours = (Object.keys(TOKENS) as (keyof Palette)[]).flatMap((field) => {
const value = (palette as Partial<Palette>)[field];
return typeof value === 'string' && HEX.test(value) ? [`--${TOKENS[field]}:${value}`] : [];
});
return [`color-scheme:${scheme}`, ...colours].join(';');
}
/** Draw the root as `kept` says, or as the system's for none. */
function drawn(kept: Kept | null) {
const html = document.documentElement;
if (kept?.style) html.setAttribute('style', kept.style);
else html.removeAttribute('style');
html.toggleAttribute('data-palette', kept?.palette ?? false);
}
/** Tell `seen` the scheme the page is drawn in, its theme's or else the system's, now and whenever the system's changes; what it returns stops it. */
export function watchScheme(seen: (scheme: Scheme) => void): () => void {
const system = matchMedia('(prefers-color-scheme: dark)');
const tell = () => {
const chosen = document.documentElement.style.colorScheme;
seen(chosen === 'light' || chosen === 'dark' ? chosen : system.matches ? 'dark' : 'light');
};
tell();
system.addEventListener('change', tell);
return () => system.removeEventListener('change', tell);
}
/** Draw the page in `theme`, or the system's light or dark for none; and keep it for the next page. */
export function draw(theme: Theme | null) {
const kept = theme ? { style: styleOf(theme), palette: theme.palette !== undefined } : null;
drawn(kept);
try {
if (kept) localStorage.setItem(KEPT, JSON.stringify(kept));
else localStorage.removeItem(KEPT);
} catch {
// Kept or not, the page is drawn; the next one may flash the system's first.
}
}
/** The colours a palette's swatch shows, as its style: its canvas, a tile on it inside its strong border, its text and its accent. */
export function swatch(palette: Palette): string {
const shown = { canvas: palette.canvas, tile: palette.background, edge: palette.borderStrong, text: palette.text, accent: palette.accent };
return Object.entries(shown)
.filter(([, value]) => HEX.test(value))
.map(([part, value]) => `--swatch-${part}:${value}`)
.join(';');
}