Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variable.less to support css variable #31496

Merged
merged 59 commits into from
Sep 1, 2021
Merged

Add variable.less to support css variable #31496

merged 59 commits into from
Sep 1, 2021

Conversation

zombieJ
Copy link
Member

@zombieJ zombieJ commented Jul 22, 2021

[中文版模板 / Chinese template]

📋 RFC

Summary

Support CSS Variable to dynamic change antd theme color with static function.

Basic example

import { ConfigProvider } from 'antd';

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

Motivation

In antd, theme is generate on build time which means user can not dynamic change theme on runtime. There is some hack way like build replacement css file back of css variable (which may cause wrong design token of same valued variables) or create a less builder on client or server side to build style add inject back.

For the modern browsers, css variable is a cheaper way to dynamic change theme. To avoid breaking current user like IE 11, it should keep all the output as previous version but provide additional build file with css variables version like antd.variable.css & antd.variable.min.css. So developer can chose to use these new variable style file to enable config provider static function.

Detailed

To realize this feature, we need create another entry for build. Current components is strong deps on the root style file which use the less function to generate style variables and these function variables can not work in browser. We need:

  1. Create a variable entry like default.less and replace all the variables by css variable
    • Consider to create script for auto replacement to avoid to variable sync issue
  2. Improve ConfigProvider.config to support inject css variables in browser side
  3. Move all the components deps apart from the root
    • 🧨 This is still need investigate since components deps on the root which makes recv deps in less. And these makes default version mixed with variable version
  4. Refactor related entry apart with target build mode
    • site build entry use variables to support dev requirement
    • dist (webpack) build entry to support multiple mode
    • es/lib build should chose correct build mode

Drawbacks

Since design token use lots of less function, its hard to replace all the variable with native one. We may lock the static function in alpha for a long time util all the design token is validate to replace

Adoption strategy

alpha version will release sync with current version of antd. When all the design token replaced, we will release final version.
Not provide less version file since it can not split on less side.


🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • Component style update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

💡 Background and solution

Before next major version. We consider to provide css variable support in v4 for the user who do not need support IE.

NOTE: This is far from finish since we do too much on less color calculation. To avoid break current behavior, we need some workaround to make it work.

https://preview-31496-ant-design.surge.sh/components/config-provider-cn/#components-config-provider-demo-theme

📝 Changelog

Language Changelog
🇺🇸 English
🇨🇳 Chinese

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@codecov
Copy link

codecov bot commented Jul 22, 2021

Codecov Report

Merging #31496 (4911998) into feature (a43e21e) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 4911998 differs from pull request most recent head 8afd4fb. Consider uploading reports for the commit 8afd4fb to get more accurate results
Impacted file tree graph

@@           Coverage Diff            @@
##           feature   #31496   +/-   ##
========================================
  Coverage    99.95%   99.96%           
========================================
  Files          409      410    +1     
  Lines         7461     7509   +48     
  Branches      2032     2039    +7     
========================================
+ Hits          7458     7506   +48     
  Misses           3        3           
Impacted Files Coverage Δ
components/config-provider/context.tsx 100.00% <ø> (ø)
components/config-provider/cssVariables.tsx 100.00% <100.00%> (ø)
components/config-provider/index.tsx 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a43e21e...8afd4fb. Read the comment docs.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 22, 2021

@github-actions
Copy link
Contributor

  • 🚨 Please fill changelog in the PR

    • Write with a developer-oriented perspective and narrative method, without describing the details of the repair
    • Describing the problem and the impact on the developer
    • describing the user-first site problem, not your solution
    • Refer: https://ant.design/changelog#4.9.0
  • 🚨 请填写 PR 中的 changelog

    • 请用面向开发者的角度叙述方式撰写,不描述修复细节
    • 描述问题和对开发者的影响
    • 描述用户第一现场的问题,而非你的解决方式
    • 参考:https://ant.design/changelog-cn#4.9.0

@github-actions
Copy link
Contributor

github-actions bot commented Jul 22, 2021

Size Change: +70.1 kB (+8%) 🔍

Total Size: 912 kB

Filename Size Change
./dist/antd-with-locales.min.js 350 kB +2.28 kB (+1%)
./dist/antd.compact.min.css 64.7 kB +34 B (0%)
./dist/antd.dark.min.css 66.1 kB +62 B (0%)
./dist/antd.min.css 64.9 kB +20 B (0%)
./dist/antd.min.js 301 kB +2.29 kB (+1%)
./dist/antd.variable.min.css 65.4 kB +65.4 kB (new file) 🆕

compressed-size-action

@afc163
Copy link
Member

afc163 commented Jul 26, 2021

  1. 需要发一段时间的 beta 版本,稳定第一
  2. 是否只要自定义 @primary-color 就能更换所有色彩,还是需要 @primary-1 到 10 都要自定义?

@zombieJ
Copy link
Member Author

zombieJ commented Jul 26, 2021

  1. 需要发一段时间的 beta 版本,稳定第一
  2. 是否只要自定义 @primary-color 就能更换所有色彩,还是需要 @primary-1 到 10 都要自定义?
  1. +1
  2. 目前看起来是散装的,大部分的可以合并干掉。几个散装的比较蛋疼。

