Replaceable product icons through an SVG sprite
通过 SVG sprite 实现可替换的产品图标
Long-term skin customization needs themes to replace icons, not only colors. Product icons were React components that drew inline SVG paths, so a theme registered through `ctx.theme` could recolor them through `currentColor` but could not change their artwork. About 150 Client files import these components.
English
Problem
Long-term skin customization needs themes to replace icons, not only colors. Product icons were React components that drew inline SVG paths, so a theme registered through ctx.theme could recolor them through currentColor but could not change their artwork. About 150 Client files import these components.
Decision
Each product glyph weight is one sprite symbol, dsw-icon-<glyph>-<weight>. ui-primitives keeps the default SVG documents as ICON_SYMBOLS; an icon component keeps its export name, size, and className, and renders an <svg> box with one <use> of its symbol. ui-theme keeps one hidden sprite in the document whose <symbol> children come from the active theme's ThemeDefinition.icons replacements or the defaults. Artwork reached through <use> inherits currentColor and CSS custom properties from the use site, so a replacement can be multi-color, can paint with --dsw-* color tokens, and follows light and dark palettes without separate values.
Alternatives considered
- Icons as CSS image tokens painted through
mask-image. Rejected after design review: a mask keeps only the image's alpha, so every icon stays one color, and aurl()image cannot read CSS variables, so multi-color artwork could not follow color tokens. It would have reused the token override layer without a new theme field. - A runtime icon registry service with React components as values. Rejected: components cannot read services or React context, so every icon import site would need a new prop or slot.
- A slot per icon. Rejected for the same caller cost; slots carry composition, not artwork.
Consequences
- Icon DOM contains a
<use>reference instead of paths; tests identify glyphs throughdata-iconand inspect artwork throughICON_SYMBOLS. - Without ui-theme, the sprite is absent and icons render nothing.
- Every symbol shares the document id space, so
registerrequires element ids inside a replacement to start with<symbol id>-;<use>in another document, such as an iframe, cannot reach the sprite. - Replacement markup is trusted like the registering plugin's code;
registerrejects<style>and<script>only because they would apply to the whole document. - Gradients and other paint servers resolve their stops where the sprite sits, so they follow palette tokens but not use-site color overrides.
- Default and token-colored replacement symbols render pixel-identically through
<use>and inline in Chromium, WebKit, and Firefox. - File-type, plugin, site, and brand artwork still render inline and are not replaceable yet.
中文
问题
长期的皮肤定制需要主题能替换图标,而不只是颜色。产品图标原先是绘制内联 SVG 路径的 React 组件,因此通过 ctx.theme 注册的主题可以借助 currentColor 给它们换色,却无法更换图形。约 150 个 Client 文件导入了这些组件。
决策
每个产品图形的每种线重对应一个 sprite symbol dsw-icon-<glyph>-<weight>。ui-primitives 以 ICON_SYMBOLS 保存默认 SVG 文档;图标组件保留导出名、size 与 className,渲染一个 <svg> 框,其中包含一个引用自身 symbol 的 <use>。ui-theme 在文档中保留一个隐藏的 sprite,其 <symbol> 子元素来自活动主题的 ThemeDefinition.icons 替换项或默认值。通过 <use> 引用的图形会从使用处继承 currentColor 与 CSS 自定义属性,因此替换图形可以是多色的,可以用 --dsw-* 颜色 token 绘制,并且无需单独的值就能跟随浅色与深色调色板。
考虑过的替代方案
- 把图标作为通过
mask-image绘制的 CSS 图像 token。 经设计评审后否决:遮罩只保留图像的 alpha,因此每个图标都只能是单色;url()图像无法读取 CSS 变量,因此多色图形无法跟随颜色 token。该方案本可复用 token 覆盖层,而无需新增主题字段。 - 以 React 组件为值的运行时图标注册服务。 否决:组件无法读取服务或 React context,因此每个图标导入处都需要新的 prop 或 slot。
- 每个图标一个 slot。 因同样的调用方成本而否决;slot 承载的是组合,而不是图形。
后果
- 图标 DOM 包含
<use>引用而不是路径;测试通过data-icon识别图形,通过ICON_SYMBOLS检查图形。 - 没有 ui-theme 时,sprite 不存在,图标不渲染任何内容。
- 所有 symbol 共享文档的 id 空间,因此
register要求替换图形内的元素 id 以<symbol id>-开头;另一个文档(例如 iframe)中的<use>无法引用该 sprite。 - 替换标记与注册它的插件代码同等受信任;
register仅拒绝<style>与<script>,因为它们会作用于整个文档。 - 渐变及其他绘制服务器在 sprite 所在位置解析其色标,因此它们跟随调色板 token,但不跟随使用处的颜色覆盖。
- 默认 symbol 与使用 token 着色的替换 symbol 在 Chromium、WebKit 与 Firefox 中通过
<use>渲染与内联渲染的结果逐像素一致。 - 文件类型、插件、站点与品牌图形仍以内联方式渲染,暂不可替换。