Skip to content

Commit 27357d5

Browse files
committed
docs: update customize theme docs
1 parent 895d741 commit 27357d5

File tree

5 files changed

+243
-51
lines changed

5 files changed

+243
-51
lines changed

README-zh_CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ An enterprise-class UI components based on Ant Design and Vue.
2323

2424
</div>
2525

26+
[![](https://cdn-images-1.medium.com/max/2000/1*NIlj0-TdLMbo_hzSBP8tmg.png)](https://vuecomponent.github.io/ant-design-vue/)
27+
2628
[English](./README.md) | 简体中文 | [官网国内镜像](http://tangjinzhou.gitee.io/ant-design-vue/docs/vue/introduce-cn/)
2729

2830
<p align="center">

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ An enterprise-class UI components based on Ant Design and Vue.
2323

2424
</div>
2525

26+
[![](https://cdn-images-1.medium.com/max/2000/1*NIlj0-TdLMbo_hzSBP8tmg.png)](https://vuecomponent.github.io/ant-design-vue/)
27+
2628
English | [简体中文](./README-zh_CN.md) | [官网国内镜像](http://tangjinzhou.gitee.io/ant-design-vue/docs/vue/introduce-cn/)
2729

2830
<p align="center">

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

Lines changed: 130 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,155 @@ Ant Design allows you to customize some basic design aspects in order to meet th
77

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

10-
## Less variables
10+
## Ant Design Vue 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

14-
- [Default Variables](https://github.com/vueComponent/ant-design-vue/blob/master/components/style/themes/default.less)
14+
There are some major variables below, all less variables could be found in [Default Variables](https://github.com/vueComponent/ant-design-vue/blob/master/components/style/themes/default.less).
15+
16+
```less
17+
@primary-color: #1890ff; // primary color for all components
18+
@link-color: #1890ff; // link color
19+
@success-color: #52c41a; // success state color
20+
@warning-color: #faad14; // warning state color
21+
@error-color: #f5222d; // error state color
22+
@font-size-base: 14px; // major text font size
23+
@heading-color: rgba(0, 0, 0, .85); // heading text color
24+
@text-color: rgba(0, 0, 0, .65); // major text color
25+
@text-color-secondary : rgba(0, 0, 0, .45); // secondary text color
26+
@disabled-color : rgba(0, 0, 0, .25); // disable state color
27+
@border-radius-base: 4px; // major border radius
28+
@border-color-base: #d9d9d9; // major border color
29+
@box-shadow-base: 0 2px 8px rgba(0, 0, 0, .15); // major shadow for layers
30+
```
1531

1632
Please report an issue if the existing list of variables is not enough for you.
1733

1834
## How to do it
1935

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.
36+
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. We now introduce some popular way to do it depends on different workflow.
37+
38+
### Customize in webpack
39+
40+
We take a typical `webpack.config.js` file as example to customize it's [less-loader](https://github.com/webpack-contrib/less-loader) options.
41+
42+
```diff
43+
// webpack.config.js
44+
module.exports = {
45+
rules: [{
46+
test: /\.less$/,
47+
use: [{
48+
loader: 'style-loader',
49+
}, {
50+
loader: 'css-loader', // translates CSS into CommonJS
51+
}, {
52+
loader: 'less-loader', // compiles Less to CSS
53+
+ options: {
54+
+ modifyVars: {
55+
+ 'primary-color': '#1DA57A',
56+
+ 'link-color': '#1DA57A',
57+
+ 'border-radius-base': '2px',
58+
+ },
59+
+ javascriptEnabled: true,
60+
+ },
61+
}],
62+
// ...other rules
63+
}],
64+
// ...other config
65+
}
66+
```
2167

22-
### 1) Using `theme` property (recommended way)
68+
Note that do not exclude antd package in node_modules when using less-loader.
2369

24-
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:
25-
- example of directly specifying the custom values as an object:
26-
```js
27-
"theme": {
28-
"primary-color": "#1DA57A",
29-
},
30-
```
31-
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 vue cli 2
3271

33-
Note:
72+
Modify the `build/utils.js` file
73+
```diff
74+
// build/utils.js
75+
- less: generateLoaders('less'),
76+
+ less: generateLoaders('less', {
77+
+ modifyVars: {
78+
+ 'primary-color': '#1DA57A',
79+
+ 'link-color': '#1DA57A',
80+
+ 'border-radius-base': '2px',
81+
+ },
82+
+ javascriptEnabled: true,
83+
+ }),
3484

35-
- Importing styles from less files is necessary.
36-
- 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.
37-
- If you import styles from `'ant-design-vue/dist/antd.css'`, change it to `ant-design-vue/dist/antd.less`.
38-
- If you want to override `@icon-url`, the value must be contained in quotes like `"@icon-url": "'your-icon-font-path'"`.
85+
```
3986

40-
### 2) Overriding Less variables (alternative way)
87+
### Customize in vue cli 3
4188

42-
Override variables via less definition files.
89+
Create a new file `vue.config.js` in the project directory.
90+
```
91+
// vue.config.js
92+
module.exports = {
93+
configureWebpack: {
94+
module: {
95+
rules: [{
96+
test: /\.less$/,
97+
use: [{
98+
loader: 'style-loader',
99+
}, {
100+
loader: 'css-loader',
101+
}, {
102+
loader: 'less-loader',
103+
options: {
104+
modifyVars: {
105+
'primary-color': '#1DA57A',
106+
'link-color': '#1DA57A',
107+
'border-radius-base': '2px',
108+
},
109+
javascriptEnabled: true,
110+
},
111+
}],
112+
}],
113+
},
114+
}
115+
}
116+
```
43117

44-
Create a standalone less file like the one below, and import it in your project.
118+
### Customize in less file
45119

46-
```less
47-
@import "~ant-design-vue/dist/antd.less"; // import official less entry file
48-
@import "your-theme-file.less"; // override variables here
49-
```
120+
Another approach to customize theme is creating a `less` file within variables to override `antd.less`.
121+
122+
```css
123+
@import "~antd/dist/antd.less"; // Import Ant Design styles by less entry
124+
@import "your-theme-file.less"; // variables to override above
125+
```
50126

51127
Note: This way will load the styles of all components, regardless of your demand, which cause `style` option of `babel-plugin-import` not working.
52128

129+
## How to avoid modifying global styles?
130+
131+
Currently ant-design-vue is designed as a whole experience and modify global styles (eg `body` etc).
132+
If you need to integrate ant-design-vue as a part of an existing website, it's likely you want to prevent ant-design-vue to override global styles.
133+
134+
While there's no canonical way to do it, you can take one of the following paths :
135+
136+
### Configure webpack to load an alternale less file and scope global styles
137+
138+
It's possible to configure webpack to load an alternate less file:
139+
140+
```ts
141+
new webpack.NormalModuleReplacementPlugin( /node_modules\/antd\/lib\/style\/index\.less/, path.resolve(rootDir, 'src/myStylesReplacement.less') )
142+
143+
#antd { @import '~antd/lib/style/core/index.less'; @import '~antd/lib/style/themes/default.less'; }
144+
```
145+
146+
Where the src/myStylesReplacement.less file loads the same files as the index.less file, but loads them within the scope of a top-level selector : the result is that all of the "global" styles are being applied with the #antd scope.
147+
148+
### Use a postcss processor to scope all styles
149+
150+
See an example of usage with gulp and [postcss-prefixwrap](https://github.com/dbtedman/postcss-prefixwrap) : https://gist.github.com/sbusch/a90eafaf5a5b61c6d6172da6ff76ddaa
151+
152+
## Not working?
153+
154+
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.
155+
156+
- 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.
157+
- If you import styles from `'antd/dist/antd.css'`, change it to `antd/dist/antd.less`.
158+
53159
## Related Articles
54160

55161
- [How to Customize Ant Design with React & Webpack… the Missing Guide](https://medium.com/@GeoffMiller/how-to-customize-ant-design-with-react-webpack-the-missing-guide-c6430f2db10f)

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

Lines changed: 107 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,133 @@ Ant Design 设计规范上支持一定程度的样式定制,以满足业务和
77

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

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

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

14-
- [默认样式变量](https://github.com/vueComponent/ant-design-vue/blob/master/components/style/themes/default.less)
14+
以下是一些最常用的通用变量,所有样式变量可以在 [这里](https://github.com/vueComponent/ant-design-vue/blob/master/components/style/themes/default.less) 找到。
15+
16+
```less
17+
@primary-color: #1890ff; // 全局主色
18+
@link-color: #1890ff; // 链接色
19+
@success-color: #52c41a; // 成功色
20+
@warning-color: #faad14; // 警告色
21+
@error-color: #f5222d; // 错误色
22+
@font-size-base: 14px; // 主字号
23+
@heading-color: rgba(0, 0, 0, .85); // 标题色
24+
@text-color: rgba(0, 0, 0, .65); // 主文本色
25+
@text-color-secondary : rgba(0, 0, 0, .45); // 次文本色
26+
@disabled-color : rgba(0, 0, 0, .25); // 失效色
27+
@border-radius-base: 4px; // 组件/浮层圆角
28+
@border-color-base: #d9d9d9; // 边框色
29+
@box-shadow-base: 0 2px 8px rgba(0, 0, 0, .15); // 浮层阴影
30+
```
1531

1632
如果以上变量不能满足你的定制需求,可以给我们提 issue。
1733

1834
## 定制方式
1935

20-
我们使用 [modifyVars](http://lesscss.org/usage/#using-less-in-the-browser-modify-variables) 的方式来覆盖变量。
21-
在具体工程实践中,有 `package.theme``less` 两种方案,选择一种即可。
36+
我们使用 [modifyVars](http://lesscss.org/usage/#using-less-in-the-browser-modify-variables) 的方式来进行覆盖变量。
37+
下面将针对不同的场景提供一些常用的定制方式。
38+
39+
40+
### 在 webpack 中定制主题
41+
42+
我们以 webpack@4 为例进行说明,以下是一个 `webpack.config.js` 的典型例子,对 [less-loader](https://github.com/webpack-contrib/less-loader) 的 options 属性进行相应配置。
43+
44+
```diff
45+
// webpack.config.js
46+
module.exports = {
47+
rules: [{
48+
test: /\.less$/,
49+
use: [{
50+
loader: 'style-loader',
51+
}, {
52+
loader: 'css-loader', // translates CSS into CommonJS
53+
}, {
54+
loader: 'less-loader', // compiles Less to CSS
55+
+ options: {
56+
+ modifyVars: {
57+
+ 'primary-color': '#1DA57A',
58+
+ 'link-color': '#1DA57A',
59+
+ 'border-radius-base': '2px',
60+
+ },
61+
+ javascriptEnabled: true,
62+
+ },
63+
}],
64+
// ...other rules
65+
}],
66+
// ...other config
67+
}
68+
```
2269

70+
注意 less-loader 的处理范围不要过滤掉 `node_modules` 下的 antd 包。
2371

24-
### 1) theme 属性(推荐)
72+
### 在 vue cli 2中定制主题
2573

26-
配置在 `package.json``.webpackrc` 下的 `theme` 字段。theme 可以配置为一个对象或文件路径。
74+
修改`build/utils.js`文件
75+
```diff
76+
// build/utils.js
77+
- less: generateLoaders('less'),
78+
+ less: generateLoaders('less', {
79+
+ modifyVars: {
80+
+ 'primary-color': '#1DA57A',
81+
+ 'link-color': '#1DA57A',
82+
+ 'border-radius-base': '2px',
83+
+ },
84+
+ javascriptEnabled: true,
85+
+ }),
2786

28-
```js
29-
"theme": {
30-
"primary-color": "#1DA57A",
31-
},
3287
```
33-
定义 `theme` 属性时,需要配合使用`less-loader``modifyVars` 配置来覆盖原来的样式变量。
34-
可以参考 [atool-build 中 less-loader 的 webpack 相关配置](https://github.com/ant-tool/atool-build/blob/a4b3e3eec4ffc09b0e2352d7f9d279c4c28fdb99/src/getWebpackCommonConfig.js#L131-L138)
3588

36-
注意:
89+
### 在 vue cli 3中定制主题
3790

38-
- 样式必须加载 less 格式。
39-
- 如果你在使用 [babel-plugin-import](https://github.com/ant-design/babel-plugin-import)`style` 配置来引入样式,需要将配置值从 `'css'` 改为 `true`,这样会引入 less 文件。
40-
- 如果你是通过 `'ant-design-vue/dist/antd.css'` 引入样式的,改为 `ant-design-vue/dist/antd.less`
41-
- 如果要覆盖 `@icon-url` 变量,内容需要包括引号 `"@icon-url": "'your-icon-font-path'"`
91+
项目更目录下新建文件`vue.config.js`
92+
```
93+
// vue.config.js
94+
module.exports = {
95+
configureWebpack: {
96+
module: {
97+
rules: [{
98+
test: /\.less$/,
99+
use: [{
100+
loader: 'style-loader',
101+
}, {
102+
loader: 'css-loader',
103+
}, {
104+
loader: 'less-loader',
105+
options: {
106+
modifyVars: {
107+
'primary-color': '#1DA57A',
108+
'link-color': '#1DA57A',
109+
'border-radius-base': '2px',
110+
},
111+
javascriptEnabled: true,
112+
},
113+
}],
114+
}],
115+
},
116+
}
117+
}
118+
```
42119

43-
### 2) less
120+
### 配置 less 变量文件
121+
122+
另外一种方式是建立一个单独的 `less` 变量文件,引入这个文件覆盖 `antd.less` 里的变量。
123+
124+
```css
125+
@import "~antd/dist/antd.less"; // 引入官方提供的 less 样式入口文件
126+
@import "your-theme-file.less"; // 用于覆盖上面定义的变量
127+
```
44128

45-
用 less 文件进行变量覆盖
129+
注意,这种方式已经载入了所有组件的样式,不需要也无法和按需加载插件 `babel-plugin-import` 的 `style` 属性一起使用
46130

47-
建立一个单独的 `less` 文件如下,再引入这个文件。
131+
## 没有生效?
48132

49-
```less
50-
@import "~ant-design-vue/dist/antd.less"; // 引入官方提供的 less 样式入口文件
51-
@import "your-theme-file.less"; // 用于覆盖上面定义的变量
52-
```
133+
注意样式必须加载 less 格式,一个常见的问题就是引入了多份样式,less 的样式被 css 的样式覆盖了。
53134

54-
注意:这种方式已经载入了所有组件的样式,不需要也无法和按需加载插件 `babel-plugin-import``style` 属性一起使用。
135+
- 如果你在使用 [babel-plugin-import](https://github.com/ant-design/babel-plugin-import) 的 `style` 配置来引入样式,需要将配置值从 `'css'` 改为 `true`,这样会引入 less 文件。
136+
- 如果你是通过 `'antd/dist/antd.css'` 引入样式的,改为 `antd/dist/antd.less`。
55137

56138
## 社区教程 for Antd React
57139

docs/vue/introduce.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Ant Design of Vue
33

4-
这里是 Ant Design 3.X 的 Vue 实现,开发和服务于企业级后台产品。
4+
这里是 Ant Design 的 Vue 实现,开发和服务于企业级后台产品。
55

66
<div class="pic-plus">
77
<img width="150" src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg">
@@ -134,7 +134,7 @@ import 'ant-design-vue/dist/antd.css'; // or 'ant-design-vue/dist/antd.less'
134134

135135
众所周知,Ant Design作为一门设计语言面世,经历过多年的迭代和积累,它对UI的设计思想已经成为一套事实标准,受到众多前端开发者及企业的追捧和喜爱,也是React开发者手中的神兵利器。希望ant-design-vue能够让Vue开发者也享受到Ant Design的优秀设计。
136136

137-
ant-design-vue是Ant Design 3.X的Vue实现,该组件库目前以实现Ant Design React版80%以上的组件,组件的风格与Ant Design 3.4.0版本保持同步,组件的html结构和css样式也保持一致,真正做到了样式0修改,组件API也尽量保持了一致。
137+
ant-design-vue 是 Ant Design 的Vue实现,组件的风格与Ant Design保持同步,组件的html结构和css样式也保持一致,真正做到了样式0修改,组件API也尽量保持了一致。
138138

139139
Ant Design Vue 致力于提供给程序员**愉悦**的开发体验。
140140

0 commit comments

Comments
 (0)