Ant Design Pro
是一个很是优秀的开箱即用的中台前端/设计解决方案,私觉得有些地方过分封装,固然这是见仁见智的见解。项目中遇到须要切换主题的需求,研究了一下,最后获得了比较完美的结果。css
主题切换分为两个部分,一个是 Ant Design
组件的主题切换,另外一个是自定义组件的主题切换。下面分为两部分一个个解决。前端
Ant Design
组件在 V2 版本中,主题切换的功能是有Bug的,官方也不推荐使用,可是在 V4 版本中解决了此问题,通过本人测试,V4 版本的解决方案同时适应于 V2 版本。react
首先,安装 umi-plugin-antd-theme
这个插件:git
npm i umi-plugin-antd-theme
复制代码
这个插件的原理就是根据你的配置项,将 Ant Design
全部组件中的变量替换成你自定义的,生成多个主题.css
文件,放置在theme
文件夹下。github
将下面的代码复制到config/config.*.js
文件中去,最后以下:npm
const plugins = [ [ 'umi-plugin-react', ... ], [ 'umi-plugin-antd-theme', { theme: [ { fileName: 'theme1.css', key:'theme1', modifyVars: { '@primary-color': '#13C2C2', '@menu-dark-color': '#324444', '@menu-dark-bg': '#5A5A5A', }, }, { fileName: 'theme2.css', key:'theme2', modifyVars: { '@primary-color': '#4992BF', '@menu-dark-color': '#9B9B9B', '@menu-dark-bg': '#3A3A3A', }, }, ], // 是否压缩css min: true, // css module isModule: true, // 忽略 antd 的依赖 ignoreAntd: false, // 忽略 pro-layout ignoreProLayout: false, // 不使用缓存 cache: true, }, ], ]; 复制代码
config/themePluginConfig.ts
添加相似代码:export default { theme: [ ... { fileName: 'theme1.css', key:'theme1', modifyVars: { '@primary-color': '#13C2C2', '@menu-dark-color': '#324444', '@menu-dark-bg': '#5A5A5A', }, }, { fileName: 'theme2.css', key:'theme2', modifyVars: { '@primary-color': '#4992BF', '@menu-dark-color': '#9B9B9B', '@menu-dark-bg': '#3A3A3A', }, }, ], }; 复制代码
在global.less
文件中,添加以下代码:缓存
.body-wrap-theme1 { // theme1下的全局变量在此定义 --font-color: #000000; --bg-color: #011313; } .body-wrap-theme2 { // theme2下的全局变量在此定义 --font-color: #ffffff; --bg-color: #ffffff; } 复制代码
自定义组件的index.less
中用法以下:bash
.flatButton{
color: var(--font-color);
backgroud: var(--bg-color);
}
复制代码
在主题切换的方法中添加以下代码,能够根据本身须要进行修改,好比添加从本地获取上次主题配置项等:markdown
theme1 = true; onClick = () => { let styleLink = document.getElementById("theme-style"); let body = document.getElementsByTagName('body')[0]; if (styleLink) { // 假如存在id为theme-style 的link标签,直接修改其href if (this.theme1) { styleLink.href = '/theme/theme1.css'; // 切换 ant design 组件主题 body.className = "body-wrap-theme1"; // 切换自定义组件的主题 } else { styleLink.href = '/theme/theme2.css'; body.className = "body-wrap-theme2"; } this.theme1 = !this.theme1; } else { // 不存在的话,则新建一个 styleLink = document.createElement('link'); styleLink.type = 'text/css'; styleLink.rel = 'stylesheet'; styleLink.id = 'theme-style'; if (this.theme1) { styleLink.href = '/theme/theme1.css'; body.className = "body-wrap-theme1"; } else { styleLink.href = '/theme/theme2.css'; body.className = "body-wrap-theme2"; } this.theme1 = !this.theme1; document.body.append(styleLink); } } 复制代码
这个实现的思路容易理解,一个是替换CSS文件,一个是css
自带的 var
替换变量。PS:感谢同事梁老师的帮助antd
JerryMissTom是个人 GitHub
地址,欢迎Follow