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

Table of Contents

1. Switching between Light and Dark Themes
2. Automatically Responding to System Themes
3. Controlled Mode for Theme Switching
4. Customizing Themes with Theme Configuration
API
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
Writing Style
NEXT
Custom Theme

相关资源

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

Theme Switching

Nowadays, the ability to switch between light and dark themes has gradually become a default feature on PCs, mobile phones, and other devices. Browsers also provide the ability to use media queries to help us quickly adapt to light themes. In Ant Design V5, we have implemented a more elegant runtime theme switching capability based on the Css in Js.

However, due to Ant Design's positioning as a basic component, it only provides basic APIs for theme capabilities and does not offer an out-of-the-box solution for application-level dynamic themes. Therefore, antd-style provides a complete application-level dynamic theme solution based on Ant Design's dynamic theme foundation.

1. Switching between Light and Dark Themes

By modifying the appearance props on the container component ThemeProvider, you can achieve theme switching. This is also the simplest way to use dynamic themes.

tsx
If

In Ant Design V5, the static methods like message, notification, and modal do not respond to themes. It is recommended to use the hook versions of the static methods.

If you need to use these three static methods and also want them to respond to themes, please refer to Static Method Theme Response.

2. Automatically Responding to System Themes

After introducing the above solution, attentive individuals should notice that this approach heavily relies on manual theme switching by users. However, the system-level theme switching on mobile phones and computers is generally automatically completed over time and usually does not require manual operation by users. Therefore, a modern theme switching capability should provide an "automatic" mode like macOS does.

In ThemeProvider, we provide the themeMode prop. For scenarios that do not require manual control of themes, you can directly set themeMode="auto" to achieve automatic switching of system theme modes with one click.

tsx

:::info{title=What is the difference between appearance and themeMode?} appearance describes the current appearance state of the application, while themeMode is used to describe the logic for controlling theme modes. Distinguishing between the two helps to achieve advanced theme capabilities.

See discussion: #52 :::

3. Controlled Mode for Theme Switching

However, in many scenarios, we need to provide users with the autonomy to switch modes as they wish. We provide the useThemeMode hook, which is used to obtain the current theme mode and the capability to switch theme modes.

In addition to the useThemeMode hook, controlled mode can also be implemented through the themeMode prop.

WARNING

Note: It is currently not recommended to simultaneously control themeMode and appearance, as this may lead to unexpected issues. The linkage capability in this area will be enhanced in the future.

4. Customizing Themes with Theme Configuration

Ant Design V5's ConfigProvider provides theme configuration, allowing you to pass in a custom theme object to customize the theme.

tsx

API

Refer to the APIs related to theme switching: ThemeProvider

自动响应系统主题

当切换系统的主题时,该 demo 会响应系统主题的切换,用户无需做任何操作

使用 useThemeMode 切换主题

该 Demo 中演示了使用 useThemeMode 切换主题的能力

受控模式下的主题切换

该 Demo 中演示了受控模式的主题切换能力,使用了 zustand 作为全局状态管理方案。

传入 Antd Theme 主题

传入不同的 token 、算法组成的 theme ,可以实现主题风格的自定义