导航

v2.1.0
自定义

主题定制

以可用的 Paper 主题为例,覆盖主题令牌、定义材质配方并构建自己的主题。

本页内容

主题为令牌赋值,并定义组件读取的材质配方。可以调整一个共享值,将调整限定在页面的某个区域,也可以在保留组件标记和行为的情况下替换整个界面的材质。

Moonstone 和 Obsidian 的材质选择见设计。默认主题记录了这两个主题的令牌用途和取值。要判断应在主题还是组件中进行修改,先阅读自定义。

#调整令牌

在导入语句之后覆盖令牌。所有读取该令牌的组件都会随之更新。

app.css
@import '@master/css';@import '@master/ui';@theme {  --color-brand: oklch(58% .19 28);  --color-brand-ink: oklch(99% .01 28);  --radius-control: .5rem;}

这会更改品牌色表面和控件的默认圆角半径。小号和大号控件各有自己的圆角半径令牌。请在新的表面颜色下检查 --color-brand-ink 的表现。品牌色文字使用独立的 --color-brand-text 令牌,需要单独调整才能采用新色相。

#将覆盖限制在一个区域内

令牌是自定义属性,因此也遵循层叠规则。在元素上设置令牌即可调整其后代,无需重新构建。此处,结账区的两个按钮继承较小的圆角半径,区域外的按钮保留默认值:

令牌的作用域与继承

作用域决定后代元素继承的值。 结账区将 --radius-control 的默认值从 10px 改为 4px,供“核对订单”和“付款”按钮使用。“继续浏览”按钮位于结账区外,保留 10px。每个按钮都通过自身的 --btn-radius 读取继承的令牌。祖先元素上的 --btn-radius 无法替换按钮自身对该属性的声明。图中数值尚未乘以主题的圆角缩放系数。

仅在结账区生效的令牌

<div class="flex flex-col gap-lg w-full max-w(440) mx-auto">  <div class="sheet flex flex-col gap-sm p-md" style="--radius-control: .25rem">    <span class="text-xs fg-text-muted">Inside checkout · 4px radius token</span>    <div class="flex flex-wrap gap-sm">      <button type="button" class="btn">Review</button>      <button type="button" class="btn">Pay</button>    </div>  </div>  <div class="flex flex-wrap items-center gap-sm">    <button type="button" class="btn">Continue browsing</button>    <span class="text-xs fg-text-muted">Outside · default 10px token</span>  </div></div>
<div className="flex flex-col gap-lg w-full max-w(440) mx-auto">  <div className="sheet flex flex-col gap-sm p-md" style={{ '--radius-control': '.25rem' } as React.CSSProperties}>    <span className="text-xs fg-text-muted">Inside checkout · 4px radius token</span>    <div className="flex flex-wrap gap-sm">      <button type="button" className="btn">Review</button>      <button type="button" className="btn">Pay</button>    </div>  </div>  <div className="flex flex-wrap items-center gap-sm">    <button type="button" className="btn">Continue browsing</button>    <span className="text-xs fg-text-muted">Outside · default 10px token</span>  </div></div>
<div class="flex flex-col gap-lg w-full max-w(440) mx-auto">  <div class="sheet flex flex-col gap-sm p-md" style="--radius-control: .25rem">    <span class="text-xs fg-text-muted">Inside checkout · 4px radius token</span>    <div class="flex flex-wrap gap-sm">      <button type="button" class="btn">Review</button>      <button type="button" class="btn">Pay</button>    </div>  </div>  <div class="flex flex-wrap items-center gap-sm">    <button type="button" class="btn">Continue browsing</button>    <span class="text-xs fg-text-muted">Outside · default 10px token</span>  </div></div>
<template>  <div class="flex flex-col gap-lg w-full max-w(440) mx-auto">    <div class="sheet flex flex-col gap-sm p-md" style="--radius-control: .25rem">      <span class="text-xs fg-text-muted">Inside checkout · 4px radius token</span>      <div class="flex flex-wrap gap-sm">        <button type="button" class="btn">Review</button>        <button type="button" class="btn">Pay</button>      </div>    </div>    <div class="flex flex-wrap items-center gap-sm">      <button type="button" class="btn">Continue browsing</button>      <span class="text-xs fg-text-muted">Outside · default 10px token</span>    </div>  </div></template>

