Skip to content

Commit 6fd5e95

Browse files
committed
docs: renew <Customize Theme> documentation
1 parent 60cff5d commit 6fd5e95

File tree

2 files changed

+108
-44
lines changed

2 files changed

+108
-44
lines changed

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

+56-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Ant Design allows you to customize some basic design aspects in order to meet th
77

88
![customized themes](https://zos.alipayobjects.com/rmsportal/zTFoszBtDODhXfLAazfSpYbSLSEeytoG.png)
99

10-
## Less variables
10+
## Ant Design Less variables
1111

1212
We are using [Less](http://lesscss.org/) as the development language for styling. A set of less variables are defined for each design aspect that can be customized to your needs.
1313

@@ -17,46 +17,80 @@ Please report an issue if the existing list of variables is not enough for you.
1717

1818
## How to do it
1919

20-
We recommend [modifyVars](http://lesscss.org/usage/#using-less-in-the-browser-modify-variables) to override the default values of the variables. There are two ways to achieve it in practice.
20+
We will use [modifyVars](http://lesscss.org/usage/#using-less-in-the-browser-modify-variables) provided by less.js to override the default values of the variables, You can use this [example](https://github.com/ant-design/create-react-app-antd) as a live playground. We now introduce some popular way to do it depends on different workflow.
21+
22+
### Customize in webpack
23+
24+
We take a typical `webpack.config.js` file as example to customize it's [less-loader](https://github.com/webpack-contrib/less-loader) options.
25+
26+
```diff
27+
// webpack.config.js
28+
module.exports = {
29+
rules: [{
30+
test: /\.less$/,
31+
use: [{
32+
loader: 'style-loader',
33+
}, {
34+
loader: 'css-loader', // translates CSS into CommonJS
35+
}, {
36+
loader: 'less-loader', // compiles Less to CSS
37+
+ options: {
38+
+ modifyVars: {
39+
+ 'primary-color': '#1DA57A',
40+
+ 'link-color': '#1DA57A',
41+
+ 'border-radius-base': '2px',
42+
+ },
43+
+ javascriptEnabled: true,
44+
+ },
45+
}],
46+
// ...other rules
47+
}],
48+
// ...other config
49+
}
50+
```
51+
52+
Note that do not exclude antd package in node_modules when using less-loader.
2153

22-
You can use this [example](https://github.com/ant-design/antd-init/tree/master/examples/customize-antd-theme) as a playground.
54+
### Customize in roadhug or Umi
2355

24-
### 1) Using `theme` property (recommended way)
56+
You can easily use `theme` field in `.webpackrc` file of your project root directory if you are using 如果你在使用 [roadhug](https://github.com/sorrycc/roadhog) or [Umi](http://umijs.org/), which could be a object or a javascript file path.
2557

26-
Specify the `theme` property in the `package.json` or `.webpackrc` file, whose value can be either an object or the path to a JS file that contains the custom values of specific variables:
27-
- example of directly specifying the custom values as an object:
2858
```js
2959
"theme": {
3060
"primary-color": "#1DA57A",
3161
},
3262
```
33-
- example of specifying a [file path](https://github.com/ant-design/antd-init/blob/master/examples/customize-antd-theme/theme.js) to a JS file:
63+
64+
Or just [a javascript file path](https://github.com/ant-design/ant-design-pro/blob/3c2a056ef0dac06ce3b4389192691bb1f5c448e2/.webpackrc.js#L19):
65+
3466
```js
3567
"theme": "./theme.js",
3668
```
3769

38-
This approach is available only when using [antd-init](https://github.com/ant-design/antd-init) or [dva-cli](https://github.com/dvajs/dva-cli). If you choose other boilerplates, you can write a webpack config about [less-loader modifyVars](https://github.com/webpack/less-loader#less-options) like [atool-build ](https://github.com/ant-tool/atool-build/blob/a4b3e3eec4ffc09b0e2352d7f9d279c4c28fdb99/src/getWebpackCommonConfig.js#L131-L138) does.
70+
### Customize in create-react-app
3971

40-
Note:
72+
Follow [Use in create-react-app](/docs/react/create-react-app).
4173

42-
- Importing styles from less files is necessary.
43-
- If you import styles by specifying the `style` option of [babel-plugin-import](https://github.com/ant-design/babel-plugin-import), change it from `'css'` to `true`, which will import the `less` version of antd.
44-
- If you import styles from `'antd/dist/antd.css'`, change it to `antd/dist/antd.less`.
45-
- When using `dva-cli@0.7.0+`, you should add the `theme` block to [.roadhogrc](https://github.com/dvajs/dva-example-user-dashboard/commit/d6da33b3a6e18eb7f003752a4b00b5a660747c31) instead of `package.json`.
46-
- If you want to override `@icon-url`, the value must be contained in quotes like `"@icon-url": "'your-icon-font-path'"` ([A fix sample](https://github.com/visvadw/dvajs-user-dashboard/pull/2)).
74+
### Customize in less file
4775

48-
### 2) Overriding Less variables (alternative way)
76+
Another approach to customize theme is creating a `less` file within variables to override `antd.less`.
4977

50-
Override variables via less definition files.
78+
```css
79+
@import "~antd/dist/antd.less"; // Import Ant Design styles by less entry
80+
@import "your-theme-file.less"; // variables to override above
81+
```
5182

52-
Create a standalone less file like the one below, and import it in your project.
83+
Note: This way will load the styles of all components, regardless of your demand, which cause `style` option of `babel-plugin-import` not working.
5384

54-
```css
55-
@import "~antd/dist/antd.less"; // import official less entry file
56-
@import "your-theme-file.less"; // override variables here
57-
```
85+
## Not working?
5886

59-
Note: This way will load the styles of all components, regardless of your demand, which cause `style` option of `babel-plugin-import` not working.
87+
You must import styles as less format. A common mistake would be importing multiple copied of styles that some of them are css format to override the less styles.
88+
89+
- If you import styles by specifying the `style` option of [babel-plugin-import](https://github.com/ant-design/babel-plugin-import), change it from `'css'` to `true`, which will import the `less` version of antd.
90+
- If you import styles from `'antd/dist/antd.css'`, change it to `antd/dist/antd.less`.
91+
- When using `dva-cli@0.7.0+`, you should add the `theme` block to [.roadhogrc](https://github.com/dvajs/dva-example-user-dashboard/commit/d6da33b3a6e18eb7f003752a4b00b5a660747c31) instead of `package.json`.
92+
93+
If you want to override `@icon-url`, the value must be contained in quotes like `"@icon-url": "'your-icon-font-path'"` ([A fix sample](https://github.com/visvadw/dvajs-user-dashboard/pull/2)).
6094

6195
## Related Articles
6296

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

+52-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Ant Design 设计规范上支持一定程度的样式定制,以满足业务和
77

88
![一些配置好的主题](https://zos.alipayobjects.com/rmsportal/zTFoszBtDODhXfLAazfSpYbSLSEeytoG.png)
99

10-
## 样式变量
10+
## Ant Design 的样式变量
1111

1212
antd 的样式使用了 [Less](http://lesscss.org/) 作为开发语言,并定义了一系列全局/组件的样式变量,你可以根据需求进行相应调整。
1313

@@ -17,49 +17,79 @@ antd 的样式使用了 [Less](http://lesscss.org/) 作为开发语言,并定
1717

1818
## 定制方式
1919

20-
我们使用 [modifyVars](http://lesscss.org/usage/#using-less-in-the-browser-modify-variables) 的方式来覆盖变量。
21-
在具体工程实践中,有 `package.theme``less` 两种方案,选择一种即可。
20+
原理上是使用 less 提供的 [modifyVars](http://lesscss.org/usage/#using-less-in-the-browser-modify-variables) 的方式进行覆盖变量,可以在本地运行 [例子](https://github.com/ant-design/create-react-app-antd) 查看定制效果。下面将针对不同的场景提供一些常用的定制方式。
21+
22+
### 在 webpack 中定制主题
23+
24+
我们以 webpack@4 为例进行说明,以下是一个 `webpack.config.js` 的典型例子,对 [less-loader](https://github.com/webpack-contrib/less-loader) 的 options 属性进行相应配置。
25+
26+
```diff
27+
// webpack.config.js
28+
module.exports = {
29+
rules: [{
30+
test: /\.less$/,
31+
use: [{
32+
loader: 'style-loader',
33+
}, {
34+
loader: 'css-loader', // translates CSS into CommonJS
35+
}, {
36+
loader: 'less-loader', // compiles Less to CSS
37+
+ options: {
38+
+ modifyVars: {
39+
+ 'primary-color': '#1DA57A',
40+
+ 'link-color': '#1DA57A',
41+
+ 'border-radius-base': '2px',
42+
+ },
43+
+ javascriptEnabled: true,
44+
+ },
45+
}],
46+
// ...other rules
47+
}],
48+
// ...other config
49+
}
50+
```
2251

23-
可以在本地运行 [例子](https://github.com/ant-design/antd-init/tree/master/examples/customize-antd-theme) 查看定制效果
52+
注意 less-loader 的处理范围不要过滤掉 `node_modules` 下的 antd
2453

25-
### 1) theme 属性(推荐)
54+
### 在 roadhug 或 Umi 里配置主题
2655

27-
配置在 `package.json``.webpackrc` 下的 `theme` 字段。theme 可以配置为一个对象或文件路径。
56+
如果你在使用 [roadhug](https://github.com/sorrycc/roadhog) 或者 [Umi](http://umijs.org/),那么可以很方便地在项目根目录的 `.webpackrc` 文件中 `theme` 字段进行主题配置。`theme` 可以配置为一个对象或文件路径。
2857

2958
```js
3059
"theme": {
3160
"primary-color": "#1DA57A",
3261
},
3362
```
3463

35-
或者 [一个 js 文件](https://github.com/ant-design/antd-init/blob/master/examples/customize-antd-theme/theme.js)
64+
或者 [一个 js 文件](https://github.com/ant-design/ant-design-pro/blob/3c2a056ef0dac06ce3b4389192691bb1f5c448e2/.webpackrc.js#L19)
3665

3766
```js
3867
"theme": "./theme.js",
3968
```
4069

41-
定义 `theme` 属性时,需要配合使用([antd-init](https://github.com/ant-design/antd-init)[dva-cli](https://github.com/dvajs/dva-cli)。如果你使用的是其他脚手架,可以参考 [atool-build 中 less-loader 的 webpack 相关配置 ](https://github.com/ant-tool/atool-build/blob/a4b3e3eec4ffc09b0e2352d7f9d279c4c28fdb99/src/getWebpackCommonConfig.js#L131-L138),利用 [less-loader](https://github.com/webpack/less-loader#less-options)`modifyVars` 配置来覆盖原来的样式变量。
70+
### 在 create-react-app 中定制主题
71+
72+
参考 [在 create-react-app 中使用](/docs/react/create-react-app) 进行配置即可。
4273

43-
注意:
74+
### 配置 less 变量文件
4475

45-
- 样式必须加载 less 格式。
46-
- 如果你在使用 [babel-plugin-import](https://github.com/ant-design/babel-plugin-import)`style` 配置来引入样式,需要将配置值从 `'css'` 改为 `true`,这样会引入 less 文件。
47-
- 如果你是通过 `'antd/dist/antd.css'` 引入样式的,改为 `antd/dist/antd.less`
48-
- `dva-cli@0.7.0+``theme` 属性需要写在 [.roadhogrc](https://github.com/dvajs/dva-example-user-dashboard/commit/d6da33b3a6e18eb7f003752a4b00b5a660747c31) 文件里。
49-
- 如果要覆盖 `@icon-url` 变量,内容需要包括引号 `"@icon-url": "'your-icon-font-path'"`[修正示例](https://github.com/visvadw/dvajs-user-dashboard/pull/2))。
76+
另外一种方式是建立一个单独的 `less` 变量文件,引入这个文件覆盖 `antd.less` 里的变量。
77+
78+
```css
79+
@import "~antd/dist/antd.less"; // 引入官方提供的 less 样式入口文件
80+
@import "your-theme-file.less"; // 用于覆盖上面定义的变量
81+
```
5082

51-
### 2) less
83+
注意,这种方式已经载入了所有组件的样式,不需要也无法和按需加载插件 `babel-plugin-import` 的 `style` 属性一起使用。
5284

53-
用 less 文件进行变量覆盖。
85+
## 没有生效?
5486

55-
建立一个单独的 `less` 文件如下,再引入这个文件
87+
注意样式必须加载 less 格式,一个常见的问题就是引入了多份样式,less 的样式被 css 的样式覆盖了
5688

57-
```css
58-
@import "~antd/dist/antd.less"; // 引入官方提供的 less 样式入口文件
59-
@import "your-theme-file.less"; // 用于覆盖上面定义的变量
60-
```
89+
- 如果你在使用 [babel-plugin-import](https://github.com/ant-design/babel-plugin-import) 的 `style` 配置来引入样式,需要将配置值从 `'css'` 改为 `true`,这样会引入 less 文件。
90+
- 如果你是通过 `'antd/dist/antd.css'` 引入样式的,改为 `antd/dist/antd.less`。
6191

62-
注意:这种方式已经载入了所有组件的样式,不需要也无法和按需加载插件 `babel-plugin-import` 的 `style` 属性一起使用
92+
如果要覆盖 `@icon-url` 变量,内容需要包括引号 `"@icon-url": "'your-icon-font-path'"`([修正示例](https://github.com/visvadw/dvajs-user-dashboard/pull/2))
6393

6494
## 社区教程
6595

0 commit comments

Comments
 (0)