主题与设计令牌
ConfigProvider 配置其子树的设计。从通用设计令牌(design tokens)开始,并在确实需要例外时更改单个组件标记。
通用主题
<script lang="ts">
import { Button, ConfigProvider } from '@woodger/antd';
let color = $state('#389e0d');
</script>
<ConfigProvider theme={{ token: { colorPrimary: color, borderRadius: 8 } }}>
<Button type="primary">Сохранить</Button>
</ConfigProvider>
改变 color 会反应性地更新主题。不要仅仅为了改变颜色而通过按键块替换组件:这将重置它们的本地状态。
代币级别
种子令牌指定初始决策,例如 colorPrimary、fontSize 和 borderRadius。该算法接收种子并计算地图令牌:调色板、状态和指标。 Alias Token 将计算值与界面元素的用途关联起来,例如 colorTextSecondary 或 colorBgContainer。
当更改应该对调色板产生一致的效果时,从种子开始。点别名覆盖不会取代关联状态的重新计算。除非必要,否则不要在应用程序状态下保存单独计算的调色板。
使用 theme.components 设置单个组件族的主题。其配置记录可以包含已公开的组件令牌以及允许覆盖的通用令牌。类型中存在某个名称,并不能代替对该令牌实际应用情况的验证;当前限制见相应组件族页面。
算法和嵌套
theme 接受来自 theme 公共对象的 defaultAlgorithm、darkAlgorithm 和 compactAlgorithm。可以组合算法:
<script lang="ts">
import { Button, ConfigProvider, theme } from '@woodger/antd';
</script>
<ConfigProvider theme={{ mode: 'dark', compact: true, algorithm: [theme.darkAlgorithm, theme.compactAlgorithm] }}>
<Button>Тёмная компактная тема</Button>
</ConfigProvider>
算法按顺序应用。示例中的 mode 和紧凑也将区域的本机模式与所选算法相匹配。对于轻型常规主题,请使用 defaultAlgorithm。
嵌套提供程序允许您配置单独的区域。常规设置和本地异常显示在 ConfigProvider 页上。
动态主题
该示例切换颜色、深色和紧凑模式,而无需重新创建输入。该区域有自己的主题;当前标记由子 ThemeReadout 读取。这两个文件都显示在源代码中。
主题对象可以从 Svelte 状态反应式地计算。不要将一个区域包裹在一个关键块中来改变算法。将选定的设置存储在应用程序中;对于与 SSR 相同的初始主题,从服务器传输模式,如 饼干的例子 中。
没有状态重置的主题
独立范围、反应式算法和子组件标记。
ThemeExample.svelte<script lang="ts"> import { Button, Checkbox, ConfigProvider, Input, theme } from '@woodger/antd'; import ThemeReadout from './ThemeReadout.svelte'; let dark = $state(false); let compact = $state(false); let purple = $state(false); let draft = $state(''); let saved = $state(''); const algorithms = $derived([ dark ? theme.darkAlgorithm : theme.defaultAlgorithm, ...(compact ? [theme.compactAlgorithm] : []) ]); </script> <style> .example, .surface { display: grid; gap: 16px; } .controls { display: flex; flex-wrap: wrap; gap: 12px; } .surface { padding: 16px; border-radius: 12px; background: var(--ant-color-bg-container); } .surface :global(.ant-btn) { justify-self: start; } output { overflow-wrap: anywhere; } </style> <div class="example"> <div class="controls"> <Checkbox bind:checked={dark}>{("深色主题示例")}</Checkbox> <Checkbox bind:checked={compact}>{("紧凑的示例主题")}</Checkbox> <Checkbox bind:checked={purple}>{("紫色基色")}</Checkbox> </div> <ConfigProvider theme={{ inherit: false, mode: dark ? 'dark' : 'light', compact, algorithm: algorithms, token: { colorPrimary: purple ? '#722ed1' : '#389e0d', motion: false }, components: { Button: { borderRadius: 12 } } }}> <div class="surface"> <Input aria-label={("草稿主题")} placeholder={("输入文字")} bind:value={draft} /> <Button type="primary" onclick={() => { saved = draft; }} >{("保存草稿")}</Button> <output aria-label={("案例研究结果")}>{(saved || ("尚未保存"))}</output> <ThemeReadout /> </div> </ConfigProvider> </div>ThemeReadout.svelte<script lang="ts"> import { theme } from '@woodger/antd'; const current = theme.useToken(); </script> <style> output { display: block; font-size: 12px; overflow-wrap: anywhere; } </style> <output aria-label={("主题标记示例")}> primary: {(current.token.colorPrimary)}; height: {(current.token.controlHeight)}; font: {(current.token.fontSize)}; background: {(current.token.colorBgContainer)} </output>
继承与隔离
theme.token 与父覆盖相结合。 theme.components 中的记录由组件名称及其一级属性组合而成。 Algorithm 选择范围算法,并 inherit:false 重置父令牌/算法/组件。 在所需提供者下初始化子组件时调用
Inherit:false 不会单独重置整个 ConfigProvider:locale、direction 和组件配置。 Hashed/cssVar/zeroRuntime 有自己的继承;如有必要,请明确指定它们。空的 theme.components.Button 不会删除先前继承的 Button 标记。
组件配置(例如 ConfigProvider.button)的工作方式有所不同:其自己的记录(包括 {})会替换整个父记录。这些是默认的道具/样式,而不是令牌映射。完整示例位于 ConfigProvider 中。
读取 代币
对于原生元素,在初始化provider下的子组件时调用 theme.useToken:
<script lang="ts">
import { theme } from '@woodger/antd';
const current = theme.useToken();
</script>
<p style:color={current.token.colorTextSecondary}>Дополнительное пояснение</p>
该对象返回反应式 getter。读取标记中的 current.token 或 $derived;解构一次会保存该值的快照。
元件设计
特定系列的代币在 theme.components 中指定。语义 classNames/styles 指的是组件的各个部分,并在其页面上进行了描述;并非所有系列都具有相同的插槽和 render API。
请参见 ConfigProvider 组件令牌 和 按钮设计。size 和 variant 是单独的设置,不能替代主题标记。
CSS 变量和哈希值
theme.cssVar 接受 prefix/key 对象,例如 { prefix: 'product', key: 'editor-theme' }。前缀指定CSS变量的名称,key 是主题区域类。对于几个不同的主题,设置不同的强 key 或将其生成留给提供者。不同值的相同键可能会产生 CSS 冲突。
这些不是 prefixCls 组件。选定的变量前缀不会重命名按钮和弹出窗口。对于自定义 CSS,使用 theme.useToken 中的令牌很方便:这样您就不必手动猜测变量名称。
通用 ConfigProvider.prefixCls 更改 有联系的家庭 类。创建主题的源区域时,它还会设置后备命名空间 CSS 变量。继承的 cssVar.prefix 优先于新的 prefixCls;本地 cssVar.prefix 优先于两者。显式 cssVar.prefix:undefined 会清除继承的命名空间并返回当前提供程序的回退,即使 inherit=false 也是如此。
如果没有本地主题,则保留父 theme 范围和命名空间。在仅通过 mode/primary/compact 配置而没有源设置的树中,命名空间仍为 ant。 前缀示例 通过子 useToken 读取当前变量的引用;命名空间继承与令牌/算法/组件重置分开。
Hashed:false 返回一个空的 hashId,但不会替换其 CSS 隔离或删除所有范围类。 useToken.cssVar 的结果是转换后的令牌的映射,而不是 prefix/key 配置对象。
zeroRuntime 不是 CSS 提取命令。组件继续使用已发布的样式; React CSS-in-JS 工具说明不适用于此包。
静态计算和运动
theme.getDesignToken(config) 在没有上下文的情况下计算令牌,并且不会更新页面主题。用它来单独计算;对于提供者内部的反应式标记,useToken 是合适的。
token.motion:false 禁用支持的组件动画。分别考虑应用程序自己的动画和减少的运动首选项。不要指望更改颜色会自动纠正所有自定义表面的对比度。
代币的工作设置在ConfigProvider页面给出;圆点设计 - 风格。