ant-design/docs/react/customize-theme-variable.zh-CN.md
二货机器人 36bcaaef85
Add variable.less to support css variable (#31496)
* chore: use varaible.less

* chore: basic primary varaible

* chore: Move to variable

* chore: align active color

* chore: global fix of css variable

* chore: primary colors

* chore: button danger

* chore: btn default error color

* chore: button series

* chore: More examples

* chore: More components

* chore: Form demo

* chore: form style

* fix: Tag & Alert variable

* chore: update footer

* chore: rm tmp code

* chore: transfer

* fix: picker column active color

* chore: Adjust active bg color

* chore: table hover color

* chore: all css variables

* chore: Global using variables

* chore: Test case

* chore: Update test logic

* chore: back of default less

* chore: entry of site use proxy style

* chore: update entry

* chore: split of variables

* refactor: quick dist speed

* fix: site use variable version

* chore: Update less config

* chore: add mv script

* chore: Update repalcement script

* chore: Add inject variables

* chore: Update script

* fix: script path

* chore: Move to component instead

* chore: fix condition

* chore: update config

* chore: Update in less transform

* chore: Modify logic

* chore: change to variables

* chore: Update name

* fix: script name

* chore: do inject

* revert: back of path

* chore: 2 way of generate

* bump tools

* chore: Add auto replacement script

* chore: auto genrate less file

* chore: fix test

* test: More test case

* chore: Update limit config

* test: coverage

* docs: Update doc
2021-09-01 10:56:50 +08:00

1.6 KiB
Raw Blame History

order title
7.1 动态主题(实验性)

除了 less 定制主题外 外,我们还提供了 CSS Variable 版本以支持动态切换主题能力。你可以在 ConfigProvider 进行体验。

注意事项

该功能通过动态修改 CSS Variable 实现,因而在 IE 中页面将无法正常展示。请先确认你的用户环境是否需要支持 IE。

如何使用

引入 antd.variable.min.css

替换当前项目引入样式文件为 CSS Variable 版本:

-- import 'antd/dist/antd.min.css';
++ import 'antd/dist/antd.variable.min.css';

静态方法配置

调用 ConfigProvider 配置方法设置主题色:

import { ConfigProvider } from 'antd';

ConfigProvider.config({
  theme: {
    primaryColor: '#25b864',
  },
});

冲突解决

默认情况下CSS Variable 会以 --ant 作为前缀。当你的项目中引用多份 css 文件时,可以通过修改前缀的方式避免冲突。

代码调整

通过 ConfigProvider 在顶层修改 prefixCls

import { ConfigProvider } from 'antd';

export default () => (
  <ConfigProvider prefixCls="custom">
    <MyApp />
  </ConfigProvider>
);

通过静态方法设置主题色以及对应 prefixCls

ConfigProvider.config({
  theme: {
    prefixCls: 'custom',
    primaryColor: '#25b864',
  },
});

编译 less

由于前缀变更,你需要重新生成一份对应的 css 文件。

lessc  --modify-var="ant-prefix=custom" antd/dist/antd.variable.less modified.css