fix: 移除全局CSS变量的导入并更新默认CSS变量配置
一个用于管理 CSS 变量和主题切换的 React 库。
npm install @ant-design/theme-token # 或 yarn add @ant-design/theme-token # 或 pnpm add @ant-design/theme-token
theme 对象是基于 global 对象自动生成的,它将 CSS 变量名转换为驼峰命名法的属性名,方便在 CSS-in-JS 中使用。
theme
global
import { theme } from '@ant-design/theme-token'; // 使用 theme 对象 const styles = { margin: theme.marginNone, // 'var(--margin-none)' padding: theme.marginComponentBase, // 'var(--margin-component-base)' color: theme.colorGrayText, // 'var(--color-gray-text)' borderRadius: '4px', };
import React from 'react'; import { theme, useCSSVariables } from '@ant-design/theme-token'; const MyComponent: React.FC = () => { // 注入 CSS 变量 useCSSVariables('MyComponent', { '--margin-none': '0', '--margin-component-base': '8px', '--color-gray-text': '#333', }); return ( <div style={{ margin: theme.marginNone, padding: theme.marginComponentBase, color: theme.colorGrayText, }} > 使用 theme 对象的组件 </div> ); };
import React from 'react'; import { ThemeProvider } from '@ant-design/theme-token'; const App: React.FC = () => { const [isDark, setIsDark] = React.useState(false); return ( <ThemeProvider className={isDark ? 'dark-theme' : 'light-theme'}> <div> <button onClick={() => setIsDark(!isDark)}>切换主题</button> <MyComponent /> </div> </ThemeProvider> ); };
import styled from 'styled-components'; import { theme } from '@ant-design/theme-token'; const StyledButton = styled.button` margin: ${theme.marginComponentBase}; padding: 8px 16px; background-color: ${theme.colorBlueControlFillPrimary}; border-radius: 4px; color: #ffffff; &:hover { background-color: ${theme.colorBlueControlFillPrimaryHover}; } `;
import { css } from '@emotion/react'; import { theme } from '@ant-design/theme-token'; const buttonStyles = css` margin: ${theme.marginComponentBase}; padding: 8px 16px; background-color: ${theme.colorBlueControlFillPrimary}; border-radius: 4px; color: #ffffff; &:hover { background-color: ${theme.colorBlueControlFillPrimaryHover}; } `;
theme 对象包含以下类别的属性:
marginNone
marginComponentXs
marginComponentSm
marginComponentBase
marginComponentLg
marginBlockXs
marginBlockSm
marginBlockBase
marginBlockXl
marginSection2xs
marginSectionXs
marginSectionSm
marginSectionBase
marginSectionLg
marginSectionXl
colorGrayText
colorGrayTextSecondary
colorGrayTextLight
colorGrayTextDisabled
colorBlueText
colorBlueTextContrast
colorBlueControlFillPrimary
colorBlueControlFillPrimaryHover
用于注入 CSS 变量的 React Hook。
useCSSVariables(componentName: string, cssVariables: CSSVariables)
用于提供主题上下文的 React 组件。
<ThemeProvider className={className}>{children}</ThemeProvider>
TypeScript 用户可以获得完整的类型支持:
import { theme, Theme } from '@ant-design/theme-token'; // theme 对象有完整的类型定义 const margin: string = theme.marginNone; // ✅ 类型安全 // 可以定义使用 theme 的样式对象类型 type MyStyles = { margin: Theme['marginNone']; padding: Theme['marginComponentBase']; color: Theme['colorGrayText']; };
var(--variable-name)
useCSSVariables
ThemeProvider
# 安装依赖 npm install # 运行测试 npm test # 构建 npm run build
MIT
Theme Token
一个用于管理 CSS 变量和主题切换的 React 库。
功能特性
安装
基本用法
使用 Theme 对象
theme对象是基于global对象自动生成的,它将 CSS 变量名转换为驼峰命名法的属性名,方便在 CSS-in-JS 中使用。在 React 组件中使用
主题切换
在 styled-components 中使用
在 emotion 中使用
API 参考
theme 对象
theme对象包含以下类别的属性:间距 (Margin)
marginNone- 无间距marginComponentXs- 组件内小间距marginComponentSm- 组件内中小间距marginComponentBase- 组件内基础间距marginComponentLg- 组件内大间距marginBlockXs- 区块内小间距marginBlockSm- 区块内中小间距marginBlockBase- 区块内基础间距marginBlockXl- 区块内超大间距marginSection2xs- 区块间超小间距marginSectionXs- 区块间小间距marginSectionSm- 区块间中小间距marginSectionBase- 区块间基础间距marginSectionLg- 区块间大间距marginSectionXl- 区块间超大间距颜色 (Color)
colorGrayText- 灰色文本colorGrayTextSecondary- 灰色次要文本colorGrayTextLight- 灰色浅色文本colorGrayTextDisabled- 灰色禁用文本colorBlueText- 蓝色文本colorBlueTextContrast- 蓝色对比文本colorBlueControlFillPrimary- 蓝色主按钮填充colorBlueControlFillPrimaryHover- 蓝色主按钮悬停填充useCSSVariables
用于注入 CSS 变量的 React Hook。
ThemeProvider
用于提供主题上下文的 React 组件。
类型支持
TypeScript 用户可以获得完整的类型支持:
注意事项
var(--variable-name)useCSSVariables或ThemeProvider注入了相应的 CSS 变量开发
许可证
MIT