按钮通过自身的 --btn-radius 属性读取 --radius-control。仅在结账容器上设置 --btn-radius 时,每个按钮仍使用自己声明的属性值。可以为该区域设置主题令牌,也可以直接在按钮上设置组件属性。也可以将行内声明移入项目的类中,再将该类应用于容器。

#保留圆弧圆角

浏览器支持时,默认主题使用连续曲率圆角。要让整个界面都使用圆弧圆角,请同时设置这两个圆角属性:

app.css
:root {  --corner-shape: round;  --corner-scale: 1;}

@theme 中声明的令牌也可以通过工具类使用。bg-surface-tray 和 shadow-lifted 让自定义元素使用与组件相同的材质。导入 Master UI 后,预设的共享语义令牌采用默认主题的取值。覆盖令牌会同时调整读取它的组件和工具类。

#主题决定哪些内容

构建主题时可参照这份清单。令牌的用途和默认值见默认主题。

设置项令牌
各表面的色调--color-surface-base, -tray, -raised, -raised-low, -inset, -inset-hover, -floating, -chip, -inverse, -scrim
各线条的清晰度--color-line-hair, -contour, -divider, -strong, -mark
墨色--color-text-strong, -body, -muted, -disabled, -inverse, --color-focus, --color-focus-halo
受光位置--color-highlight、--color-lip、--color-shade-1 至 -5
层次--shadow-pressed, -flat, -raised, -raised-hover, -lifted, -floating, -overlay
墨色表面--color-surface-inverse-high、-low、--color-ink-high、-low 及其 -hover 形式、--color-ink-pressed、--color-ink-highlight、--color-ink-edge、--shadow-ink、-ink-hover、-ink-pressed、-ink-mark、-ink-well、-ink-top、--color-surface-tip、--shadow-tip
滑块--color-thumb-high, -low, --color-thumb-on-high, -on-low, --shadow-thumb-on-ink
色相--color-brand, -brand-ink, -brand-text, --color-info, -success, -warning, -danger, --color-tone-highlight, -tone-highlight-soft
图片与传入的颜色--color-line-image, -image-strong, --color-line-on-color, --color-highlight-on-color, --color-on-image, -on-image-shade, --color-mark, -mark-underline, --color-sheen
形状--radius-chip, -control-sm, -control, -control-lg, -surface, --corner-shape, --corner-scale
位移与时长--lift, --duration-press, -quick, -base, --easing-glide
字体排印--font-weight-control,以及预设的 --font-sans 和 --font-mono

如果组件样式表包含颜色字面值,包的测试就会失败,以确保组件颜色由主题控制。要查阅完整的令牌和配方,以已安装的 @master/ui/theme.css 为准。

#另一个主题 Paper

Paper 随 @master/ui/themes/paper.css 发布。它将默认材质换成平坦的表面、线条勾勒的边缘、半径更小的圆弧圆角和暖调中性色,并采用绿色作为品牌色。它的 --lift: 0px 取消由该令牌控制的悬停和按压位移,开关滑块移动等组件动画仍然有效。Paper 不改动任何组件样式表。

app.css
@import '@master/css';@import '@master/ui';@import '@master/ui/themes/paper.css';

同一份标记在两个主题中的效果:

Moonstone

<div class="tray max-w(440) mx-auto">  <div class="sheet flex flex-col gap-lg p-lg">    <div class="flex flex-wrap items-center gap-sm">      <button type="button" class="btn btn-primary">Deploy</button>      <button type="button" class="btn">Preview</button>      <button type="button" class="btn btn-quiet">Cancel</button>      <button type="button" class="btn btn-brand">Upgrade</button>    </div>    <div class="field">      <label for="theme-domain">Domain</label>      <input class="input" id="theme-domain" value="atlas.dev">    </div>    <div class="flex flex-wrap items-center gap-lg">      <label class="check"><input type="checkbox" class="checkbox" checked> Preview builds</label>      <label class="check"><input type="radio" class="radio" name="theme-plan" checked> Team</label>      <label class="check">        <input type="checkbox" role="switch" class="switch" checked> Alerts      </label>    </div>    <div class="segmented" role="radiogroup" aria-label="Environment">      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment" checked>Production      </label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment">Preview      </label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment">Local      </label>    </div>    <input type="range" class="slider" value="60" aria-label="Traffic to the new release">    <div class="flex flex-wrap items-center gap-sm">      <span class="badge">Draft</span>      <span class="badge badge-success badge-dot">Ready</span>      <span class="badge badge-ink">New</span>    </div>    <div class="sheet sheet-inverse p-md text-sm">Ship the release when the checks pass.</div>  </div></div>
<div className="tray max-w(440) mx-auto">  <div className="sheet flex flex-col gap-lg p-lg">    <div className="flex flex-wrap items-center gap-sm">      <button type="button" className="btn btn-primary">Deploy</button>      <button type="button" className="btn">Preview</button>      <button type="button" className="btn btn-quiet">Cancel</button>      <button type="button" className="btn btn-brand">Upgrade</button>    </div>    <div className="field">      <label htmlFor="theme-domain">Domain</label>      <input className="input" id="theme-domain" defaultValue="atlas.dev" />    </div>    <div className="flex flex-wrap items-center gap-lg">      <label className="check">        <input type="checkbox" className="checkbox" defaultChecked /> Preview builds      </label>      <label className="check">        <input type="radio" className="radio" name="theme-plan" defaultChecked /> Team      </label>      <label className="check">        <input type="checkbox" role="switch" className="switch" defaultChecked /> Alerts      </label>    </div>    <div className="segmented" role="radiogroup" aria-label="Environment">      <label className="segmented-item">        <input type="radio" className="segmented-input" name="theme-environment" defaultChecked />Production      </label>      <label className="segmented-item">        <input type="radio" className="segmented-input" name="theme-environment" />Preview      </label>      <label className="segmented-item">        <input type="radio" className="segmented-input" name="theme-environment" />Local      </label>    </div>    <input type="range" className="slider" defaultValue="60" aria-label="Traffic to the new release" />    <div className="flex flex-wrap items-center gap-sm">      <span className="badge">Draft</span>      <span className="badge badge-success badge-dot">Ready</span>      <span className="badge badge-ink">New</span>    </div>    <div className="sheet sheet-inverse p-md text-sm">Ship the release when the checks pass.</div>  </div></div>
<div class="tray max-w(440) mx-auto">  <div class="sheet flex flex-col gap-lg p-lg">    <div class="flex flex-wrap items-center gap-sm">      <button type="button" class="btn btn-primary">Deploy</button>      <button type="button" class="btn">Preview</button>      <button type="button" class="btn btn-quiet">Cancel</button>      <button type="button" class="btn btn-brand">Upgrade</button>    </div>    <div class="field">      <label for="theme-domain">Domain</label>      <input class="input" id="theme-domain" value="atlas.dev">    </div>    <div class="flex flex-wrap items-center gap-lg">      <label class="check"><input type="checkbox" class="checkbox" checked> Preview builds</label>      <label class="check"><input type="radio" class="radio" name="theme-plan" checked> Team</label>      <label class="check">        <input type="checkbox" role="switch" class="switch" checked> Alerts      </label>    </div>    <div class="segmented" role="radiogroup" aria-label="Environment">      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment" checked>Production      </label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment">Preview      </label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment">Local      </label>    </div>    <input type="range" class="slider" value="60" aria-label="Traffic to the new release">    <div class="flex flex-wrap items-center gap-sm">      <span class="badge">Draft</span>      <span class="badge badge-success badge-dot">Ready</span>      <span class="badge badge-ink">New</span>    </div>    <div class="sheet sheet-inverse p-md text-sm">Ship the release when the checks pass.</div>  </div></div>
<template>  <div class="tray max-w(440) mx-auto">    <div class="sheet flex flex-col gap-lg p-lg">      <div class="flex flex-wrap items-center gap-sm">        <button type="button" class="btn btn-primary">Deploy</button>        <button type="button" class="btn">Preview</button>        <button type="button" class="btn btn-quiet">Cancel</button>        <button type="button" class="btn btn-brand">Upgrade</button>      </div>      <div class="field">        <label for="theme-domain">Domain</label>        <input class="input" id="theme-domain" value="atlas.dev">      </div>      <div class="flex flex-wrap items-center gap-lg">        <label class="check"><input type="checkbox" class="checkbox" checked> Preview builds</label>        <label class="check">          <input type="radio" class="radio" name="theme-plan" checked> Team        </label>        <label class="check">          <input type="checkbox" role="switch" class="switch" checked> Alerts        </label>      </div>      <div class="segmented" role="radiogroup" aria-label="Environment">        <label class="segmented-item">          <input type="radio" class="segmented-input" name="theme-environment" checked>Production        </label>        <label class="segmented-item">          <input type="radio" class="segmented-input" name="theme-environment">Preview        </label>        <label class="segmented-item">          <input type="radio" class="segmented-input" name="theme-environment">Local        </label>      </div>      <input type="range" class="slider" value="60" aria-label="Traffic to the new release">      <div class="flex flex-wrap items-center gap-sm">        <span class="badge">Draft</span>        <span class="badge badge-success badge-dot">Ready</span>        <span class="badge badge-ink">New</span>      </div>      <div class="sheet sheet-inverse p-md text-sm">Ship the release when the checks pass.</div>    </div>  </div></template>

Paper

<div class="tray max-w(440) mx-auto">  <div class="sheet flex flex-col gap-lg p-lg">    <div class="flex flex-wrap items-center gap-sm">      <button type="button" class="btn btn-primary">Deploy</button>      <button type="button" class="btn">Preview</button>      <button type="button" class="btn btn-quiet">Cancel</button>      <button type="button" class="btn btn-brand">Upgrade</button>    </div>    <div class="field">      <label for="theme-domain">Domain</label>      <input class="input" id="theme-domain" value="atlas.dev">    </div>    <div class="flex flex-wrap items-center gap-lg">      <label class="check"><input type="checkbox" class="checkbox" checked> Preview builds</label>      <label class="check"><input type="radio" class="radio" name="theme-plan" checked> Team</label>      <label class="check">        <input type="checkbox" role="switch" class="switch" checked> Alerts      </label>    </div>    <div class="segmented" role="radiogroup" aria-label="Environment">      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment" checked>Production      </label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment">Preview      </label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment">Local      </label>    </div>    <input type="range" class="slider" value="60" aria-label="Traffic to the new release">    <div class="flex flex-wrap items-center gap-sm">      <span class="badge">Draft</span>      <span class="badge badge-success badge-dot">Ready</span>      <span class="badge badge-ink">New</span>    </div>    <div class="sheet sheet-inverse p-md text-sm">Ship the release when the checks pass.</div>  </div></div>
<div className="tray max-w(440) mx-auto">  <div className="sheet flex flex-col gap-lg p-lg">    <div className="flex flex-wrap items-center gap-sm">      <button type="button" className="btn btn-primary">Deploy</button>      <button type="button" className="btn">Preview</button>      <button type="button" className="btn btn-quiet">Cancel</button>      <button type="button" className="btn btn-brand">Upgrade</button>    </div>    <div className="field">      <label htmlFor="theme-domain">Domain</label>      <input className="input" id="theme-domain" defaultValue="atlas.dev" />    </div>    <div className="flex flex-wrap items-center gap-lg">      <label className="check">        <input type="checkbox" className="checkbox" defaultChecked /> Preview builds      </label>      <label className="check">        <input type="radio" className="radio" name="theme-plan" defaultChecked /> Team      </label>      <label className="check">        <input type="checkbox" role="switch" className="switch" defaultChecked /> Alerts      </label>    </div>    <div className="segmented" role="radiogroup" aria-label="Environment">      <label className="segmented-item">        <input type="radio" className="segmented-input" name="theme-environment" defaultChecked />Production      </label>      <label className="segmented-item">        <input type="radio" className="segmented-input" name="theme-environment" />Preview      </label>      <label className="segmented-item">        <input type="radio" className="segmented-input" name="theme-environment" />Local      </label>    </div>    <input type="range" className="slider" defaultValue="60" aria-label="Traffic to the new release" />    <div className="flex flex-wrap items-center gap-sm">      <span className="badge">Draft</span>      <span className="badge badge-success badge-dot">Ready</span>      <span className="badge badge-ink">New</span>    </div>    <div className="sheet sheet-inverse p-md text-sm">Ship the release when the checks pass.</div>  </div></div>
<div class="tray max-w(440) mx-auto">  <div class="sheet flex flex-col gap-lg p-lg">    <div class="flex flex-wrap items-center gap-sm">      <button type="button" class="btn btn-primary">Deploy</button>      <button type="button" class="btn">Preview</button>      <button type="button" class="btn btn-quiet">Cancel</button>      <button type="button" class="btn btn-brand">Upgrade</button>    </div>    <div class="field">      <label for="theme-domain">Domain</label>      <input class="input" id="theme-domain" value="atlas.dev">    </div>    <div class="flex flex-wrap items-center gap-lg">      <label class="check"><input type="checkbox" class="checkbox" checked> Preview builds</label>      <label class="check"><input type="radio" class="radio" name="theme-plan" checked> Team</label>      <label class="check">        <input type="checkbox" role="switch" class="switch" checked> Alerts      </label>    </div>    <div class="segmented" role="radiogroup" aria-label="Environment">      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment" checked>Production      </label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment">Preview      </label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="theme-environment">Local      </label>    </div>    <input type="range" class="slider" value="60" aria-label="Traffic to the new release">    <div class="flex flex-wrap items-center gap-sm">      <span class="badge">Draft</span>      <span class="badge badge-success badge-dot">Ready</span>      <span class="badge badge-ink">New</span>    </div>    <div class="sheet sheet-inverse p-md text-sm">Ship the release when the checks pass.</div>  </div></div>
<template>  <div class="tray max-w(440) mx-auto">    <div class="sheet flex flex-col gap-lg p-lg">      <div class="flex flex-wrap items-center gap-sm">        <button type="button" class="btn btn-primary">Deploy</button>        <button type="button" class="btn">Preview</button>        <button type="button" class="btn btn-quiet">Cancel</button>        <button type="button" class="btn btn-brand">Upgrade</button>      </div>      <div class="field">        <label for="theme-domain">Domain</label>        <input class="input" id="theme-domain" value="atlas.dev">      </div>      <div class="flex flex-wrap items-center gap-lg">        <label class="check"><input type="checkbox" class="checkbox" checked> Preview builds</label>        <label class="check">          <input type="radio" class="radio" name="theme-plan" checked> Team        </label>        <label class="check">          <input type="checkbox" role="switch" class="switch" checked> Alerts        </label>      </div>      <div class="segmented" role="radiogroup" aria-label="Environment">        <label class="segmented-item">          <input type="radio" class="segmented-input" name="theme-environment" checked>Production        </label>        <label class="segmented-item">          <input type="radio" class="segmented-input" name="theme-environment">Preview        </label>        <label class="segmented-item">          <input type="radio" class="segmented-input" name="theme-environment">Local        </label>      </div>      <input type="range" class="slider" value="60" aria-label="Traffic to the new release">      <div class="flex flex-wrap items-center gap-sm">        <span class="badge">Draft</span>        <span class="badge badge-success badge-dot">Ready</span>        <span class="badge badge-ink">New</span>      </div>      <div class="sheet sheet-inverse p-md text-sm">Ship the release when the checks pass.</div>    </div>  </div></template>

可以用 Paper 的源码作为模板。源码包含一组令牌、一项圆角设置和两条材质配方。

@master/ui/themes/paper.css
/* * Paper: a second theme, and the proof that the first one is only a theme. * * Moonstone is lit from above: faces fall from light to shade, edges are found by a highlight and a shadow, * controls rise and sink. Paper has no light source. Every face is one flat tone, every edge is a ruled * hairline, nothing travels, corners are tighter and circular, and the neutrals are warm instead of zinc. * It is written with tokens alone, plus the recipes a token cannot reach (a toned control and the brand's band * mix their faces from a hue), and it changes no component. Import it after the library: * *   @import '@master/css'; *   @import '@master/ui'; *   @import '@master/ui/themes/paper.css'; * * Copy this file to start a theme of your own: the tokens below are the ones a theme decides. */@theme {  /* Surfaces: warm paper and warm slate. A raised face has no fall, so its two ends are one tone. */  --color-surface-base: light-dark(oklch(98.4% .004 90), oklch(18.5% .004 80));  --color-surface-tray: light-dark(oklch(96.2% .005 90), oklch(16.5% .004 80));  --color-surface-raised: light-dark(oklch(99.6% .002 90), oklch(22.5% .005 80));  --color-surface-raised-low: var(--color-surface-raised);  --color-surface-inset: light-dark(oklch(96.8% .005 90), oklch(15.5% .004 80));  --color-surface-inset-hover: light-dark(oklch(95.6% .006 90), oklch(14.5% .004 80));  --color-surface-floating: light-dark(oklch(99.6% .002 90), oklch(25% .005 80));  --color-surface-chip: light-dark(oklch(95.4% .006 90), oklch(100% 0 none / .08));  --color-surface-inverse: light-dark(oklch(24% .006 80), oklch(94% .005 90));  --color-surface-inverse-high: var(--color-surface-inverse);  --color-surface-inverse-low: var(--color-surface-inverse);  --color-surface-tip: light-dark(var(--color-surface-inverse), oklch(32% .006 80));  /* Lines carry every edge here, so each is a step firmer than under a light. */  --color-line-hair: light-dark(oklch(24% .006 80 / .1), oklch(100% 0 none / .11));  --color-line-contour: light-dark(oklch(24% .006 80 / .22), oklch(100% 0 none / .22));  --color-line-divider: light-dark(oklch(24% .006 80 / .16), oklch(100% 0 none / .16));  --color-line-strong: light-dark(oklch(24% .006 80 / .36), oklch(100% 0 none / .36));  --color-line-mark: light-dark(oklch(24% .006 80 / .56), oklch(100% 0 none / .56));  --color-text-strong: light-dark(oklch(22% .006 80), oklch(95% .005 90));  --color-text-body: light-dark(oklch(38% .007 80), oklch(81% .006 90));  --color-text-muted: light-dark(oklch(48% .008 80), oklch(69% .007 90));  --color-text-disabled: light-dark(oklch(68% .007 80), oklch(48% .007 90));  --color-text-inverse: light-dark(oklch(98.4% .004 90), oklch(18.5% .004 80));  --color-focus: light-dark(oklch(24% .006 80), oklch(94% .005 90));  --color-focus-halo: light-dark(oklch(24% .006 80 / .12), oklch(100% 0 none / .14));  /* No light: no highlight on a top edge, no lip under a recess, no falling shade. */  --color-highlight: transparent;  --color-lip: transparent;  --color-shade-1: transparent;  --color-shade-2: transparent;  --color-shade-3: transparent;  --color-shade-4: transparent;  --color-shade-5: transparent;  /* Ink is one flat tone at rest, a step lighter under the pointer and a step darker under a press. */  --color-ink-high: var(--color-surface-inverse);  --color-ink-low: var(--color-surface-inverse);  --color-ink-high-hover: light-dark(oklch(32% .007 80), oklch(100% 0 none));  --color-ink-low-hover: var(--color-ink-high-hover);  --color-ink-pressed: light-dark(oklch(18% .005 80), oklch(87% .006 90));  --color-ink-highlight: transparent;  --color-ink-edge: transparent;  --color-tone-highlight: transparent;  --color-tone-highlight-soft: transparent;  /* A thumb is a flat disc with a ruled edge. */  --color-thumb-high: oklch(100% 0 none);  --color-thumb-low: oklch(100% 0 none);  --color-thumb-on-high: light-dark(oklch(100% 0 none), oklch(22.5% .005 80));  --color-thumb-on-low: var(--color-thumb-on-high);  /* Brand: a forest green, so the one hued moment is plainly not the default indigo. */  --color-brand: oklch(50% .13 155);  --color-brand-ink: oklch(99% .01 155);  --color-brand-text: light-dark(oklch(44% .12 155), oklch(80% .12 155));  /*   * Elevation is a rule, not a shadow. What floats over content (a menu, a dialog) keeps one soft shade,   * because a rule alone cannot say that something is above the page.   */  --shadow-pressed: inset 0 0 0 .5px var(--color-line-strong);  --shadow-flat: 0 0 0 .5px var(--color-line-contour);  --shadow-raised: 0 0 0 .5px var(--color-line-strong);  --shadow-raised-hover: 0 0 0 .5px var(--color-line-mark);  --shadow-lifted: 0 0 0 .5px var(--color-line-contour);  --shadow-floating: 0 0 0 .5px var(--color-line-strong), 0 12px 32px -16px light-dark(oklch(24% .006 80 / .28), oklch(0% 0 none / .6));  --shadow-overlay: 0 0 0 .5px var(--color-line-strong), 0 32px 64px -32px light-dark(oklch(24% .006 80 / .36), oklch(0% 0 none / .7));  --shadow-ink: 0 0 0 0 transparent;  --shadow-ink-hover: 0 0 0 0 transparent;  --shadow-ink-pressed: 0 0 0 0 transparent;  --shadow-ink-mark: 0 0 0 0 transparent;  --shadow-ink-well: 0 0 0 0 transparent;  --shadow-ink-top: 0 0 0 0 transparent;  --shadow-tip: 0 0 0 .5px light-dark(transparent, var(--color-line-strong));  --shadow-thumb-on-ink: 0 0 0 .5px light-dark(oklch(24% .006 80 / .3), oklch(0% 0 none / .5));  /* Shape: tighter, and circular in every browser (see the corner block below). */  --radius-chip: .1875rem;  --radius-control-sm: .25rem;  --radius-control: .3125rem;  --radius-control-lg: .375rem;  --radius-surface: .5rem;  --font-weight-control: 500;  /* Nothing rises or sinks. */  --lift: 0px;}@layer theme {  :root, :host {    --corner-shape: round;    --corner-scale: 1;  }  /* More contrast, when the reader asks for it: the rules that carry every edge here are firmer still. */  @media (prefers-contrast: more) {    :root, :host {      --color-line-hair: light-dark(oklch(24% .006 80 / .22), oklch(100% 0 none / .24));      --color-line-contour: light-dark(oklch(24% .006 80 / .4), oklch(100% 0 none / .42));      --color-line-divider: light-dark(oklch(24% .006 80 / .34), oklch(100% 0 none / .36));      --color-line-strong: light-dark(oklch(24% .006 80 / .56), oklch(100% 0 none / .58));      --color-line-mark: light-dark(oklch(24% .006 80 / .74), oklch(100% 0 none / .76));      --color-text-muted: light-dark(oklch(38% .008 80), oklch(80% .007 90));      --color-text-disabled: light-dark(oklch(55% .007 80), oklch(62% .007 90));    }  }}/* * A toned control mixes its face from its own hue, which a root token cannot do, so this theme writes the * recipe again: one flat hue, a step lighter under the pointer and a step darker under a press. */@mixin --control-tone {  color: var(--tone-ink);  background: var(--tone);  box-shadow: 0 0 0 0 transparent;}@mixin --control-tone-hover {  background: color-mix(in oklab, var(--tone), white 10%);  box-shadow: 0 0 0 0 transparent;}@mixin --control-tone-pressed {  background: color-mix(in oklab, var(--tone), black 12%);  box-shadow: 0 0 0 0 transparent;}/* The brand's band is one flat hue too: no light along its top and no darker edge below. */@mixin --surface-brand {  background: var(--color-brand);  box-shadow: none;}

#创建主题文件

要从平面材质入手,请从已安装的包中复制 Paper。要以默认材质为基础,请复制 theme.css:

mkdir -p themescp node_modules/@master/ui/themes/paper.css themes/app.css

先导入库,再导入自己的主题文件,然后编辑该文件中的令牌和材质配方:

app.css
@import '@master/css';@import '@master/ui';@import './themes/app.css';

用现有的 Master CSS 集成重新构建。组件标记保持不变。在整个应用中启用主题前,先在浅色和深色模式下对照检查按钮、输入字段、墨色表面和浮层。

#材质配方

有些材质需要多个值共同定义,例如带有光照渐变的表面,或各个状态下表面不同的控件。主题用混入定义这些配方,组件按名称应用。主题文件可以重新定义混入,所有组件都使用最后一次定义。

混入绘制内容Paper
--surface-sheet凸起的表面通过 --color-surface-raised-low 呈现平面效果
--surface-ink墨色表面,例如反色面板或已发送的消息通过 -inverse-high 和 -low 令牌呈现平面效果
--surface-brand品牌色色带,例如品牌色横幅重新定义为单一纯色
--control-ink, -hover, -pressed主按钮在各状态下的表面和阴影通过墨色令牌呈现平面效果
--control-tone, -hover, -pressed带色调的控件,以控件自身的 --tone 为基础混色重新定义为单一纯色
--surface-thumb开关和滑动条的滑块通过滑块令牌呈现平面效果
--focus-ring键盘焦点保持不变
--on-ink墨色表面传给其中行内胶囊标签、徽标和文本高亮的样式信息保持不变
--anchored, --pop弹出层的位置及其进出场效果保持不变

分类的四个角色也是主题令牌:--color-category-<name>-surface、-text、-line 和 -mark。因此主题可以像调整任何颜色一样重新调整色板,同时保持默认主题所保证的对比度:文字在自身底色及所在的各个表面上达到 4.5:1,标记达到 3:1。在 packages/ui 中,scripts/category.ts 根据预设的色板生成默认的分类令牌。

优先使用主题令牌。Paper 只重新定义两条配方,因为两者都用色相混出表面颜色,而不是读取令牌:带色调控件的配方用控件自身设置的色相混色,而根元素上的令牌无法读取该色相;品牌色带的配方用 --color-brand 混出更亮的顶部和更暗的边缘,这两处颜色均未由令牌直接设置。

主题令牌是 CSS 自定义属性,可在运行时用于任何元素。Master CSS 编译样式表时会解析混入,因此重新定义的混入对整个构建生效。

#不使用 Master CSS

包中也提供各个主题与组件一起编译得到的纯 CSS。CSS 打包工具可以从已安装的包中导入 Paper,用它替换默认的独立样式表:

app.css
@import '@master/ui/dist/themes/paper.css';:root {  --color-brand: oklch(58% .19 28);}

页面没有使用打包工具时,可将编译后的文件作为静态资源提供。样式重置和工具类的使用限制见安装。dist/standalone.css 包含 moonstone 和 obsidian。编译后的文件支持覆盖自定义属性,重新定义混入则需要用 Master CSS 编译主题。

