-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Refactor Component Style to Adapt CSS Variables #45618
Comments
This comment was marked as resolved.
This comment was marked as resolved.
为了方便重构,在重构分支是默认开启的。整个方案和原先是兼容的,可以配置局部开启 |
Calendar |
认领 Watermark Statistic Empty Popconfirm skeleton |
想要讨论一个场景,看看在新的 css-variables 模式下怎么支持: 如果两个组件(比如 Button 和 Input 好了)都使用了 .ant-button {
--antd-color-primary: red;
}
.ant-input {
--antd-color-primary: blue;
} 然而,我作为一个设计规范的指定者,更希望在一个非常全局的范围定义他们,并且把组件级别的自定义能力交给业务开发者,比如: /* 设计规范 */
.app {
--antd-button-color-primary: red; /* 当然,此时可以取更好的名字 */
--antd-input-color-primary: blue;
}
/* 业务层面覆盖 */
.ant-button {
--antd-button-color-primary: pink;
} 这个提案可能可行吗?似乎会产生大量的 component token。(不过我觉得是不是可以在 map token 的层面还是用 theme,然后把 css var 仅作为 component token?) 我期望的 API 可能是这样的:
const className = calculateComponentTokenClassName(theme); // .css-var-hashxxx.ant-theme
return <div className={className}>{children}</div> .css-var-hashxxx.ant-theme {
--ant-button-primary-color: #1976d2;
--ant-input-border-color: #1976d2;
--ant-input-placeholder-color: #eeeeee;
/* ... */
}
|
@dancerphil 我们目前希望用户还是使用 ConfigProvider 去修改主题——也就是说是否开启 css 变量对于主题的使用是没有任何影响的。 如果需要在 react 生命周期之外修改 css 变量,自定义主题的 key(用于替代 css-var-hash),再加上组件的 root class 就可以做到覆盖。 无论如何,我们想尽量淡化用户对 css 变量的认知,有些后续的功能或者拓展仍然可以在此之上进行。 |
我想认领一下: |
认领 |
认领 |
Task updated for above volunteers ❤️. |
认领 |
Why need Refactor
Currently we generate different hash and tokens for each theme, which are used in styles to get different styles, and sometimes we will use tokens to calculate a new value. Since we will replace actual value of token with CSS variables like
var(--color-primary)
, tokens could not be used to calculate directly any more. That's the major reason that we must refactor antd component style.Refactor Plan
We will put component refactoring tasks in this issue, and these tasks should be all done in November, which means the feature of CSS variables will be released in
5.12.0
. Assigned task that do not have response for more than one week will be called back.All PRs should be based on
feature
branch. Infeature
branch, all demos are enabled with CSS variables.The first PR: #45589 . This PR contains feature of CSS variables and refactor of Button. We will accpect task requestment after this PR is merged.
How and What to Refactor
Turn Component Token into CSS variables
cssVar.ts
incomponents/xxx/style/
directory, and fill in with code below (demonstrated with Button)prepareComponentToken
is the third param ofgenComponentStyleHook
. If it exist, we should extract and export it fromstyle/index.ts
.This file will export a hook, we will use it in component.
useStyle
, importuseCSSVar
in component:wrapSSR
is useless, we could safely remove it.useCSSVar
need one param, which should berootCls
of the component. In most cases, thisrootCls
could beprefixCls
. But ifprefixCls
is not always on the root element of component, we could useuseCSSVarCls
to generate a new class and apply it to whererootClassName
is applied to.In this way, all component token will be transformed into CSS variables. If you customize component token, you could find styles like this:
Refactor Token Calculation
As is said before, we need to refactor token calculation in order to get right value. With CSS variables, all calculation should be transformed with
calc()
, or it should become a component token.In
prepareComponentToken
, tokens remain actual value, which could be directly calculated with js code, so we do not need to refactor this part of code, and can even add more component tokens to avoid transforming. Some calculation for color can not be transformed withcalc()
neither, so we should add component token for them.token.calc
Outside of
prepareComponentToken
, we must replace all calculation withtoken.calc
. This is a function that will differ by configuration of CSS variables. So we could use one code to get different value with different configuration. For example:token.calc
is chainable, and it will do calculation by the order. For more example, take a look at the test cases:https://github.com/ant-design/ant-design/pull/45589/files#diff-474a355b4973b5cd9ab68c695f77e8fc28504f72f7af6297c8396151bbfff873R13
Replace
Math.max
andMath.min
withtoken.max
andtoken.min
Same as
token.calc
, the result with css var will bemin(var(--border-radius), 4px)
Replace
px
withunit()
For some css properties like
border
, we used to write like this:border: `${token.lineWidth}px ${token.lineStyle} ${token.borderColor}`
But with CSS varaibles,
`${token.lineWidth}px`
will not work. So we should useunit()
from@ant-deisng/cssinjs
.Replace
fontSize * lineHeight
withfontHeight
We used to write
Math.round(fontSize * lineHeight)
in style to get integer, butround()
of CSS is not available now. SofontHeight
is added into token, which is calculated already withfontSize
andlineHeight
, and so asfontHeightSM
andfontHeightLG
.Task List
If you are interested in contributing, please comment in this issue and pick one component that is not assigned to anyone.
Simple PR to reference: #45716
Example with custom
rootCls
andMath.ceil
: #45727@MadCcc
@li-jia-nan
@RedJue
@c0dedance
Watermark@kiner-tang
@JarvisArt
@cc-hearts
The text was updated successfully, but these errors were encountered: