From 71fca8e051aa2213f4757496dd0520fc40314e59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=A5=95?= Date: Tue, 24 Oct 2017 05:43:23 -0500 Subject: [PATCH] update custom theme docs (#7679) * update custom theme docs * update form docs --- examples/docs/en-US/custom-theme.md | 92 ++++++++++++++++++++++++++++- examples/docs/en-US/form.md | 32 +++++----- examples/docs/zh-CN/custom-theme.md | 90 ++++++++++++++++++++++++++++ examples/docs/zh-CN/form.md | 32 +++++----- packages/select/src/select.vue | 2 +- 5 files changed, 210 insertions(+), 38 deletions(-) diff --git a/examples/docs/en-US/custom-theme.md b/examples/docs/en-US/custom-theme.md index 069f8c5e6..ed130f880 100644 --- a/examples/docs/en-US/custom-theme.md +++ b/examples/docs/en-US/custom-theme.md @@ -1,2 +1,92 @@ ## Custom theme -Under construction. \ No newline at end of file +Element uses BEM-styled CSS so that you can override styles easily. But if you need to replace styles at a large scale, e.g. change the theme color from blue to orange or green, maybe overriding them one by one is not a good idea, and this is where our theme customization tools kick in. + +### Changing theme color +Under construction. + +### More customizations +If you need more customization than just changing the theme color, please follow these steps: + +#### Install related tool +First install the theme generator globally or locally. Local install is recommended because in this way, when others clone your project, npm will automatically install it for them. +```shell +npm i element-theme -g +``` + +Then install the chalk theme from npm or GitHub. +```shell +# from npm +npm i element-theme-chalk -D + +# from GitHub +npm i https://github.com/ElementUI/theme-chalk -D +``` + +#### Initialize variable file +After successfully installing the above packages, a command named `et` is available in CLI (if the packages are installed locally, use `node_modules/.bin/et` instead). Run `-i` to initialize the variable file which outputs to `element-variables.scss` by default. And you can specify its output directory as you will. + +```shell +et -i [custom output file] + +> ✔ Generator variables file +``` + +In `element-variables.scss` you can find all the variables we used to style Element and they are defined in SCSS format: +```css +$--color-primary: #409EFF !default; +$--color-primary-light-1: mix($--color-white, $--color-primary, 10%); /* 53a8ff */ +$--color-primary-light-2: mix($--color-white, $--color-primary, 20%); /* 66b1ff */ +$--color-primary-light-3: mix($--color-white, $--color-primary, 30%); /* 79bbff */ +$--color-primary-light-4: mix($--color-white, $--color-primary, 40%); /* 8cc5ff */ +$--color-primary-light-5: mix($--color-white, $--color-primary, 50%); /* a0cfff */ +$--color-primary-light-6: mix($--color-white, $--color-primary, 60%); /* b3d8ff */ +$--color-primary-light-7: mix($--color-white, $--color-primary, 70%); /* c6e2ff */ +$--color-primary-light-8: mix($--color-white, $--color-primary, 80%); /* d9ecff */ +$--color-primary-light-9: mix($--color-white, $--color-primary, 90%); /* ecf5ff */ + +$--color-success: #67c23a !default; +$--color-warning: #eb9e05 !default; +$--color-danger: #fa5555 !default; +$--color-info: #878d99 !default; +``` + +#### Modify variables +Just edit `element-variables.scss`, e.g. changing the theme color to red: +```CSS +$--color-primary: red; +``` + +#### Build theme +After saving the variable file, use `et` to build your theme. You can activate `watch` mode by adding a parameter `-w`. And if you customized the variable file's output, you need to add a parameter `-c` and variable file's name: +```shell +et + +> ✔ build theme font +> ✔ build element theme +``` + +#### Import custom theme +By default the build theme file is placed inside `./theme`. You can specify its output directory with parameter `-o`. Importing your own theme is just like importing the default theme, only this time you import the file you just built: + +```javascript +import '../theme/index.css' +import ElementUI from 'element-ui' +import Vue from 'vue' + +Vue.use(ElementUI) +``` + +#### Import component theme on demand +If you are using `babel-plugin-component` for on-demand import, just modify `.babelrc` and specify `styleLibraryName` to the path where your custom theme is located relative to `.babelrc`. Note that `~` is required: +```json +{ + "plugins": [["component", [ + { + "libraryName": "element-ui", + "styleLibraryName": "~theme" + } + ]]] +} +``` + +If you are unfamiliar with `babel-plugin-component`, please refer to Quick Start. For more details, check out the [project repository](https://github.com/ElementUI/element-theme) of `element-theme`. \ No newline at end of file diff --git a/examples/docs/en-US/form.md b/examples/docs/en-US/form.md index 2eb1b4153..23520e62b 100644 --- a/examples/docs/en-US/form.md +++ b/examples/docs/en-US/form.md @@ -5,17 +5,15 @@ if (!value) { return callback(new Error('Please input the age')); } - setTimeout(() => { - if (!Number.isInteger(value)) { - callback(new Error('Please input digits')); + if (!Number.isInteger(value)) { + callback(new Error('Please input digits')); + } else { + if (value < 18) { + callback(new Error('Age must be greater than 18')); } else { - if (value < 18) { - callback(new Error('Age must be greater than 18')); - } else { - callback(); - } + callback(); } - }, 1000); + } }; var validatePass = (rule, value, callback) => { if (value === '') { @@ -548,17 +546,15 @@ This example shows how to customize your own validation rules to finish a two-fa if (!value) { return callback(new Error('Please input the age')); } - setTimeout(() => { - if (!Number.isInteger(value)) { - callback(new Error('Please input digits')); + if (!Number.isInteger(value)) { + callback(new Error('Please input digits')); + } else { + if (value < 18) { + callback(new Error('Age must be greater than 18')); } else { - if (value < 18) { - callback(new Error('Age must be greater than 18')); - } else { - callback(); - } + callback(); } - }, 1000); + } }; var validatePass = (rule, value, callback) => { if (value === '') { diff --git a/examples/docs/zh-CN/custom-theme.md b/examples/docs/zh-CN/custom-theme.md index acbca83ff..5c27efc8c 100644 --- a/examples/docs/zh-CN/custom-theme.md +++ b/examples/docs/zh-CN/custom-theme.md @@ -1,2 +1,92 @@ ## 自定义主题 +Element 默认提供一套主题,CSS 命名采用 BEM 的风格方便使用者覆盖样式。如果你想完全替换主题色或者部分样式,可以使用下面的方法。 + +### 仅替换主题色 整理中。 + +### 深层次的定制 +如果仅仅改变主题色不能满足你的需求,请按以下步骤进行更深层次的主题定制: + +#### 安装工具 +首先安装「主题生成工具」,可以全局安装或者安装在当前项目下,推荐安装在项目里,方便别人 clone 项目时能直接安装依赖并启动,这里以全局安装做演示。 +```shell +npm i element-theme -g +``` + +安装白垩主题,可以从 npm 安装或者从 GitHub 拉取最新代码。 +```shell +# 从 npm +npm i element-theme-chalk -D + +# 从 GitHub +npm i https://github.com/ElementUI/theme-chalk -D +``` + +#### 初始化变量文件 +主题生成工具安装成功后,如果全局安装可以在命令行里通过 `et` 调用工具,如果安装在当前目录下,需要通过 `node_modules/.bin/et` 访问到命令。执行 `-i` 初始化变量文件。默认输出到 `element-variables.scss`,当然你可以传参数指定文件输出目录。 + +```shell +et -i [可以自定义变量文件] + +> ✔ Generator variables file +``` + +如果使用默认配置,执行后当前目录会有一个 `element-variables.scss` 文件。内部包含了主题所用到的所有变量,它们使用 SCSS 的格式定义。大致结构如下: +```css +$--color-primary: #409EFF !default; +$--color-primary-light-1: mix($--color-white, $--color-primary, 10%); /* 53a8ff */ +$--color-primary-light-2: mix($--color-white, $--color-primary, 20%); /* 66b1ff */ +$--color-primary-light-3: mix($--color-white, $--color-primary, 30%); /* 79bbff */ +$--color-primary-light-4: mix($--color-white, $--color-primary, 40%); /* 8cc5ff */ +$--color-primary-light-5: mix($--color-white, $--color-primary, 50%); /* a0cfff */ +$--color-primary-light-6: mix($--color-white, $--color-primary, 60%); /* b3d8ff */ +$--color-primary-light-7: mix($--color-white, $--color-primary, 70%); /* c6e2ff */ +$--color-primary-light-8: mix($--color-white, $--color-primary, 80%); /* d9ecff */ +$--color-primary-light-9: mix($--color-white, $--color-primary, 90%); /* ecf5ff */ + +$--color-success: #67c23a !default; +$--color-warning: #eb9e05 !default; +$--color-danger: #fa5555 !default; +$--color-info: #878d99 !default; +``` + +#### 修改变量 +直接编辑 `element-variables.scss` 文件,例如修改主题色为红色。 +```CSS +$--color-primary: red; +``` + +#### 编译主题 +保存文件后,到命令行里执行 `et` 编译主题,如果你想启用 `watch` 模式,实时编译主题,增加 `-w` 参数;如果你在初始化时指定了自定义变量文件,则需要增加 `-c` 参数,并带上你的变量文件名 +```shell +et + +> ✔ build theme font +> ✔ build element theme +``` + +#### 引入自定义主题 +默认情况下编译的主题目录是放在 `./theme` 下,你可以通过 `-o` 参数指定打包目录。像引入默认主题一样,在代码里直接引用 `theme/index.css` 文件即可。 + +```javascript +import '../theme/index.css' +import ElementUI from 'element-ui' +import Vue from 'vue' + +Vue.use(ElementUI) +``` + +#### 搭配插件按需引入组件主题 +如果是搭配 `babel-plugin-component` 一起使用,只需要修改 `.babelrc` 的配置,指定 `styleLibraryName` 路径为自定义主题相对于 `.babelrc` 的路径,注意要加 `~`。 +```json +{ + "plugins": [["component", [ + { + "libraryName": "element-ui", + "styleLibraryName": "~theme" + } + ]]] +} +``` + +如果不清楚 `babel-plugin-component` 是什么,请阅读 快速上手 一节。更多 `element-theme` 用法请参考[项目仓库](https://github.com/ElementUI/element-theme)。 \ No newline at end of file diff --git a/examples/docs/zh-CN/form.md b/examples/docs/zh-CN/form.md index 0f540eb99..b516e6d9d 100644 --- a/examples/docs/zh-CN/form.md +++ b/examples/docs/zh-CN/form.md @@ -5,17 +5,15 @@ if (!value) { return callback(new Error('年龄不能为空')); } - setTimeout(() => { - if (!Number.isInteger(value)) { - callback(new Error('请输入数字值')); + if (!Number.isInteger(value)) { + callback(new Error('请输入数字值')); + } else { + if (value < 18) { + callback(new Error('必须年满18岁')); } else { - if (value < 18) { - callback(new Error('必须年满18岁')); - } else { - callback(); - } + callback(); } - }, 1000); + } }; var validatePass = (rule, value, callback) => { if (value === '') { @@ -539,17 +537,15 @@ if (!value) { return callback(new Error('年龄不能为空')); } - setTimeout(() => { - if (!Number.isInteger(value)) { - callback(new Error('请输入数字值')); + if (!Number.isInteger(value)) { + callback(new Error('请输入数字值')); + } else { + if (value < 18) { + callback(new Error('必须年满18岁')); } else { - if (value < 18) { - callback(new Error('必须年满18岁')); - } else { - callback(); - } + callback(); } - }, 1000); + } }; var validatePass = (rule, value, callback) => { if (value === '') { diff --git a/packages/select/src/select.vue b/packages/select/src/select.vue index 9733df4d2..6d10cdfbb 100644 --- a/packages/select/src/select.vue +++ b/packages/select/src/select.vue @@ -544,7 +544,7 @@ resetInputHeight() { this.$nextTick(() => { - if (!this.$refs.reference) return; + if (!this.$refs.reference || !this.$refs.tags) return; let inputChildNodes = this.$refs.reference.$el.childNodes; let input = [].filter.call(inputChildNodes, item => item.tagName === 'INPUT')[0]; input.style.height = this.selected.length === 0 && this.size === 'mini'