Ant Design StyleAnt Design Style
Quick Start
Best Practices
API Reference
Release Notes
⌘ K

Table of Contents

The principle of theme response in styled
Integration of styled and ThemeProvider
styled-components
@emotion/styled
Globally unified integration of styled
全局设定 Styled 配置
Typescript type definition support
Injecting main theme type for styled-components
Injecting main theme type for @emotion/styled
Basic Knowledge
Introduction
Quick Start to CSS in JS
Comparison of CSS in JS Writing Methods
Design Philosophy and Implementation Strategy
Quick Start
Writing Style
Theme Switching
Custom Theme
Advanced Usage
Babel Plugin
SSR Integration
Component Development
Integrated styled
🚧 Stylish Composite Styles
Performance Comparison of CSS-in-JS Libraries
Migrating from Less
Less Application Automated Migration
Manual Migration of Less Applications
Migrating Less Components
🚧 Compilation Differences Between CSS-in-JS and Less
PREV
Component Development
NEXT
🚧 Stylish Composite Styles

相关资源

Ant Design
Ant Design Pro
Ant Design Pro Components
Umi-React 应用开发框架
Dumi-组件/文档研发工具
qiankun-微前端框架

社区

Medium
Twitter
yuqueAnt Design 语雀专栏
Ant Design 知乎专栏
体验科技专栏
seeconfSEE Conf-SEE Conf-蚂蚁体验科技大会

帮助

GitHub
更新日志
讨论

more products更多产品

yuque语雀-知识创作与分享工具
AntVAntV-数据可视化解决方案
EggEgg-企业级 Node.js 框架
kitchenKitchen-Sketch 工具集
xtech蚂蚁体验科技
Copyright © 2022-2024
Made with ❤️ by 蚂蚁集团 - AFX & 数字科技
‌
‌
‌
‌

Consuming styled with Integration

After thorough research and extensive business practice, we have decided not to embed the styled method in antd-style, but to provide a theme compatibility solution for users of styled.

Friendly

If you are not familiar with styled, it is recommended to first refer to styled-components to understand its usage.

The principle of theme response in styled

Let's take a look at a typical way of consuming themes in styled:

tsx

It can be seen that the theme response of styled is provided through ThemeProvider, which will pass the theme as props to StyledButton, thereby achieving theme response. At the underlying level, whether it's styled-components or @emotion/styled's ThemeProvider, they both create Provider containers and hooks methods through React's Context.

In other words, the styled method and ThemeProvider (along with the accompanying useTheme) must appear in pairs. styled-components' styled cannot respond to @emotion/styled's ThemeProvider, and @emotion/styled's useTheme cannot respond to styled-components' ThemeProvider.

By reading the source code of both ( styled-components and @emotion/styled ), we found that the implementation of these styled methods is coupled with their own created Context, so custom Context cannot be passed in from the outside.

This means that if you want to create styled components and use them, each component must be wrapped in a ThemeProvider externally, instead of being directly imported like regular components.

tsx

The root cause of these cumbersome operations lies in the fact that the ThemeProvider used by styled does not provide custom Context injection. At present, it seems that styled-components is unlikely to implement this feature (issue).

This also means that if you want to customize a ThemeContext as the default theme for components and access it in the styled syntax, you must implement your own styled method, which is not practical for most component developers.

In the chapter Comparison of CSS in JS Syntax, we believe that due to design flaws, the styled API will decline in the future. Therefore, in antd-style, we will only provide a compatible usage solution for styled's ThemeProvider and useTheme.

Integration of styled and ThemeProvider

In antd-style's ThemeProvider, we provide a styled prop, which is used to receive the external ThemeContext corresponding to styled, so that the styled method can respond to antd-style's antd token, custom token, and theme state.

tsx

For the documentation of the styled API in ThemeProvider, please refer to: ThemeProvider - styled integration configuration

Globally unified integration of styled

For scenarios where there are multiple ThemeProviders, we provide a setupStyled method to uniformly inject the external styled ThemeContext into antd-style's ThemeProvider, so that the styled method can respond to any ThemeProvider.

tsx
WARNING

Please note: setupStyled needs to be executed before the application is initialized, otherwise it will not take effect. In addition, since setupStyled will affect all ThemeProviders in antd-style, it is not recommended to use it in multiple application scenarios, as it may contaminate other applications' ThemeProviders.

Typescript type definition support

Similar to the extension of the main theme type in antd-style, if you want your own styled methods to be able to access the main theme type of antd-style, you need to globally supplement the type definitions of styled-components or @emotion/styled in the project.

Injecting main theme type for styled-components

Inject the main theme type of antd-style into styled-components' styled.

tsx

Injecting main theme type for @emotion/styled

When injecting the main theme type for emotion's styled, it is important to note that the package to be declared is @emotion/react rather than @emotion/styled.

tsx
styled-components

只有注入了 styled-components 的 ThemeContext ,才能响应自定义 token

@emotion/styled

antd-style 内置了 @emotion/react 的 ThemeContext,所以默认可以响应自定义 Token

全局设定 Styled 配置

在入口文件中设定 styled 的 ThemeContext 配置,可以让所有 ThemeProvider 都能响应自定义 token