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. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
873f8561ca
commit
da9ac11b24
3 changed files with 64 additions and 2 deletions
50
packages/shell/src/frame/iconPack.tsx
Normal file
50
packages/shell/src/frame/iconPack.tsx
Normal 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}/>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue