Shell: the chrome's own glyphs follow the theme's icon pack

A site's theme adapter may name the pack of the global theme (iconPack); the frame's Icon draws
that pack's glyph for the same meaning (home, search, bell, mail, bookmark, user, ...) from its
validated path data, and its own line glyph where the pack has none. Rendered as React SVG
elements, never markup.
This commit is contained in:
CRTkafa 2026-10-01 04:07:49 +07:00
parent 531d2a1e07
commit 2147de628c
3 changed files with 64 additions and 2 deletions

View file

@ -0,0 +1,50 @@
// The icon pack of the shell's own chrome: the global theme's pack (or the owner's pick), drawn in
// place of the frame's line glyphs wherever the pack has a glyph for that meaning. The glyphs are
// the pack's validated path data (framework themeIcons.ts), rendered as SVG elements by React, never
// as markup.
import { useSyncExternalStore } from 'react';
import type { IconGlyph, IconName, IconPack } from '../../../runtime/src/themeIcons.ts';
let current: IconPack | null = null;
const listeners = new Set<() => void>();
/** Sets the chrome's pack (the theme store calls it with the global theme's). */
export function setChromeIconPack(pack: IconPack | null) {
if (pack === current) return;
current = pack;
for (const listener of [...listeners]) listener();
}
const subscribe = (listener: () => void) => { listeners.add(listener); return () => { listeners.delete(listener); }; };
export const useChromeIconPack = () => useSyncExternalStore(subscribe, () => current, () => null);
/** Which pack glyph stands for each of the frame's own glyph names. */
export const CHROME_ICON_NAMES: Readonly<Record<string, IconName>> = {
home: 'home', search: 'search', explore: 'explore', bell: 'notifications', mail: 'messages', bookmark: 'bookmarks', user: 'profile',
list: 'lists', close: 'close', back: 'back', image: 'media', draft: 'compose', edit: 'compose', settings: 'settings',
};
let gradients = 0;
/** One pack glyph as SVG children (no `<svg>` wrapper). */
export function GlyphPaths({ glyph, pack }: { glyph: IconGlyph; pack: IconPack }) {
const style = pack.style;
if ('d' in glyph) {
return glyph.stroke
? <path d={glyph.d} style={{ fill: 'none', stroke: 'currentColor', strokeWidth: style.stroke, strokeLinecap: style.cap, strokeLinejoin: style.join }}/>
: <path d={glyph.d} fillRule={glyph.rule} style={{ fill: 'currentColor', stroke: 'none' }}/>;
}
if ('layers' in glyph) {
const base = `wbg${++gradients}`;
return <>{glyph.layers.map((layer, index) => {
const id = `${base}-${index}`;
const fill = typeof layer.fill === 'string'
? (layer.fill.startsWith('#') ? layer.fill : 'currentColor')
: `url(#${id})`;
return <g key={index}>
{typeof layer.fill === 'object' && <defs><linearGradient id={id} x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor={layer.fill.from}/><stop offset="1" stopColor={layer.fill.to}/></linearGradient></defs>}
<path d={layer.d} fillRule={layer.rule} opacity={layer.opacity} style={{ fill, stroke: 'none' }}/>
</g>;
})}</>;
}
return <image href={glyph.png} width="24" height="24" style={glyph.pixel ? { imageRendering: 'pixelated' } : undefined}/>;
}

View file

@ -1,5 +1,7 @@
// Shared 24px stroke glyphs for the shell chrome (1.75 stroke, round joins; sized by CSS). // Shared 24px stroke glyphs for the shell chrome (1.75 stroke, round joins; sized by CSS).
import { CHROME_ICON_NAMES, GlyphPaths, useChromeIconPack } from './iconPack.tsx';
export const ICONS = { export const ICONS = {
back: 'M15 5l-7 7 7 7', back: 'M15 5l-7 7 7 7',
forward: 'M9 5l7 7-7 7', forward: 'M9 5l7 7-7 7',
@ -57,8 +59,13 @@ export const ICONS = {
export type IconName = keyof typeof ICONS; export type IconName = keyof typeof ICONS;
export const Icon = ({ name, className }: { name: IconName; className?: string }) => /** One of the frame's glyphs, or the chrome icon pack's glyph for the same meaning (iconPack.tsx). */
<svg className={className ? `bt-glyph ${className}` : 'bt-glyph'} viewBox="0 0 24 24" aria-hidden="true"><path d={ICONS[name]}/></svg>; export const Icon = ({ name, className }: { name: IconName; className?: string }) => {
const pack = useChromeIconPack();
const mapped = CHROME_ICON_NAMES[name];
const glyph = pack && mapped ? pack.glyphs[mapped] : undefined;
return <svg className={className ? `bt-glyph ${className}` : 'bt-glyph'} viewBox="0 0 24 24" aria-hidden="true">{glyph && pack ? <GlyphPaths glyph={glyph} pack={pack}/> : <path d={ICONS[name]}/>}</svg>;
};
/** Readable initial on a profile color: dark ink on light colors, white on dark ones. */ /** Readable initial on a profile color: dark ink on light colors, white on dark ones. */
export function inkOn(hex: string): string { export function inkOn(hex: string): string {

View file

@ -5,6 +5,8 @@
// document is belongs to the app (`ThemeAdapter`); `createThemeStore(adapter)` makes the store the // document is belongs to the app (`ThemeAdapter`); `createThemeStore(adapter)` makes the store the
// app keeps and hands to the frame (`site.themes`). // app keeps and hands to the frame (`site.themes`).
import type { IconPack } from '../../../runtime/src/themeIcons.ts';
import { setChromeIconPack } from './iconPack.tsx';
import type { DockviewApi } from 'dockview'; import type { DockviewApi } from 'dockview';
import { BOOT_CACHE_KEY } from './boot.ts'; import { BOOT_CACHE_KEY } from './boot.ts';
import { invoke } from './bridge.ts'; import { invoke } from './bridge.ts';
@ -42,6 +44,8 @@ export type ThemeAdapter<Choice = unknown, Spec extends ThemeDoc = ThemeDoc> = {
googleFonts(spec: Spec, roles?: readonly string[]): string[]; googleFonts(spec: Spec, roles?: readonly string[]): string[];
/** The chrome's colors and fonts for a theme. */ /** The chrome's colors and fonts for a theme. */
chrome(spec: Spec): ChromeTheme; chrome(spec: Spec): ChromeTheme;
/** The icon pack a theme suggests for the chrome (theme engine v2); absent: the frame's own glyphs. */
iconPack?(spec: Spec): IconPack | null;
/** Three swatch colors for menus (page, text, accent, on the theme's surface and border). */ /** Three swatch colors for menus (page, text, accent, on the theme's surface and border). */
dots(spec: Spec): { page: string; bg: string; border: string; text: string; accent: string }; dots(spec: Spec): { page: string; bg: string; border: string; text: string; accent: string };
}; };
@ -204,6 +208,7 @@ export function createThemeStore<Choice, Spec extends ThemeDoc>(adapter: ThemeAd
chromeFontsFor(global); chromeFontsFor(global);
const vars = chromeVars(adapter.chrome(global), own); const vars = chromeVars(adapter.chrome(global), own);
setVars(document.documentElement, vars); setVars(document.documentElement, vars);
try { setChromeIconPack(own ? null : adapter.iconPack?.(global) ?? null); } catch { setChromeIconPack(null); }
// The next start paints these from its first frame (boot.ts); an editor's draft is not kept. // The next start paints these from its first frame (boot.ts); an editor's draft is not kept.
if (!state.preview) { try { localStorage.setItem(BOOT_CACHE_KEY, JSON.stringify(vars)); } catch { /* Best effort. */ } } if (!state.preview) { try { localStorage.setItem(BOOT_CACHE_KEY, JSON.stringify(vars)); } catch { /* Best effort. */ } }
syncGroups(); syncGroups();