#主题编写规则

  • 主题中禁止使用组件选择器。 主题负责设置令牌和定义混入。主题中形如 .btn { … } 的规则属于组件样式覆盖,应该写在哪里可参阅自定义。
  • 成对选取颜色。 表面与其上的墨色要一起选定,线条与其下方的表面也一样。在浅色和深色模式下,检查正文在各个表面上的对比度。
  • 让边界清晰可辨。 如果主题去掉了光影,边界就更依赖线条来界定。Paper 使用更醒目的线条令牌,并按用途和配色模式分别调整。
  • 用 0 0 0 0 transparent 表示不可见的阴影令牌,避免使用 none。 组件通过列表组合多个阴影,none 在列表中是无效值。
  • 响应更高对比度的偏好。 在 prefers-contrast: more 下,主题会增强线条和两种低强调度墨色的对比度。如果主题自行设置了线条,也要像 Paper 一样编写这个规则块。修改材质配方时,保留组件在强制颜色和减少动态效果模式下的行为。

在浅色和深色模式下,检查键盘聚焦、悬停、按下、禁用控件和无效字段的效果。在页面上打开菜单或对话框,结合实际背景检查边缘和文字是否清晰。启用减少动态效果和强制颜色后再检查一遍。静止时看起来清晰的主题,仍可能让某个状态难以辨认,或使唯一可见的边界消失。