Chakra UI 设计系统 #
设计系统概述 #
Chakra UI 内置了一套完整的设计系统,包括颜色、排版、间距、断点等。这些设计令牌(Design Tokens)确保了整个应用的视觉一致性。
text
Chakra UI 设计系统
├── Colors 颜色系统
├── Typography 排版系统
├── Spacing 间距系统
├── Sizes 尺寸系统
├── Borders 边框系统
├── Shadows 阴影系统
├── Breakpoints 断点系统
└── Z-Index 层级系统
颜色系统 #
默认颜色调色板 #
Chakra UI 提供了丰富的颜色调色板,每种颜色都有 50-900 共 10 个色阶:
jsx
<Box bg="blue.50">blue.50</Box>
<Box bg="blue.100">blue.100</Box>
<Box bg="blue.200">blue.200</Box>
<Box bg="blue.300">blue.300</Box>
<Box bg="blue.400">blue.400</Box>
<Box bg="blue.500">blue.500</Box>
<Box bg="blue.600">blue.600</Box>
<Box bg="blue.700">blue.700</Box>
<Box bg="blue.800">blue.800</Box>
<Box bg="blue.900">blue.900</Box>
可用颜色 #
| 颜色名 | 用途 |
|---|---|
| gray | 中性色 |
| red | 错误/危险 |
| orange | 警告 |
| yellow | 提示 |
| green | 成功 |
| teal | 信息 |
| blue | 主要操作 |
| cyan | 辅助信息 |
| purple | 特殊强调 |
| pink | 强调色 |
语义颜色 #
jsx
<Box color="gray.500">次要文字</Box>
<Text color="red.500">错误信息</Text>
<Text color="green.500">成功信息</Text>
<Badge colorScheme="blue">标签</Badge>
自定义颜色 #
jsx
import { extendTheme } from '@chakra-ui/react'
const theme = extendTheme({
colors: {
brand: {
50: '#e3f2fd',
100: '#bbdefb',
200: '#90caf9',
300: '#64b5f6',
400: '#42a5f5',
500: '#2196f3',
600: '#1e88e5',
700: '#1976d2',
800: '#1565c0',
900: '#0d47a1',
},
},
})
排版系统 #
字体族 #
jsx
const theme = extendTheme({
fonts: {
heading: `'Heading Font', sans-serif`,
body: `'Body Font', sans-serif`,
mono: `'Code Font', monospace`,
},
})
字体大小 #
Chakra UI 使用 t-shirt 尺寸命名法:
| Token | 大小 |
|---|---|
| xs | 12px |
| sm | 14px |
| md | 16px |
| lg | 18px |
| xl | 20px |
| 2xl | 24px |
| 3xl | 30px |
| 4xl | 36px |
| 5xl | 48px |
| 6xl | 60px |
| 7xl | 72px |
| 8xl | 96px |
| 9xl | 128px |
jsx
<Text fontSize="xs">超小文字</Text>
<Text fontSize="sm">小号文字</Text>
<Text fontSize="md">中等文字</Text>
<Text fontSize="lg">大号文字</Text>
<Text fontSize="xl">超大文字</Text>
<Heading fontSize="2xl">标题</Heading>
字重 #
jsx
<Text fontWeight="hairline">最细</Text>
<Text fontWeight="thin">细体</Text>
<Text fontWeight="light">轻体</Text>
<Text fontWeight="normal">正常</Text>
<Text fontWeight="medium">中等</Text>
<Text fontWeight="semibold">半粗</Text>
<Text fontWeight="bold">粗体</Text>
<Text fontWeight="extrabold">特粗</Text>
<Text fontWeight="black">最粗</Text>
行高 #
jsx
<Text lineHeight="none">行高 1</Text>
<Text lineHeight="shorter">行高 1.25</Text>
<Text lineHeight="short">行高 1.375</Text>
<Text lineHeight="normal">行高 1.5</Text>
<Text lineHeight="tall">行高 1.625</Text>
<Text lineHeight="taller">行高 2</Text>
间距系统 #
Chakra UI 使用 4px 为基础单位的间距系统:
| Token | 像素值 |
|---|---|
| 0 | 0px |
| 1 | 4px |
| 2 | 8px |
| 3 | 12px |
| 4 | 16px |
| 5 | 20px |
| 6 | 24px |
| 7 | 28px |
| 8 | 32px |
| 9 | 36px |
| 10 | 40px |
| 12 | 48px |
| 14 | 56px |
| 16 | 64px |
| 20 | 80px |
| 24 | 96px |
| 28 | 112px |
| 32 | 128px |
使用间距 #
jsx
<Box p={4}>内边距 16px</Box>
<Box m={2}>外边距 8px</Box>
<Box px={6} py={3}>水平内边距 24px,垂直内边距 12px</Box>
<Box mt={8} mb={4}>上边距 32px,下边距 16px</Box>
间距简写 #
| 属性 | 说明 |
|---|---|
m |
margin(四边) |
mt |
margin-top |
mr |
margin-right |
mb |
margin-bottom |
ml |
margin-left |
mx |
margin-left + margin-right |
my |
margin-top + margin-bottom |
p |
padding(四边) |
pt |
padding-top |
pr |
padding-right |
pb |
padding-bottom |
pl |
padding-left |
px |
padding-left + padding-right |
py |
padding-top + padding-bottom |
尺寸系统 #
宽度和高度 #
jsx
<Box w="100%">宽度 100%</Box>
<Box w="50%">宽度 50%</Box>
<Box w="300px">固定宽度 300px</Box>
<Box w="auto">自动宽度</Box>
<Box maxW="container.lg">最大宽度</Box>
<Box minH="100vh">最小高度</Box>
容器尺寸 #
| Token | 宽度 |
|---|---|
| container.sm | 640px |
| container.md | 768px |
| container.lg | 1024px |
| container.xl | 1280px |
jsx
<Container maxW="container.md">
内容居中,最大宽度 768px
</Container>
边框系统 #
圆角 #
| Token | 值 |
|---|---|
| none | 0 |
| sm | 0.125rem |
| base | 0.25rem |
| md | 0.375rem |
| lg | 0.5rem |
| xl | 0.75rem |
| 2xl | 1rem |
| 3xl | 1.5rem |
| full | 9999px |
jsx
<Box borderRadius="sm">小圆角</Box>
<Box borderRadius="md">中等圆角</Box>
<Box borderRadius="lg">大圆角</Box>
<Box borderRadius="full">完全圆形</Box>
边框宽度 #
jsx
<Box borderWidth="1px">细边框</Box>
<Box borderWidth="2px">中等边框</Box>
<Box borderWidth="4px">粗边框</Box>
阴影系统 #
jsx
<Box shadow="sm">小阴影</Box>
<Box shadow="base">基础阴影</Box>
<Box shadow="md">中等阴影</Box>
<Box shadow="lg">大阴影</Box>
<Box shadow="xl">超大阴影</Box>
<Box shadow="2xl">特大阴影</Box>
<Box shadow="inner">内阴影</Box>
<Box shadow="outline">轮廓阴影</Box>
断点系统 #
默认断点 #
| 断点 | 最小宽度 |
|---|---|
| base | 0px |
| sm | 480px |
| md | 768px |
| lg | 992px |
| xl | 1280px |
| 2xl | 1536px |
响应式语法 #
jsx
<Box
width={{
base: '100%',
md: '50%',
lg: '25%'
}}
>
响应式宽度
</Box>
<Text
fontSize={{
base: 'md',
md: 'lg',
lg: 'xl'
}}
>
响应式字体大小
</Text>
<Flex
direction={{
base: 'column',
md: 'row'
}}
>
响应式布局方向
</Flex>
显示/隐藏 #
jsx
<Box display={{ base: 'block', md: 'none' }}>
仅在移动端显示
</Box>
<Box display={{ base: 'none', md: 'block' }}>
仅在桌面端显示
</Box>
使用 useBreakpointValue #
jsx
import { useBreakpointValue } from '@chakra-ui/react'
function ResponsiveComponent() {
const variant = useBreakpointValue({
base: 'outline',
md: 'solid',
lg: 'ghost'
})
return <Button variant={variant}>响应式按钮</Button>
}
Z-Index 层级 #
| Token | 值 | 用途 |
|---|---|---|
| hide | -1 | 隐藏元素 |
| auto | auto | 自动 |
| base | 0 | 基础层级 |
| docked | 10 | 停靠元素 |
| dropdown | 1000 | 下拉菜单 |
| sticky | 1100 | 粘性元素 |
| banner | 1200 | 横幅 |
| overlay | 1300 | 遮罩层 |
| modal | 1400 | 模态框 |
| popover | 1500 | 弹出框 |
| skipLink | 1600 | 跳转链接 |
| toast | 1700 | 提示消息 |
| tooltip | 1800 | 工具提示 |
jsx
<Box zIndex="modal">模态框层级</Box>
<Box zIndex="tooltip">工具提示层级</Box>
设计令牌总结 #
text
设计令牌体系
├── 颜色 (Colors)
│ ├── 调色板 (50-900)
│ ├── 语义色 (success, error, warning)
│ └── 品牌色 (brand)
│
├── 排版 (Typography)
│ ├── 字体族 (fonts)
│ ├── 字体大小 (fontSizes)
│ ├── 字重 (fontWeights)
│ └── 行高 (lineHeights)
│
├── 间距 (Spacing)
│ ├── 内边距 (padding)
│ └── 外边距 (margin)
│
├── 尺寸 (Sizes)
│ ├── 宽度 (width)
│ ├── 高度 (height)
│ └── 最大/最小尺寸
│
├── 边框 (Borders)
│ ├── 圆角 (borderRadius)
│ └── 边框宽度 (borderWidth)
│
├── 阴影 (Shadows)
│ └── 盒阴影 (boxShadow)
│
└── 断点 (Breakpoints)
└── 响应式断点
下一步 #
现在你已经了解了 Chakra UI 的设计系统,接下来学习 Style Props,学习如何使用样式属性快速构建界面!
最后更新:2026-03-28