【RFC】应用级 CSSinJS 解决方案 | CSSinJS solution for Application #40489
arvinxx
announced in
RFCs - archive
Replies: 4 comments 10 replies
-
有个疑问,改成 |
Beta Was this translation helpful? Give feedback.
6 replies
-
基于Antd5 自己实现了一个ProThemeProvider... |
Beta Was this translation helpful? Give feedback.
0 replies
-
dumi 这个站点主题比以前好看多了 😆 |
Beta Was this translation helpful? Give feedback.
2 replies
-
这个通过 jss-react 把 token 注入到 ThemeProvider 就可以做到,感觉不需要单独做这个了。 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
概述 Summary
为应用级研发提供结合 antd V5 Token System 的 css-in-js 解决方案。
We will provide a CSS-in-JS solution combined with the antd V5 token system for application-level development.
动机 Motivation
距 antd 发布已经 3 月有余,逐步有新的应用开始接入 antd v5, antd 作为一个组件库的职责和边界只在于提供高品质的基础组,应用层如何消费 antd 并不限制。但目前来看,无论是内部落地应用,还是社区的相关反馈来看,关于 antd v5 token 系统如何使用、less 怎么迁移、应用如何集成 cssinjs 等问题都让应用开发者较难享受到 cssinjs 所带来的红利。
It has been more than three months since the release of antd v5, and new applications are gradually starting to integrate with antd v5. As a component library, antd's responsibility and boundaries are only to provide high-quality basic components, and the application development is not restricted in how to consume antd. However, based on internal application implement and community feedback, it is difficult for application developers to enjoy the dividends brought by CSS-in-JS technology because of issues such as how to use antd v5 token system, how to migrate from Less, and how to integrate CSS-in-JS into applications.
所以为了将 token 系统的推行变得更加顺利,我们需要提供一个使用 antd token 系统的最佳实践,帮助应用开发者更低门槛地集成 cssinjs 方案,享受新技术所带来的红利。
Therefore, to make the promotion of the token system more smooth, we need to provide a best practice for using the antd token system to help application developers integrate the CSS-in-JS solution with lower barriers and enjoy the benefits of new technology.
于是 antd-style 应运而生。
That's why antd-style born.
核心问题与解决手法 Core Problems and Solutions
目前我们看下来和 antd token 体系集成存在最大的几个问题:
Currently, there are several major problems with integrating the antd token system:
antd-style 需要逐一回答上述问题,解决思路如下:
antd-style
中提供一个 high-level 的 Provider,只负责主题相关的功能(基于CP封装),提供一键切换亮暗色主题、自定义 Token 的统一入口,使得应用开发者可以一键完成主题切换;antd-style needs to answer the above questions one by one, and the solution is as follows:
“解耦 ”这件事情可以展开说说。在 CSSinJS 生态中,目前已经固化的两个通用 api 是
styled
和css
。前者用于创建样式组件,后者用于创建样式片段。antd-style 要做的事情就是基于styled
和css
两个 api 做一层 high-level 的封装,进而集成 antd 的 token 体系,或者做进一步的性能优化等等。这样的一个好处就是,如果未来某个 CSSinJS 库就有了更加好的性能或者更强的能力(比如构建时抽取css),上层应用的使用方式不用做任何变化,只需修改 antd-style 的css
、styled
的引入库即可接入。甚至可以让用户自行指定css
、styled
方法使用哪个 CSSinJS 库。Let's talk about "decoupling". In the CSS-in-JS ecosystem, the two universal APIs that have been solidified are styled and css. The former is used to create style components, and the latter is used to create style fragments. What antd-style needs to do is to encapsulate styled and css APIs at a high level, and then integrate the antd token system or further performance optimization. The advantage of this is that if a CSS-in-JS library has better performance or stronger capabilities in the future (such as extracting CSS during build time), the usage of the upper application does not need to change at all, and only the css and styled introduction libraries of antd-style need to be modified. Users can even specify which CSS-in-JS library to use for css and styled methods.
API
基于上述思路 antd-style 所提供的两个核心 API:
ThemeProvider
和createStyles
。Based on the above ideas, antd-style provides two core APIs: ThemeProvider and createStyles.
ThemeProvider
:用于全局管理主题变量的容器。基于 CP封装 且提供一键切换亮暗色主题、自定义主题、应用级作用域样式的统一入口。文档 ->
a container for globally managing theme variables. Based on CP encapsulation, it provides a unified entry for one-click switching between light and dark themes, custom themes, and application-level scoped styles. Documentation ->
createStyles
:对标 css modules 的应用样式方案,可以创建具有局部作用域的样式。 文档 ->
An application style solution that is similar to CSS modules and can create styles with local scope. Documentation ->
除此之外,由于 antd-style 是 css-in-js based 。因此还有一些配套的 hooks:
useTheme
、useThemeMode
、useResponsive
等,后续会在文档中阐释。In addition, because antd-style is css-in-js based, there are also some related hooks: useTheme, useThemeMode, useResponsive, etc., which will be explained in the documentation later.
文档 Documentation ->
进展
2023.06.10
发布正式版
介绍文章:https://www.yuque.com/ant-design/ant-design/me6sc23d3thgysxt
知乎: https://zhuanlan.zhihu.com/p/636143897
2023.02.27 更新
欢迎使用 beta 验证(核心 API 已经稳定,边界场景的个别方法存在 Breaking Change 可能性)
Beta verification is welcome (the core API is stable, but some methods in boundary scenarios may have breaking changes).
功能部分:当前已完成所有基础功能研发,同时 dog fooding 了文档站主题(Landing)。还需要做个性能的 benchmark。
文档部分:进度 90%,还缺三篇: stylish 和 babel 插件、SSR 集成。
落地验证部分:已在内部4个应用落地测试,待进一步在 Ant Design 官网和 Ant Design Pro 中验证。
后续增强计划:
Functionality: All basic functionality has been developed and the documentation site theme has been dogfooded ( [Landing] (https://ant-design-antd-style-preview-pr-19.surge.sh/)). Performance benchmarking is still needed.
Documentation: 90% completed, still missing three articles: stylish and babel plugins, SSR integration.
Validation: Tested in four internal applications and further validation is needed on the Ant Design website and Ant Design Pro.
Future Enhancement Plan:
Beta Was this translation helpful? Give feedback.
All reactions