From 27357d5fedcabac05ae19a38b1a1fd4aee31f077 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Tue, 18 Sep 2018 10:18:52 +0800 Subject: [PATCH] docs: update customize theme docs --- README-zh_CN.md | 2 + README.md | 2 + docs/vue/customize-theme.en-US.md | 152 +++++++++++++++++++++++++----- docs/vue/customize-theme.zh-CN.md | 132 +++++++++++++++++++++----- docs/vue/introduce.zh-CN.md | 4 +- 5 files changed, 242 insertions(+), 50 deletions(-) diff --git a/README-zh_CN.md b/README-zh_CN.md index 83b40693e..0a020574d 100644 --- a/README-zh_CN.md +++ b/README-zh_CN.md @@ -23,6 +23,8 @@ An enterprise-class UI components based on Ant Design and Vue. +[![](https://cdn-images-1.medium.com/max/2000/1*NIlj0-TdLMbo_hzSBP8tmg.png)](https://vuecomponent.github.io/ant-design-vue/) + [English](./README.md) | 简体中文 | [官网国内镜像](http://tangjinzhou.gitee.io/ant-design-vue/docs/vue/introduce-cn/)

diff --git a/README.md b/README.md index 9fdd926d1..8e1535312 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ An enterprise-class UI components based on Ant Design and Vue. +[![](https://cdn-images-1.medium.com/max/2000/1*NIlj0-TdLMbo_hzSBP8tmg.png)](https://vuecomponent.github.io/ant-design-vue/) + English | [简体中文](./README-zh_CN.md) | [官网国内镜像](http://tangjinzhou.gitee.io/ant-design-vue/docs/vue/introduce-cn/)

diff --git a/docs/vue/customize-theme.en-US.md b/docs/vue/customize-theme.en-US.md index 6df8e399e..333ace169 100644 --- a/docs/vue/customize-theme.en-US.md +++ b/docs/vue/customize-theme.en-US.md @@ -7,49 +7,155 @@ Ant Design allows you to customize some basic design aspects in order to meet th ![](https://zos.alipayobjects.com/rmsportal/zTFoszBtDODhXfLAazfSpYbSLSEeytoG.png) -## Less variables +## Ant Design Vue Less variables 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. -- [Default Variables](https://github.com/vueComponent/ant-design-vue/blob/master/components/style/themes/default.less) +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). + +```less +@primary-color: #1890ff; // primary color for all components +@link-color: #1890ff; // link color +@success-color: #52c41a; // success state color +@warning-color: #faad14; // warning state color +@error-color: #f5222d; // error state color +@font-size-base: 14px; // major text font size +@heading-color: rgba(0, 0, 0, .85); // heading text color +@text-color: rgba(0, 0, 0, .65); // major text color +@text-color-secondary : rgba(0, 0, 0, .45); // secondary text color +@disabled-color : rgba(0, 0, 0, .25); // disable state color +@border-radius-base: 4px; // major border radius +@border-color-base: #d9d9d9; // major border color +@box-shadow-base: 0 2px 8px rgba(0, 0, 0, .15); // major shadow for layers +``` Please report an issue if the existing list of variables is not enough for you. ## How to do it -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. +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. -### 1) Using `theme` property (recommended way) +### Customize in webpack -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: -- example of directly specifying the custom values as an object: -```js -"theme": { - "primary-color": "#1DA57A", -}, +We take a typical `webpack.config.js` file as example to customize it's [less-loader](https://github.com/webpack-contrib/less-loader) options. + +```diff +// webpack.config.js +module.exports = { + rules: [{ + test: /\.less$/, + use: [{ + loader: 'style-loader', + }, { + loader: 'css-loader', // translates CSS into CommonJS + }, { + loader: 'less-loader', // compiles Less to CSS ++ options: { ++ modifyVars: { ++ 'primary-color': '#1DA57A', ++ 'link-color': '#1DA57A', ++ 'border-radius-base': '2px', ++ }, ++ javascriptEnabled: true, ++ }, + }], + // ...other rules + }], + // ...other config +} ``` -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. -Note: +Note that do not exclude antd package in node_modules when using less-loader. -- Importing styles from less files is necessary. - - 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. - - If you import styles from `'ant-design-vue/dist/antd.css'`, change it to `ant-design-vue/dist/antd.less`. -- If you want to override `@icon-url`, the value must be contained in quotes like `"@icon-url": "'your-icon-font-path'"`. +### Customize in vue cli 2 -### 2) Overriding Less variables (alternative way) +Modify the `build/utils.js` file +```diff +// build/utils.js +- less: generateLoaders('less'), ++ less: generateLoaders('less', { ++ modifyVars: { ++ 'primary-color': '#1DA57A', ++ 'link-color': '#1DA57A', ++ 'border-radius-base': '2px', ++ }, ++ javascriptEnabled: true, ++ }), -Override variables via less definition files. +``` -Create a standalone less file like the one below, and import it in your project. +### Customize in vue cli 3 - ```less - @import "~ant-design-vue/dist/antd.less"; // import official less entry file - @import "your-theme-file.less"; // override variables here - ``` +Create a new file `vue.config.js` in the project directory. +``` +// vue.config.js +module.exports = { + configureWebpack: { + module: { + rules: [{ + test: /\.less$/, + use: [{ + loader: 'style-loader', + }, { + loader: 'css-loader', + }, { + loader: 'less-loader', + options: { + modifyVars: { + 'primary-color': '#1DA57A', + 'link-color': '#1DA57A', + 'border-radius-base': '2px', + }, + javascriptEnabled: true, + }, + }], + }], + }, + } +} +``` + +### Customize in less file + +Another approach to customize theme is creating a `less` file within variables to override `antd.less`. + +```css +@import "~antd/dist/antd.less"; // Import Ant Design styles by less entry +@import "your-theme-file.less"; // variables to override above +``` Note: This way will load the styles of all components, regardless of your demand, which cause `style` option of `babel-plugin-import` not working. +## How to avoid modifying global styles? + +Currently ant-design-vue is designed as a whole experience and modify global styles (eg `body` etc). +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. + +While there's no canonical way to do it, you can take one of the following paths : + +### Configure webpack to load an alternale less file and scope global styles + +It's possible to configure webpack to load an alternate less file: + +```ts +new webpack.NormalModuleReplacementPlugin( /node_modules\/antd\/lib\/style\/index\.less/, path.resolve(rootDir, 'src/myStylesReplacement.less') ) + +#antd { @import '~antd/lib/style/core/index.less'; @import '~antd/lib/style/themes/default.less'; } +``` + +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. + +### Use a postcss processor to scope all styles + +See an example of usage with gulp and [postcss-prefixwrap](https://github.com/dbtedman/postcss-prefixwrap) : https://gist.github.com/sbusch/a90eafaf5a5b61c6d6172da6ff76ddaa + +## Not working? + +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. + +- 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. +- If you import styles from `'antd/dist/antd.css'`, change it to `antd/dist/antd.less`. + ## Related Articles - [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) diff --git a/docs/vue/customize-theme.zh-CN.md b/docs/vue/customize-theme.zh-CN.md index fd3a4b343..4ac9de546 100644 --- a/docs/vue/customize-theme.zh-CN.md +++ b/docs/vue/customize-theme.zh-CN.md @@ -7,51 +7,133 @@ Ant Design 设计规范上支持一定程度的样式定制,以满足业务和 ![](https://zos.alipayobjects.com/rmsportal/zTFoszBtDODhXfLAazfSpYbSLSEeytoG.png) -## 样式变量 +## Ant Design Vue 的样式变量 antd 的样式使用了 [Less](http://lesscss.org/) 作为开发语言,并定义了一系列全局/组件的样式变量,你可以根据需求进行相应调整。 -- [默认样式变量](https://github.com/vueComponent/ant-design-vue/blob/master/components/style/themes/default.less) +以下是一些最常用的通用变量,所有样式变量可以在 [这里](https://github.com/vueComponent/ant-design-vue/blob/master/components/style/themes/default.less) 找到。 + +```less +@primary-color: #1890ff; // 全局主色 +@link-color: #1890ff; // 链接色 +@success-color: #52c41a; // 成功色 +@warning-color: #faad14; // 警告色 +@error-color: #f5222d; // 错误色 +@font-size-base: 14px; // 主字号 +@heading-color: rgba(0, 0, 0, .85); // 标题色 +@text-color: rgba(0, 0, 0, .65); // 主文本色 +@text-color-secondary : rgba(0, 0, 0, .45); // 次文本色 +@disabled-color : rgba(0, 0, 0, .25); // 失效色 +@border-radius-base: 4px; // 组件/浮层圆角 +@border-color-base: #d9d9d9; // 边框色 +@box-shadow-base: 0 2px 8px rgba(0, 0, 0, .15); // 浮层阴影 +``` 如果以上变量不能满足你的定制需求,可以给我们提 issue。 ## 定制方式 -我们使用 [modifyVars](http://lesscss.org/usage/#using-less-in-the-browser-modify-variables) 的方式来覆盖变量。 -在具体工程实践中,有 `package.theme` 和 `less` 两种方案,选择一种即可。 +我们使用 [modifyVars](http://lesscss.org/usage/#using-less-in-the-browser-modify-variables) 的方式来进行覆盖变量。 +下面将针对不同的场景提供一些常用的定制方式。 -### 1) theme 属性(推荐) +### 在 webpack 中定制主题 -配置在 `package.json` 或 `.webpackrc` 下的 `theme` 字段。theme 可以配置为一个对象或文件路径。 +我们以 webpack@4 为例进行说明,以下是一个 `webpack.config.js` 的典型例子,对 [less-loader](https://github.com/webpack-contrib/less-loader) 的 options 属性进行相应配置。 -```js -"theme": { - "primary-color": "#1DA57A", -}, +```diff +// webpack.config.js +module.exports = { + rules: [{ + test: /\.less$/, + use: [{ + loader: 'style-loader', + }, { + loader: 'css-loader', // translates CSS into CommonJS + }, { + loader: 'less-loader', // compiles Less to CSS ++ options: { ++ modifyVars: { ++ 'primary-color': '#1DA57A', ++ 'link-color': '#1DA57A', ++ 'border-radius-base': '2px', ++ }, ++ javascriptEnabled: true, ++ }, + }], + // ...other rules + }], + // ...other config +} ``` -定义 `theme` 属性时,需要配合使用`less-loader` 的 `modifyVars` 配置来覆盖原来的样式变量。 -可以参考 [atool-build 中 less-loader 的 webpack 相关配置](https://github.com/ant-tool/atool-build/blob/a4b3e3eec4ffc09b0e2352d7f9d279c4c28fdb99/src/getWebpackCommonConfig.js#L131-L138) -注意: +注意 less-loader 的处理范围不要过滤掉 `node_modules` 下的 antd 包。 -- 样式必须加载 less 格式。 - - 如果你在使用 [babel-plugin-import](https://github.com/ant-design/babel-plugin-import) 的 `style` 配置来引入样式,需要将配置值从 `'css'` 改为 `true`,这样会引入 less 文件。 - - 如果你是通过 `'ant-design-vue/dist/antd.css'` 引入样式的,改为 `ant-design-vue/dist/antd.less`。 -- 如果要覆盖 `@icon-url` 变量,内容需要包括引号 `"@icon-url": "'your-icon-font-path'"`。 +### 在 vue cli 2中定制主题 -### 2) less +修改`build/utils.js`文件 +```diff +// build/utils.js +- less: generateLoaders('less'), ++ less: generateLoaders('less', { ++ modifyVars: { ++ 'primary-color': '#1DA57A', ++ 'link-color': '#1DA57A', ++ 'border-radius-base': '2px', ++ }, ++ javascriptEnabled: true, ++ }), -用 less 文件进行变量覆盖。 +``` -建立一个单独的 `less` 文件如下,再引入这个文件。 +### 在 vue cli 3中定制主题 - ```less - @import "~ant-design-vue/dist/antd.less"; // 引入官方提供的 less 样式入口文件 - @import "your-theme-file.less"; // 用于覆盖上面定义的变量 - ``` +项目更目录下新建文件`vue.config.js` +``` +// vue.config.js +module.exports = { + configureWebpack: { + module: { + rules: [{ + test: /\.less$/, + use: [{ + loader: 'style-loader', + }, { + loader: 'css-loader', + }, { + loader: 'less-loader', + options: { + modifyVars: { + 'primary-color': '#1DA57A', + 'link-color': '#1DA57A', + 'border-radius-base': '2px', + }, + javascriptEnabled: true, + }, + }], + }], + }, + } +} +``` -注意:这种方式已经载入了所有组件的样式,不需要也无法和按需加载插件 `babel-plugin-import` 的 `style` 属性一起使用。 +### 配置 less 变量文件 + +另外一种方式是建立一个单独的 `less` 变量文件,引入这个文件覆盖 `antd.less` 里的变量。 + +```css +@import "~antd/dist/antd.less"; // 引入官方提供的 less 样式入口文件 +@import "your-theme-file.less"; // 用于覆盖上面定义的变量 +``` + +注意,这种方式已经载入了所有组件的样式,不需要也无法和按需加载插件 `babel-plugin-import` 的 `style` 属性一起使用。 + +## 没有生效? + +注意样式必须加载 less 格式,一个常见的问题就是引入了多份样式,less 的样式被 css 的样式覆盖了。 + +- 如果你在使用 [babel-plugin-import](https://github.com/ant-design/babel-plugin-import) 的 `style` 配置来引入样式,需要将配置值从 `'css'` 改为 `true`,这样会引入 less 文件。 +- 如果你是通过 `'antd/dist/antd.css'` 引入样式的,改为 `antd/dist/antd.less`。 ## 社区教程 for Antd React diff --git a/docs/vue/introduce.zh-CN.md b/docs/vue/introduce.zh-CN.md index 23bdd6da1..2ef6afdf8 100644 --- a/docs/vue/introduce.zh-CN.md +++ b/docs/vue/introduce.zh-CN.md @@ -1,7 +1,7 @@ # Ant Design of Vue -这里是 Ant Design 3.X 的 Vue 实现,开发和服务于企业级后台产品。 +这里是 Ant Design 的 Vue 实现,开发和服务于企业级后台产品。

@@ -134,7 +134,7 @@ import 'ant-design-vue/dist/antd.css'; // or 'ant-design-vue/dist/antd.less' 众所周知,Ant Design作为一门设计语言面世,经历过多年的迭代和积累,它对UI的设计思想已经成为一套事实标准,受到众多前端开发者及企业的追捧和喜爱,也是React开发者手中的神兵利器。希望ant-design-vue能够让Vue开发者也享受到Ant Design的优秀设计。 -ant-design-vue是Ant Design 3.X的Vue实现,该组件库目前以实现Ant Design React版80%以上的组件,组件的风格与Ant Design 3.4.0版本保持同步,组件的html结构和css样式也保持一致,真正做到了样式0修改,组件API也尽量保持了一致。 +ant-design-vue 是 Ant Design 的Vue实现,组件的风格与Ant Design保持同步,组件的html结构和css样式也保持一致,真正做到了样式0修改,组件API也尽量保持了一致。 Ant Design Vue 致力于提供给程序员**愉悦**的开发体验。