Browse Source

doc: add custom theme

qingwei.li 8 năm trước cách đây
mục cha
commit
8704a872a0
2 tập tin đã thay đổi với 86 bổ sung0 xóa
  1. 1 0
      README.md
  2. 85 0
      custom-theme.md

+ 1 - 0
README.md

@@ -14,6 +14,7 @@
 - [Home Page](http://element.eleme.io/)
 - [Docs](http://element.eleme.io/#/component)
 - [FAQ 中文](./FAQ.md)
+- [自定义主题](./custom-theme.md)
 - [i18n](./i18n.md)
 - Starter Kit
  - [element-starter](https://github.com/ElementUI/element-starter)

+ 85 - 0
custom-theme.md

@@ -0,0 +1,85 @@
+# 自定义主题
+Element 默认提供一套主题,CSS 命名采用 BEM 的风格方便使用者覆盖样式。如果你想完全替换主题色或者部分样式,可以使用下面方法。
+
+## 安装工具
+首先安装「主题生成工具」,可以全局安装或者安装在当前项目下,推荐安装在项目里,方便别人 clone 项目时能直接安装依赖并启动。
+```shell
+npm i element-theme -D
+```
+
+安装默认主题,可以从 npm 安装或者从 GitHub 拉取最新代码。
+```shell
+npm i element-theme-default@next -D
+
+# 从 GitHub
+npm i https://github.com/ElementUI/theme-default -D
+```
+
+## 初始化变量文件
+主题生成工具安装成功后,如果全局安装可以在命令行里通过 `et` 调用工具,如果安装在当前目录下,需要通过 `node_modules/.bin/et` 访问到命令。执行 `-i` 初始化变量文件。默认输出到 `element-variables.css`,当然你可以传参数指定文件输出目录。
+
+```shell
+node_modules/.bin/et -i [可以自定义变量文件目录]
+
+> ✔ Generator variables file
+```
+
+如果使用默认配置,执行后当前目录会有一个 `element-variables.css` 文件。内部包含了主题所用到了所有变量,它们使用 CSS4 的风格定义。大致结构如下:
+```css
+:root {
+
+  /* Colors
+  -------------------------- */
+  --color-primary: #20a0ff;
+  --color-success: #13ce66;
+  --color-warning: #f7ba2a;
+  --color-danger: #ff4949;
+  --color-info: #50BFFF;
+  --color-blue: #2e90fe;
+  --color-blue-light: #5da9ff;
+  --color-blue-lighter: rgba(var(--color-blue), 0.12);
+  --color-white: #fff;
+  --color-black: #000;
+  --color-grey: #C0CCDA;
+```
+
+## 修改变量
+直接编辑 `element-variables.css` 文件,例如修改主题色为红色。
+```CSS
+--color-primary: red;
+```
+
+## 编译主题
+保存文件后,到命令行里执行 `et` 编译主题,如果你想启用 `watch` 模式,实时编译主题,增加 `-w` 参数。
+```shell
+node_modules/.bin/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` 是什么,请阅读 [快速上手](./examples/docs/zh-cn/quickstart.md) 一节。更多 `element-theme` 用法请参考[项目仓库](https://github.com/ElementUI/element-theme)。