Browse Source

docs: customize theme en (#37879)

pull/37650/head
MadCcc 2 years ago
committed by GitHub
parent
commit
2d605bfa3f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 84
      docs/react/customize-theme-v5.en-US.md
  2. 2
      docs/react/customize-theme-v5.zh-CN.md

84
docs/react/customize-theme-v5.en-US.md

@ -1,24 +1,24 @@
--- ---
order: 7 order: 7
title: 定制主题 title: Customize Theme
--- ---
Ant Design 设计规范和技术上支持灵活的样式定制,以满足业务和品牌上多样化的视觉需求,包括但不限于全局样式(主色、圆角、边框)和指定组件的视觉定制。 Ant Design allows you to customize our design tokens to satisfy UI diversity from business or brand requirements, including primary color, border radius, border color, etc.
在 5.0 版本的 Ant Design 中,我们提供了一套全新的定制主题方案。不同于 4.x 版本的 less 和 CSS 变量,有了 CSS-in-JS 的加持后,动态主题的能力也得到了加强,包括但不限于: In version 5.0, we provide a new way to customize themes. Different from the less and CSS variables of the 4.x version, with CSS-in-JS, the ability of theming has also been enhanced, including but not limited to:
1. 支持动态切换主题 1. Switching theme dynamically
2. 支持同时存在多个主题 2. Multiple themes
3. 支持针对某个/某些组件主题变量 3. Customizing theme variables for some component
4. ... 4. ...
## 在 ConfigProvider 中配置主题 ## Customize theme with `ConfigProvider`
在 5.0 版本中我们把影响主题的最小元素称为 **Design Token**。通过修改 Design Token,我们可以呈现出各种各样的主题或者组件。 In version 5.0 we call the smallest element that affects the theme **Design Token**. By modifying the Design Token, we can present various themes or components.
### 修改主题变量 ### Customize Design Token
通过在 ConfigProvider 中传入 `theme`,可以配置主题。在升级 v5 后,将默认使用 v5 的主题,以下是将配置主题示例: You can pass `theme` to ConfigProvider to customize theme. After migrate to V5, theme of V5 will be applied by default. Here's a simple example:
```tsx ```tsx
import React from 'react'; import React from 'react';
@ -39,13 +39,13 @@ const App: React.FC = () => (
export default App; export default App;
``` ```
这将会得到一个以 <div style="display: inline-block; width: 16px; height: 16px; border-radius: 4px; background: #00b96b; vertical-align: text-bottom;" /> `#00b96b` 为主色的主题,以 Button 组件为例可以看到相应的变化: You will get a theme with primary color <div style="display: inline-block; width: 16px; height: 16px; border-radius: 4px; background: #00b96b; vertical-align: text-bottom;" /> `#00b96b`. And we can see the change in Button:
![themed button](https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*CbF_RJfKEiwAAAAAAAAAAAAAARQnAQ) ![themed button](https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*CbF_RJfKEiwAAAAAAAAAAAAAARQnAQ)
### 修改组件变量 (Component Token) ### Customize Component Token
除了整体的 Design Token,各个组件也会开放自己的 Component Token 来实现针对组件的样式定制能力,不同的组件之间不会相互影响。同样地,也可以通过这种方式来覆盖组件的其他 Design Token。 In addition to Design Token, each component will also have its own Component Token to achieve style customization capabilities for components, and different components will not affect each other. Similarly, other Design Token of components can also be overridden in this way.
```tsx ```tsx
import React from 'react'; import React from 'react';
@ -69,19 +69,19 @@ const App: React.FC = () => (
export default App; export default App;
``` ```
通过这种方式,我们可以仅将 Radio 组件的主色改为 <div style="display: inline-block; width: 16px; height: 16px; border-radius: 4px; background: #00b96b; vertical-align: text-bottom;" /> `#00b96b`,而不会影响其他组件。 In this way, we changed the primary color of Radio to <div style="display: inline-block; width: 16px; height: 16px; border-radius: 4px; background: #00b96b; vertical-align: text-bottom;" /> `#00b96b`, and Checkbox is not affected.
![component token](https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*EMY0QrHFDjsAAAAAAAAAAAAAARQnAQ) ![component token](https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*EMY0QrHFDjsAAAAAAAAAAAAAARQnAQ)
## 动态主题的其他使用方式 ## Other Ways to Use Dynamic Themes
### 动态切换 ### Switch Themes Dynamically
在 v5 中,动态切换主题对用户来说是非常简单的,你可以在任何时候通过 `ConfigProvider``theme` 属性来动态切换主题,而不需要任何额外配置。 In v5, dynamically switching themes is very simple for users, you can dynamically switch themes at any time through the `theme` property of `ConfigProvider` without any additional configuration.
### 局部主题 ### Local Theme
可以嵌套使用 `ConfigProvider` 来实现局部主题的更换。在子主题中未被改变的 Design Token 将会继承父主题。 By nesting `ConfigProvider` you can apply local theme to some parts of your page. Design Tokens that have not been changed in the child theme will inherit the parent theme.
```tsx ```tsx
import React from 'react'; import React from 'react';
@ -111,9 +111,9 @@ const App: React.FC = () => (
export default App; export default App;
``` ```
### 使用 Design Token ### Consume Design Token
如果你希望使用当前主题下的 Design Token,我们提供了 `useToken` 这个 hook 来获取 Design Token。 If you want to consume the Design Token under the current theme, we provide `useToken` hook to get Design Token.
```tsx ```tsx
import React from 'react'; import React from 'react';
@ -130,17 +130,17 @@ const App: React.FC = () => {
export default App; export default App;
``` ```
## 进阶使用 ## Advanced
在 Design Token 中我们提供了一套更加贴合设计的三层结构,将 Design Token 拆解为 Seed Token、Map Token 和 Alias Token 三部分。这三组 Token 并不是简单的分组,而是一个三层的派生关系,由 Seed Token 派生 Map Token,再由 Map Token 派生 Alias Token。在大部分情况下,使用 Seed Token 就可以满足定制主题的需要。但如果您需要更高程度的主题定制,您需要了解 antd 中 Design Token 的生命周期。 In Design Token, we provide a three-layer structure that is more suitable for the design, and disassemble the Design Token into three parts: Seed Token, Map Token and Alias Token. These three groups of Tokens are not simple groupings, but a three-layer derivation relationship. Map Tokens are derived from Seed Tokens, and Alias Tokens are derived from Map Tokens. In most cases, using Seed Tokens is sufficient for custom themes. But if you need a higher degree of theme customization, you need to understand the life cycle of Design Token in antd.
### 演变过程 ### Life Cycle of Design Token
![token](https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*uF3kTrY4InUAAAAAAAAAAAAAARQnAQ) ![token](https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*uF3kTrY4InUAAAAAAAAAAAAAARQnAQ)
### 基础变量(Seed Token ### Seed Token
Seed Token 意味着所有设计意图的起源。比如我们可以通过改变 `colorPrimary` 来改变主题色,antd 内部的算法会自动的根据 Seed Token 计算出对应的一系列颜色并应用: Seed Token means the origin of all design intent. For example, we can change the theme color by changing `colorPrimary`, and the algorithm inside antd will automatically calculate and apply a series of corresponding colors according to the Seed Token:
```tsx ```tsx
const theme = { const theme = {
@ -150,9 +150,9 @@ const theme = {
}; };
``` ```
### 梯度变量(Map Token ### Map Token
Map Token 是基于 Seed 派生的梯度变量。定制 Map Token 推荐通过 `theme.algorithm` 来实现,这样可以保证 Map Token 之间的梯度关系。也可以通过 `theme.token` 覆盖,用于单独修改一些 map token 的值。 Map Token is a gradient variable derived from Seed. It is recommended to implement custom Map Token through `theme.algorithm`, which can ensure the gradient relationship between Map Tokens. It can also be overridden by `theme.token` to modify the value of some map tokens individually.
```tsx ```tsx
const theme = { const theme = {
@ -162,9 +162,9 @@ const theme = {
}; };
``` ```
### 别名变量(Alias Token ### Alias Token
Alias Token 用于批量控制某些共性组件的样式,基本上是 Map Token 别名,或者特殊处理过的 Map Token。 Alias Token is used to control the style of some common components in batches, which is basically a Map Token alias, or a specially processed Map Token.
```tsx ```tsx
const theme = { const theme = {
@ -174,9 +174,9 @@ const theme = {
}; };
``` ```
### 基本算法(algorithm) ### Algorithm
基本算法用于将 Seed Token 展开为 Map Token,比如由一个基本色算出一个梯度色板,或者由一个基本的圆角算出各种大小的圆角。在 v5 中,我们默认提供了三种算法,分别是默认算法(defaultAlgorithm)、暗色算法(darkAlgorithm)和紧凑算法(compactAlgorithm)。算法可以任意地组合使用,比如可以将暗色算法和紧凑算法组合使用,得到一个暗色和紧凑相结合的主题。 The basic algorithm is used to expand the Seed Token into a Map Token, such as calculating a gradient color palette from a basic color, or calculating rounded corners of various sizes from a basic rounded corner. In v5, we provide three algorithms by default, namely the default algorithm (defaultAlgorithm), the dark algorithm (darkAlgorithm) and the compact algorithm (compactAlgorithm). Algorithms can be used in any combination, for example, dark and compact algorithms can be combined to get a dark and compact theme.
```tsx ```tsx
import { theme } from 'antd'; import { theme } from 'antd';
@ -435,24 +435,18 @@ const theme = {
| screenXXLMin | - | `number` | `1599` | | screenXXLMin | - | `number` | `1599` |
| screenXXLMax | - | `number` | `1600` | | screenXXLMax | - | `number` | `1600` |
## 调试主题 ## How to Debug your Theme
我们提供了帮助用户调试主题的工具:[主题编辑器](https://ant-design.github.io/antd-token-previewer/~demos/docs-theme-editor-simple) We provide tools to help users debug themes: [Theme Editor](https://ant-design.github.io/antd-token-previewer/~demos/docs-theme-editor-simple)
你可以使用此工具自由地修改 Design Token,以达到您对主题的期望。 You can use this tool to freely modify Design Token to meet your theme expectations.
## 主题展示 ## Theme Presets
- [Ant Design 4.x 主题](https://ant-design.github.io/antd-token-previewer/~demos/docs-v4-theme) - [Ant Design 4.x](https://ant-design.github.io/antd-token-previewer/~demos/docs-v4-theme)
## FAQ ## FAQ
### 为什么 `theme``undefined` 变为对象或者变为 `undefined` 时组件重新 mount 了? ### Why component re-mounted when `theme` changed from `undefined` to some object or to `undefined`?
在 ConfigProvider 中我们通过 `DesignTokenContext` 传递 context,`theme` 为 `undefined` 时不会套一层 Provider,所以从无到有或者从有到无时 React 的 VirtualDOM 结构变化,导致组件重新 mount。解决方法:将 `undefined` 替换为空对象 `{}` 即可。
<div style="display: none;"> In ConfigProvider, we pass context through `DesignTokenContext`. When `theme` is `undefined`, a layer of Provider will not be set, so React VirtualDOM structure changes from scratch or from existence to nothing, causing components to be re-mounted. Solution: Replace `undefined` with an empty object `{}`.
- 在 Umi 4 中定制主题
- 与 V4 定制主题的区别
- less 变量与 Design Token 对照表
</div>

2
docs/react/customize-theme-v5.zh-CN.md

@ -9,7 +9,7 @@ Ant Design 设计规范和技术上支持灵活的样式定制,以满足业务
1. 支持动态切换主题; 1. 支持动态切换主题;
2. 支持同时存在多个主题; 2. 支持同时存在多个主题;
3. 支持针对某个/某些组件主题变量; 3. 支持针对某个/某些组件修改主题变量;
4. ... 4. ...
## 在 ConfigProvider 中配置主题 ## 在 ConfigProvider 中配置主题

Loading…
Cancel
Save