@@ -300,12 +329,15 @@
`
);
}
& when not (@theme = dark) {
& when (not (@theme = dark) and not (@theme = variable)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

@hengkx
Copy link
Member

hengkx commented Jul 27, 2021

有没有办法 我只改变primary color 就 可以了,自定义颜色太多的话 用户很容易出问题

@shaodahong
Copy link
Member

  1. 目前看起来是散装的,大部分的可以合并干掉。几个散装的比较蛋疼。

@zombieJ 散装的 lighten(@primary-color, 10%) 这种能支持么

import { generate } from '@ant-design/colors';
import { Theme } from './context';

export function registerTheme(theme: Theme) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法能暴露出来,或者挂到 windows 上面吗?

@zombieJ zombieJ marked this pull request as ready for review August 31, 2021 13:40
@maxim-kolesnikov
Copy link
Contributor

Congratulations! 🏆

Copy link
Member

@xrkffgg xrkffgg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

666

```ts
ConfigProvider.config({
theme: {
prefixCls: 'custom',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://ant.design/components/config-provider-cn/#ConfigProvider.config()-4.13.0+

应该是这样?

 ConfigProvider.config({
   prefixCls: 'custom',
 });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个还没回复呀。@zombieJ

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

```bash
lessc --modify-var="ant-prefix=custom" antd/dist/antd.variable.less modified.css
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

```diff
-- import 'antd/dist/antd.min.css';
++ import 'antd/dist/antd.variable.min.css';
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提示一下去掉 babel-plugin-import 里的 css 或 style 属性,避免样式重复(如果用到)。

@@ -291,7 +291,7 @@
"bundlesize": [
{
"path": "./dist/antd.min.js",
"maxSize": "270 kB"
"maxSize": "275 kB"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加了 2kB 多,主要是在哪里?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSS 计算切换到 js,多了 3+ KB。

@zombieJ zombieJ merged commit 36bcaae into feature Sep 1, 2021
@zombieJ zombieJ deleted the css-variable branch September 1, 2021 02:56
@zombieJ zombieJ mentioned this pull request Sep 1, 2021
15 tasks
title: 动态主题(实验性)
---

除了 [less 定制主题外](/docs/react/customize-theme) 外,我们还提供了 CSS Variable 版本以支持动态切换主题能力。你可以在 [ConfigProvider](/components/config-provider/#components-config-provider-demo-theme) 进行体验。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/react/customize-theme 里也补一下动态主题内容,链接到这里来。

@zombieJ
Copy link
Member Author

zombieJ commented Sep 1, 2021

纯文档更新, 我在 #31987 顺道一起了

@maxim-kolesnikov
Copy link
Contributor

@zombieJ, Thank you for you job!

Could you explain, how I can use Dynamic Theme (experience) in antd@4.17.0-alpha.0 and modifyVars in webpack.pack.conf for change size-variables?

That config worked fine..

// index.ts
import 'antd/dist/antd.variable.less';
// webpack.config.js
module.exports = {
  rules: [{
    test: /\.less$/,
    use: [{
      loader: 'style-loader',
    }, {
      loader: 'css-loader',
    }, {
      loader: 'less-loader',
      options: {
        lessOptions: {
          modifyVars: {
            'btn-height-sm': '32px',
            'btn-height-lg': '48px',
            'border-radius-base': '2px',
            // ...other sizes variables
          },
          javascriptEnabled: true,
        },
      },
    }],
  }],
}

.. but, now for change primaryColor in runtime with antd@4.17.0-alpha.0, I should change variable file .less to .css extension, right?

 // index.ts
 -- import 'antd/dist/antd.variable.less';
 ++ import 'antd/dist/antd.variable.min.css';
 
  ConfigProvider.config({
    theme: {
      primaryColor: 'blue',
    },
  });

This changes lose modifyVars working. Buttons with size="large" has height: 40px as default. 🤔

@xrkffgg xrkffgg mentioned this pull request Sep 6, 2021
19 tasks
@olivewind
Copy link

这个 feature 什么时候可以正式发布?我看现在还是 alpha

@afc163
Copy link
Member

afc163 commented Sep 8, 2021

@olivewind alpha 也可以用起来了。

@wangjianio
Copy link

wangjianio commented Sep 9, 2021

升级 4.17.0-alpha.2 报这个错:

ERROR in ./node_modules/antd/es/spin/style/css.js 1:0-39
Module not found: Error: Can't resolve '../../style/index-default.css'

4.16.13 是好的

#32108

@afc163
Copy link
Member

afc163 commented Sep 9, 2021

@wangjianio 提个 issue。

@arifszn
Copy link
Contributor

arifszn commented Sep 10, 2021

+1. Finally dynamic theming on runtime. It would be nice to see dynamic dark mode implementation in near future too.

@kaysonwu kaysonwu mentioned this pull request Sep 14, 2021
1 task
@marklai1998
Copy link

still no dynamic dark mode?

@breezewish
Copy link

Congrats to the work! I believe the dark theme can be much easier to implement when using css variables.

@liquorxm
Copy link
Contributor

@zombieJ umijs框架中,通过 @import '~antd/es/style/themes/default.less'; 这种方式引入到自定义less文件,antd默认的less变量 @primary-color 不能通过此方法修改生效,请问下怎么解决呢

@dancerphil
Copy link
Contributor

Great Work! Save us much time on to be compatible with css variables.

@ckken
Copy link

ckken commented Jun 14, 2022

no dynamic dark mode!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet