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

Table of Contents

Introduction
Theme Switching
Usage of useTheme
Injecting Custom Token Themes
Typescript Type Support
Basic Style Reset
Consuming Static Instance Methods
Adding Scoped Styles to the Container
Nested ThemeProvider
Integration with styled
API
Creating Styles
createStyles
🚧 createStylish
createGlobalStyle
Container Components
ThemeProvider
StyleProvider
Hooks
useTheme
useThemeMode
useResponsive
Advanced Settings
🚧 setupStyled
createInstance

ThemeProvider

A container for globally managing theme variables. It provides a unified entry point for one-click switching between light and dark themes, custom themes, and application-level scoped styles.
引入方法
js
import { ThemeProvider } from 'antd-style';
NPM
UNPKG
BundlePhobia
PackagePhobia
Anvaka Graph
编辑文档
PREV
createGlobalStyle
NEXT
StyleProvider

相关资源

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 & 数字科技
‌
‌
‌
‌

Introduction

ThemeProvider is a container for globally managing theme variables. Based on encapsulating the ConfigProvider, this component provides a unified entry point for one-click switching between light and dark themes, custom themes, and application-level scoped styles. At the same time, the useTheme method under this container can obtain the theme object within this layer.

ThemeProvider is essentially a React Context data container used to provide related theme consumption data for child applications. Therefore:

  • It integrates the basic state of switching between light and dark themes
  • Supports automatic response to system themes
  • Provides a callback object for the antd static instance method (this static method can respond to themes normally)
  • This component does not have a real DOM node, so it cannot limit the scope of custom styles through node styles

Theme Switching

ThemeProvider integrates the ability to switch between light and dark themes by default, and you can quickly complete the switching between light and dark themes through the appearance props. For more details about the theme switching ability of antd-style, you can refer to Theme Switching.

Usage of useTheme

After wrapping the top-level with ThemeProvider, use useTheme to get the theme object, which includes antd v5 token theme values, custom themes, and current theme appearance methods. For detailed API, see

tsx

Example effect:

Tips

useTheme will look for the theme value in the nearest ThemeProvider component. If there are multiple nested layers, only the innermost layer will take effect.

Injecting Custom Token Themes

You can inject custom tokens through the customToken method of ThemeProvider and consume them through the useTheme method.

tsx

Example effect:

Typescript Type Support

By extending the type definition of the CustomToken interface for antd-style, you can add corresponding token type definitions to the useTheme hooks.

At the same time, by adding generics to the ThemeProvider object, you can constrain the input parameter definition of customToken.

tsx
INFO

Since CustomToken is most likely an empty interface, if the project has a rule to configure @typescript-eslint/no-empty-interface, the interface definition will be corrected to type during code formatting, and since type cannot be extended, it will cause loss of prompts (related issue: #16). Therefore, the solution is to add a disabled rule as shown in the example code above.

Basic Style Reset

The default effect of the a node in the dumi document is as follows.

By resetting the style of the antd App component, you can ensure that native tags (<a>, etc.) not in the antd component also conform to the default style of antd (bottom left).

Consuming Static Instance Methods

In v5, due to the issue of react lifecycle, static methods were removed, so if you use the default exported static methods of antd such as message, they will not respond to dynamic themes. Therefore, ThemeProvider provides a getStaticInstance interface to provide the static method instance that responds to dynamic themes for external use.

tsx

Adding Scoped Styles to the Container

If you need to affect only the styles under ThemeProvider, you can combine the className props of the antd App component with the css or createStyles method to add local scoped styles.

Global

In the world of css-in-js, local scope is very easy to achieve. Therefore, global scope usage should be minimized as much as possible (use global scope only if necessary). This is also the recommended practice in antd v5. However, if you still need to inject styles at the global scope level, you can use createGlobalStyles to achieve this.

Nested ThemeProvider

In some scenarios, we may need to nest another ThemeProvider within a ThemeProvider. In this case, it should be noted that the inner ThemeProvider will override the outer ThemeProvider.

Integration with styled

antd-style provides compatibility with styled-components' ThemeProvider through the styled method, and then uses antd-style's ThemeProvider to provide theme consumption for styled-components or the emotion/styled library.

tsx

API

NameDefault ValueDescription
customTokenundefinedCustom tokens, can be extended and added to your own needs based on the antd v5 token specification
customStylishundefinedCustom Stylish variables
appearancelightThe appearance theme of the application, built-in light and dark themes, can be extended by yourself
defaultAppearance
onAppearanceChange(appearance:ThemeAppearance) => voidCallback for appearance theme
themeModelightTheme display mode, with three configurations: follow system, light, dark. Automatic mode is not enabled by default and needs to be configured manually
defaultThemeModenullDefault theme display mode
onThemeModeChange(themeMode: ThemeMode) => voidCallback after theme mode is modified
index.tsx
_app.tsx
style.ts
tsx
index.tsx
_app.tsx
tsx
ThemeProvider + App

由于 App 有 DOM 节点,能限制作用域来修改 DOM 原生样式,因此可以重置原生节点样式

ThemeProvider

由于 ThemeProvider 没有有根 DOM 节点,因此无法修改原生节点样式(除非搭配 GlobalStyle,而搭配 GlobalStyle 则会污染全局样式)

包裹 ThemeProvider
#363eba
colorPrimary
未包裹 ThemeProvider 会显示默认值
#1677ff
colorPrimary
antd Token
#1677ff
colorPrimary
自定义 Token
#c956df
customBrandColor
未包裹 ThemeProvider 时使用自定义 Token 无效
None
customBrandColor