runtime: font roles in the theme API (fonts per role, --wb-font-<role> vars, UI kit uses the dialog/mono roles); theme schema: font.roles with Google Fonts source

This commit is contained in:
CRTkafa 2026-09-28 23:56:37 +07:00
parent ae64714913
commit e07eee0041
3 changed files with 114 additions and 12 deletions

View file

@ -44,8 +44,37 @@ export type ResolvedTheme = {
buttonRadius: number; buttonRadius: number;
/** CSS `font-family` stack of the interface. */ /** CSS `font-family` stack of the interface. */
font: string; font: string;
/**
* Which font goes where: every role resolved (a role that inherits carries the stack it inherits).
* Always present after `normalizeResolved`; adapters without font roles may leave it out.
*/
fonts?: ResolvedFonts;
}; };
/**
* Font roles: `interface` (the site's UI), `post` (main content text), `heading` (names and
* headings), `numbers` (counts), `mono` (code), `chrome` (the app's own window: tabs, address bar,
* panel) and `dialog` (plugin dialogs and toasts from the UI kit).
*/
export const FONT_ROLE_NAMES = ['interface', 'post', 'heading', 'numbers', 'mono', 'chrome', 'dialog'] as const;
export type FontRoleName = typeof FONT_ROLE_NAMES[number];
export type ResolvedFont = {
/** CSS `font-family` stack. */
stack: string;
/** Weight the role asks for, or null to keep the text's own. */
weight: number | null;
/** Size relative to the site's (1 = unchanged). */
size: number;
/** Letter spacing in em, or null to keep the text's own. */
tracking: number | null;
/** Tabular (fixed-width) figures. */
tabular: boolean;
/** True when the role inherits its family from another role or the site. */
inherited: boolean;
};
export type ResolvedFonts = Record<FontRoleName, ResolvedFont>;
export const MONO_FONT = 'ui-monospace, "Cascadia Mono", "SF Mono", Consolas, Menlo, monospace';
/** What a site adapter provides: the current resolved theme and change notifications. */ /** What a site adapter provides: the current resolved theme and change notifications. */
export type ThemeSource = { export type ThemeSource = {
current(): ResolvedTheme | null; current(): ResolvedTheme | null;
@ -161,13 +190,38 @@ export function normalizeResolved(theme: ResolvedTheme): ResolvedTheme {
if (!HEX.test(theme.colors.onAccent ?? '')) colors.onAccent = readableOn(colors.accent); if (!HEX.test(theme.colors.onAccent ?? '')) colors.onAccent = readableOn(colors.accent);
const radius = Number.isFinite(theme.radius) ? Math.max(0, Math.min(28, theme.radius)) : 16; const radius = Number.isFinite(theme.radius) ? Math.max(0, Math.min(28, theme.radius)) : 16;
const buttonRadius = Number.isFinite(theme.buttonRadius) ? Math.max(0, theme.buttonRadius) : 9999; const buttonRadius = Number.isFinite(theme.buttonRadius) ? Math.max(0, theme.buttonRadius) : 9999;
const font = theme.font || FALLBACK_FONT;
return Object.freeze({ return Object.freeze({
id: String(theme.id || 'theme'), name: String(theme.name || ''), mode: theme.mode === 'light' ? 'light' : 'dark', id: String(theme.id || 'theme'), name: String(theme.name || ''), mode: theme.mode === 'light' ? 'light' : 'dark',
siteDefault: !!theme.siteDefault, radius, buttonRadius, font: theme.font || FALLBACK_FONT, siteDefault: !!theme.siteDefault, radius, buttonRadius, font,
colors: Object.freeze(colors), colors: Object.freeze(colors),
fonts: normalizeFonts(theme.fonts, font),
}); });
} }
const cssSafe = (value: string) => value.replace(/[;{}<>\\]/g, '');
const finite = (value: unknown, min: number, max: number): number | null =>
typeof value === 'number' && Number.isFinite(value) ? Math.min(max, Math.max(min, value)) : null;
/** Every role filled in: missing roles inherit the interface font (`mono` a monospaced stack). */
export function normalizeFonts(fonts: Partial<Record<FontRoleName, Partial<ResolvedFont>>> | undefined, font: string): ResolvedFonts {
const out = {} as Record<FontRoleName, ResolvedFont>;
const base = fonts?.interface?.stack ? cssSafe(fonts.interface.stack) : font;
for (const role of FONT_ROLE_NAMES) {
const given = fonts?.[role];
const stack = given?.stack ? cssSafe(given.stack) : role === 'mono' ? MONO_FONT : base;
out[role] = Object.freeze({
stack,
weight: finite(given?.weight, 100, 900),
size: finite(given?.size, 0.5, 2) ?? 1,
tracking: finite(given?.tracking, -0.1, 0.2),
tabular: !!given?.tabular,
inherited: given ? !!given.inherited : true,
});
}
return Object.freeze(out);
}
const VAR_NAMES: Record<keyof ResolvedColors, string> = { const VAR_NAMES: Record<keyof ResolvedColors, string> = {
bg: '--wb-bg', surface: '--wb-surface', elevated: '--wb-elevated', border: '--wb-border', text: '--wb-text', muted: '--wb-muted', bg: '--wb-bg', surface: '--wb-surface', elevated: '--wb-elevated', border: '--wb-border', text: '--wb-text', muted: '--wb-muted',
accent: '--wb-accent', onAccent: '--wb-on-accent', inverse: '--wb-inverse', onInverse: '--wb-on-inverse', page: '--wb-page', accent: '--wb-accent', onAccent: '--wb-on-accent', inverse: '--wb-inverse', onInverse: '--wb-on-inverse', page: '--wb-page',
@ -176,7 +230,9 @@ const VAR_NAMES: Record<keyof ResolvedColors, string> = {
/** /**
* The theme as CSS custom properties: `--wb-bg`, `--wb-surface`, `--wb-elevated`, `--wb-border`, * The theme as CSS custom properties: `--wb-bg`, `--wb-surface`, `--wb-elevated`, `--wb-border`,
* `--wb-text`, `--wb-muted`, `--wb-accent`, `--wb-on-accent`, `--wb-inverse`, `--wb-on-inverse`, * `--wb-text`, `--wb-muted`, `--wb-accent`, `--wb-on-accent`, `--wb-inverse`, `--wb-on-inverse`,
* `--wb-page`, `--wb-radius`, `--wb-button-radius`, `--wb-font`, `--wb-mode` (`light`/`dark`). * `--wb-page`, `--wb-radius`, `--wb-button-radius`, `--wb-font`, `--wb-mode` (`light`/`dark`), and
* one stack per font role: `--wb-font-interface`, `--wb-font-post`, `--wb-font-heading`,
* `--wb-font-numbers`, `--wb-font-mono`, `--wb-font-chrome`, `--wb-font-dialog`.
*/ */
export function themeVars(theme: ResolvedTheme): Record<string, string> { export function themeVars(theme: ResolvedTheme): Record<string, string> {
const vars: Record<string, string> = {}; const vars: Record<string, string> = {};
@ -184,6 +240,8 @@ export function themeVars(theme: ResolvedTheme): Record<string, string> {
vars['--wb-radius'] = `${theme.radius}px`; vars['--wb-radius'] = `${theme.radius}px`;
vars['--wb-button-radius'] = `${theme.buttonRadius}px`; vars['--wb-button-radius'] = `${theme.buttonRadius}px`;
vars['--wb-font'] = theme.font; vars['--wb-font'] = theme.font;
const fonts = theme.fonts ?? normalizeFonts(undefined, theme.font);
for (const role of FONT_ROLE_NAMES) vars[`--wb-font-${role}`] = fonts[role].stack;
vars['--wb-mode'] = theme.mode; vars['--wb-mode'] = theme.mode;
return vars; return vars;
} }

View file

@ -229,19 +229,19 @@ const TOAST_TONES: Record<Exclude<Tone, 'neutral' | 'info'>, [string, string]> =
export const UI_CSS = ` export const UI_CSS = `
:host { all: initial; } :host { all: initial; }
.wb-root { font-family: var(--wb-font); color: var(--wb-text); -webkit-font-smoothing: antialiased; } .wb-root { font-family: var(--wb-font-dialog, var(--wb-font)); color: var(--wb-text); -webkit-font-smoothing: antialiased; }
.wb-root *, .wb-root *::before, .wb-root *::after { box-sizing: border-box; } .wb-root *, .wb-root *::before, .wb-root *::after { box-sizing: border-box; }
.wb-layer { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; pointer-events: auto; overscroll-behavior: contain; } .wb-layer { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; pointer-events: auto; overscroll-behavior: contain; }
.wb-backdrop { position: absolute; inset: 0; background: var(--wb-scrim); animation: wb-fade .16s ease-out; } .wb-backdrop { position: absolute; inset: 0; background: var(--wb-scrim); animation: wb-fade .16s ease-out; }
.wb-dialog { position: relative; width: min(var(--wb-dialog-width, 480px), 100%); max-height: calc(100vh - 32px); overflow: auto; overscroll-behavior: contain; .wb-dialog { position: relative; width: min(var(--wb-dialog-width, 480px), 100%); max-height: calc(100vh - 32px); overflow: auto; overscroll-behavior: contain;
display: flex; flex-direction: column; gap: 0; padding: 20px 24px 22px; background: var(--wb-bg); color: var(--wb-text); display: flex; flex-direction: column; gap: 0; padding: 20px 24px 22px; background: var(--wb-bg); color: var(--wb-text);
border: 1px solid var(--wb-border); border-radius: var(--wb-radius); box-shadow: 0 24px 64px -12px rgba(0,0,0,.45), 0 2px 8px rgba(0,0,0,.12); border: 1px solid var(--wb-border); border-radius: var(--wb-radius); box-shadow: 0 24px 64px -12px rgba(0,0,0,.45), 0 2px 8px rgba(0,0,0,.12);
font: 400 15px/1.45 var(--wb-font); outline: none; animation: wb-in .18s cubic-bezier(.2,.9,.3,1.2); } font: 400 15px/1.45 var(--wb-font-dialog, var(--wb-font)); outline: none; animation: wb-in .18s cubic-bezier(.2,.9,.3,1.2); }
.wb-layer.wb-closing .wb-dialog { animation: wb-out .12s ease-in forwards; } .wb-layer.wb-closing .wb-dialog { animation: wb-out .12s ease-in forwards; }
.wb-layer.wb-closing .wb-backdrop { animation: wb-fade-out .12s ease-in forwards; } .wb-layer.wb-closing .wb-backdrop { animation: wb-fade-out .12s ease-in forwards; }
.wb-layer[inert] .wb-dialog { filter: saturate(.9); } .wb-layer[inert] .wb-dialog { filter: saturate(.9); }
.wb-head { display: flex; align-items: flex-start; gap: 10px; margin: 0 0 4px; } .wb-head { display: flex; align-items: flex-start; gap: 10px; margin: 0 0 4px; }
.wb-title { flex: 1; margin: 0; font: 800 20px/1.25 var(--wb-font); letter-spacing: -.01em; color: var(--wb-text); overflow-wrap: anywhere; } .wb-title { flex: 1; margin: 0; font: 800 20px/1.25 var(--wb-font-dialog, var(--wb-font)); letter-spacing: -.01em; color: var(--wb-text); overflow-wrap: anywhere; }
.wb-icon { flex: none; width: 24px; height: 24px; margin-top: 1px; color: var(--wb-tone); } .wb-icon { flex: none; width: 24px; height: 24px; margin-top: 1px; color: var(--wb-tone); }
.wb-icon svg { display: block; width: 24px; height: 24px; } .wb-icon svg { display: block; width: 24px; height: 24px; }
.wb-close { flex: none; display: grid; place-items: center; width: 32px; height: 32px; margin: -6px -10px 0 0; padding: 0; border: 0; border-radius: 999px; .wb-close { flex: none; display: grid; place-items: center; width: 32px; height: 32px; margin: -6px -10px 0 0; padding: 0; border: 0; border-radius: 999px;
@ -258,7 +258,7 @@ export const UI_CSS = `
.wb-actions.wb-stack { flex-direction: column; align-items: stretch; gap: 12px; margin-top: 24px; } .wb-actions.wb-stack { flex-direction: column; align-items: stretch; gap: 12px; margin-top: 24px; }
.wb-actions.wb-stack .wb-btn { height: 44px; font-size: 16px; } .wb-actions.wb-stack .wb-btn { height: 44px; font-size: 16px; }
.wb-btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-width: 72px; height: 36px; padding: 0 18px; .wb-btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-width: 72px; height: 36px; padding: 0 18px;
border: 1px solid transparent; border-radius: var(--wb-button-radius); font: 700 15px/1 var(--wb-font); cursor: pointer; white-space: nowrap; border: 1px solid transparent; border-radius: var(--wb-button-radius); font: 700 15px/1 var(--wb-font-dialog, var(--wb-font)); cursor: pointer; white-space: nowrap;
transition: background-color .12s, filter .12s, opacity .12s; } transition: background-color .12s, filter .12s, opacity .12s; }
.wb-btn:disabled, .wb-btn[aria-disabled="true"] { opacity: .5; cursor: default; } .wb-btn:disabled, .wb-btn[aria-disabled="true"] { opacity: .5; cursor: default; }
.wb-btn:focus-visible, .wb-close:focus-visible, .wb-toast button:focus-visible { outline: 2px solid var(--wb-accent); outline-offset: 2px; } .wb-btn:focus-visible, .wb-close:focus-visible, .wb-toast button:focus-visible { outline: 2px solid var(--wb-accent); outline-offset: 2px; }
@ -278,9 +278,9 @@ export const UI_CSS = `
.wb-hint { margin: 0; font-size: 13px; color: var(--wb-muted); } .wb-hint { margin: 0; font-size: 13px; color: var(--wb-muted); }
.wb-input, .wb-dialog input[type="text"], .wb-dialog input[type="search"], .wb-dialog input[type="url"], .wb-dialog input[type="number"], .wb-dialog textarea, .wb-dialog select { .wb-input, .wb-dialog input[type="text"], .wb-dialog input[type="search"], .wb-dialog input[type="url"], .wb-dialog input[type="number"], .wb-dialog textarea, .wb-dialog select {
width: 100%; min-height: 40px; padding: 9px 12px; border: 1px solid var(--wb-border-strong); border-radius: var(--wb-field-radius); width: 100%; min-height: 40px; padding: 9px 12px; border: 1px solid var(--wb-border-strong); border-radius: var(--wb-field-radius);
background: var(--wb-bg); color: var(--wb-text); font: 400 15px/1.4 var(--wb-font); outline: none; transition: border-color .12s, box-shadow .12s; } background: var(--wb-bg); color: var(--wb-text); font: 400 15px/1.4 var(--wb-font-dialog, var(--wb-font)); outline: none; transition: border-color .12s, box-shadow .12s; }
.wb-dialog textarea { min-height: 96px; resize: vertical; } .wb-dialog textarea { min-height: 96px; resize: vertical; }
.wb-dialog textarea.wb-mono { font: 400 13px/1.5 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; } .wb-dialog textarea.wb-mono { font: 400 13px/1.5 var(--wb-font-mono, ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace); }
.wb-input::placeholder, .wb-dialog input::placeholder, .wb-dialog textarea::placeholder { color: var(--wb-muted); opacity: .8; } .wb-input::placeholder, .wb-dialog input::placeholder, .wb-dialog textarea::placeholder { color: var(--wb-muted); opacity: .8; }
.wb-input:focus, .wb-dialog input:focus, .wb-dialog textarea:focus, .wb-dialog select:focus { border-color: var(--wb-accent); box-shadow: 0 0 0 1px var(--wb-accent); } .wb-input:focus, .wb-dialog input:focus, .wb-dialog textarea:focus, .wb-dialog select:focus { border-color: var(--wb-accent); box-shadow: 0 0 0 1px var(--wb-accent); }
.wb-dialog [aria-invalid="true"] { border-color: var(--wb-danger) !important; box-shadow: 0 0 0 1px var(--wb-danger) !important; } .wb-dialog [aria-invalid="true"] { border-color: var(--wb-danger) !important; box-shadow: 0 0 0 1px var(--wb-danger) !important; }
@ -296,14 +296,14 @@ export const UI_CSS = `
width: max-content; max-width: min(560px, calc(100vw - 32px)); pointer-events: none; } width: max-content; max-width: min(560px, calc(100vw - 32px)); pointer-events: none; }
.wb-toast { display: flex; align-items: center; gap: 12px; min-height: 44px; max-width: 100%; padding: 10px 10px 10px 16px; border-radius: min(var(--wb-radius), 12px); .wb-toast { display: flex; align-items: center; gap: 12px; min-height: 44px; max-width: 100%; padding: 10px 10px 10px 16px; border-radius: min(var(--wb-radius), 12px);
background: var(--wb-toast-bg); color: var(--wb-toast-fg); box-shadow: 0 10px 30px -6px rgba(0,0,0,.4); pointer-events: auto; background: var(--wb-toast-bg); color: var(--wb-toast-fg); box-shadow: 0 10px 30px -6px rgba(0,0,0,.4); pointer-events: auto;
font: 500 15px/1.35 var(--wb-font); animation: wb-toast-in .2s cubic-bezier(.2,.9,.3,1.2); } font: 500 15px/1.35 var(--wb-font-dialog, var(--wb-font)); animation: wb-toast-in .2s cubic-bezier(.2,.9,.3,1.2); }
.wb-toast.wb-closing { animation: wb-toast-out .14s ease-in forwards; } .wb-toast.wb-closing { animation: wb-toast-out .14s ease-in forwards; }
${Object.entries(TOAST_TONES).map(([tone, [bg, fg]]) => `.wb-toast[data-tone="${tone}"] { --wb-toast-bg: ${bg}; --wb-toast-fg: ${fg}; }`).join(' ')} ${Object.entries(TOAST_TONES).map(([tone, [bg, fg]]) => `.wb-toast[data-tone="${tone}"] { --wb-toast-bg: ${bg}; --wb-toast-fg: ${fg}; }`).join(' ')}
.wb-toast-icon { flex: none; display: grid; margin-left: -4px; } .wb-toast-icon { flex: none; display: grid; margin-left: -4px; }
.wb-toast-icon svg { width: 20px; height: 20px; } .wb-toast-icon svg { width: 20px; height: 20px; }
.wb-toast[data-tone="info"] .wb-toast-icon { color: var(--wb-accent); } .wb-toast[data-tone="info"] .wb-toast-icon { color: var(--wb-accent); }
.wb-toast-text { flex: 1; min-width: 0; overflow-wrap: anywhere; white-space: pre-line; padding-right: 4px; } .wb-toast-text { flex: 1; min-width: 0; overflow-wrap: anywhere; white-space: pre-line; padding-right: 4px; }
.wb-toast button { flex: none; border: 0; cursor: pointer; color: inherit; font: 700 14px/1 var(--wb-font); } .wb-toast button { flex: none; border: 0; cursor: pointer; color: inherit; font: 700 14px/1 var(--wb-font-dialog, var(--wb-font)); }
.wb-toast-action { height: 30px; padding: 0 12px; border-radius: var(--wb-button-radius); background: color-mix(in srgb, currentColor 16%, transparent); } .wb-toast-action { height: 30px; padding: 0 12px; border-radius: var(--wb-button-radius); background: color-mix(in srgb, currentColor 16%, transparent); }
.wb-toast-action:hover { background: color-mix(in srgb, currentColor 26%, transparent); } .wb-toast-action:hover { background: color-mix(in srgb, currentColor 26%, transparent); }
.wb-toast-close { display: grid; place-items: center; width: 28px; height: 28px; padding: 0; border-radius: 999px; background: transparent; opacity: .8; } .wb-toast-close { display: grid; place-items: center; width: 28px; height: 28px; padding: 0; border-radius: 999px; background: transparent; opacity: .8; }

View file

@ -41,8 +41,22 @@
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"family": { "$ref": "#/$defs/fontStack", "description": "Interface font stack; empty keeps the site's font." }, "family": { "$ref": "#/$defs/fontStack", "description": "Interface font stack; empty keeps the site's font. Shorthand of roles.interface.family (kept for older apps); when roles.interface is present it wins." },
"tweet": { "$ref": "#/$defs/fontStack", "description": "Optional stack for post text only." }, "tweet": { "$ref": "#/$defs/fontStack", "description": "Post text stack; shorthand of roles.post.family (roles.post wins)." },
"roles": {
"type": "object",
"additionalProperties": false,
"description": "Which font goes where. A role is \"inherit\" (the interface font; an inheriting mono role keeps a monospaced face) or its own stack with optional settings.",
"properties": {
"interface": { "$ref": "#/$defs/interfaceRole", "description": "The site's whole UI; every other role inherits it." },
"post": { "$ref": "#/$defs/fontRole", "description": "Post text." },
"heading": { "$ref": "#/$defs/fontRole", "description": "Display names and headings." },
"numbers": { "$ref": "#/$defs/fontRole", "description": "Counts (likes, reposts, followers)." },
"mono": { "$ref": "#/$defs/fontRole", "description": "Code." },
"chrome": { "$ref": "#/$defs/fontRole", "description": "The app's own window: tabs, address bar, titlebar, panel." },
"dialog": { "$ref": "#/$defs/fontRole", "description": "Plugin dialogs and toasts (the UI kit)." }
}
},
"scale": { "type": "number", "minimum": 0.85, "maximum": 1.2, "default": 1 }, "scale": { "type": "number", "minimum": 0.85, "maximum": 1.2, "default": 1 },
"weight": { "type": "integer", "minimum": 300, "maximum": 500, "default": 400 }, "weight": { "type": "integer", "minimum": 300, "maximum": 500, "default": 400 },
"bold": { "type": "integer", "minimum": 500, "maximum": 800, "default": 700 }, "bold": { "type": "integer", "minimum": 500, "maximum": 800, "default": 700 },
@ -142,6 +156,36 @@
}, },
"$defs": { "$defs": {
"hex": { "type": "string", "pattern": "^#[0-9a-fA-F]{6}$" }, "hex": { "type": "string", "pattern": "^#[0-9a-fA-F]{6}$" },
"source": { "const": "google", "description": "The first family is a Google Fonts family: apps download it (asking first for an imported theme) and load it from bytes." },
"interfaceRole": {
"oneOf": [
{ "const": "inherit" },
{
"type": "object",
"required": ["family"],
"additionalProperties": false,
"properties": { "family": { "$ref": "#/$defs/fontStack" }, "source": { "$ref": "#/$defs/source" } }
}
]
},
"fontRole": {
"oneOf": [
{ "const": "inherit" },
{
"type": "object",
"required": ["family"],
"additionalProperties": false,
"properties": {
"family": { "$ref": "#/$defs/fontStack", "description": "Empty keeps the inherited family and changes only the settings below." },
"source": { "$ref": "#/$defs/source" },
"weight": { "type": "integer", "minimum": 100, "maximum": 900 },
"size": { "type": "number", "minimum": 0.75, "maximum": 1.5, "description": "Relative to the site's size." },
"tracking": { "type": "number", "minimum": -0.05, "maximum": 0.1, "description": "Letter spacing in em." },
"tabular": { "type": "boolean", "description": "Tabular (fixed-width) figures." }
}
}
]
},
"fontStack": { "fontStack": {
"type": "array", "type": "array",
"maxItems": 8, "maxItems": 8,