core-components
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCore Components
核心组件
Design System Overview
设计系统概述
Use components from your core library instead of raw platform components. This ensures consistent styling and behavior.
请使用核心库中的组件,而非原生平台组件。这能确保样式与行为的一致性。
Design Tokens
设计令牌
NEVER hard-code values. Always use design tokens.
绝对不要硬编码数值,请始终使用设计令牌。
Spacing Tokens
间距令牌
tsx
// CORRECT - Use tokens
<Box padding="$4" marginBottom="$2" />
// WRONG - Hard-coded values
<Box padding={16} marginBottom={8} />| Token | Value |
|---|---|
| 4px |
| 8px |
| 12px |
| 16px |
| 24px |
| 32px |
tsx
// CORRECT - Use tokens
<Box padding="$4" marginBottom="$2" />
// WRONG - Hard-coded values
<Box padding={16} marginBottom={8} />| 令牌 | 数值 |
|---|---|
| 4px |
| 8px |
| 12px |
| 16px |
| 24px |
| 32px |
Color Tokens
颜色令牌
tsx
// CORRECT - Semantic tokens
<Text color="$textPrimary" />
<Box backgroundColor="$backgroundSecondary" />
// WRONG - Hard-coded colors
<Text color="#333333" />
<Box backgroundColor="rgb(245, 245, 245)" />| Semantic Token | Use For |
|---|---|
| Main text |
| Supporting text |
| Disabled/hint text |
| Brand/accent color |
| Error states |
| Success states |
tsx
// CORRECT - Semantic tokens
<Text color="$textPrimary" />
<Box backgroundColor="$backgroundSecondary" />
// WRONG - Hard-coded colors
<Text color="#333333" />
<Box backgroundColor="rgb(245, 245, 245)" />| 语义令牌 | 适用场景 |
|---|---|
| 主文本 |
| 辅助文本 |
| 禁用/提示文本 |
| 品牌/强调色 |
| 错误状态 |
| 成功状态 |
Typography Tokens
排版令牌
tsx
<Text fontSize="$lg" fontWeight="$semibold" />| Token | Size |
|---|---|
| 12px |
| 14px |
| 16px |
| 18px |
| 20px |
| 24px |
tsx
<Text fontSize="$lg" fontWeight="$semibold" />| 令牌 | 字号 |
|---|---|
| 12px |
| 14px |
| 16px |
| 18px |
| 20px |
| 24px |
Core Components
核心组件
Box
Box
Base layout component with token support:
tsx
<Box
padding="$4"
backgroundColor="$backgroundPrimary"
borderRadius="$lg"
>
{children}
</Box>支持令牌的基础布局组件:
tsx
<Box
padding="$4"
backgroundColor="$backgroundPrimary"
borderRadius="$lg"
>
{children}
</Box>HStack / VStack
HStack / VStack
Horizontal and vertical flex layouts:
tsx
<HStack gap="$3" alignItems="center">
<Icon name="user" />
<Text>Username</Text>
</HStack>
<VStack gap="$4" padding="$4">
<Heading>Title</Heading>
<Text>Content</Text>
</VStack>水平与垂直弹性布局:
tsx
<HStack gap="$3" alignItems="center">
<Icon name="user" />
<Text>Username</Text>
</HStack>
<VStack gap="$4" padding="$4">
<Heading>Title</Heading>
<Text>Content</Text>
</VStack>Text
Text
Typography with token support:
tsx
<Text
fontSize="$lg"
fontWeight="$semibold"
color="$textPrimary"
>
Hello World
</Text>支持令牌的排版组件:
tsx
<Text
fontSize="$lg"
fontWeight="$semibold"
color="$textPrimary"
>
Hello World
</Text>Button
Button
Interactive button with variants:
tsx
<Button
onPress={handlePress}
variant="solid"
size="md"
isLoading={loading}
isDisabled={disabled}
>
Click Me
</Button>| Variant | Use For |
|---|---|
| Primary actions |
| Secondary actions |
| Tertiary/subtle actions |
| Inline actions |
带多种变体的交互按钮:
tsx
<Button
onPress={handlePress}
variant="solid"
size="md"
isLoading={loading}
isDisabled={disabled}
>
Click Me
</Button>| 变体 | 适用场景 |
|---|---|
| 主要操作 |
| 次要操作 |
| 三级/细微操作 |
| 内联操作 |
Input
Input
Form input with validation:
tsx
<Input
value={value}
onChangeText={setValue}
placeholder="Enter text"
error={touched ? errors.field : undefined}
label="Field Name"
/>带验证功能的表单输入框:
tsx
<Input
value={value}
onChangeText={setValue}
placeholder="Enter text"
error={touched ? errors.field : undefined}
label="Field Name"
/>Card
Card
Content container:
tsx
<Card padding="$4" gap="$3">
<CardHeader>
<Heading size="sm">Card Title</Heading>
</CardHeader>
<CardBody>
<Text>Card content</Text>
</CardBody>
</Card>内容容器组件:
tsx
<Card padding="$4" gap="$3">
<CardHeader>
<Heading size="sm">Card Title</Heading>
</CardHeader>
<CardBody>
<Text>Card content</Text>
</CardBody>
</Card>Layout Patterns
布局模式
Screen Layout
屏幕布局
tsx
const MyScreen = () => (
<Screen>
<ScreenHeader title="Page Title" />
<ScreenContent padding="$4">
{/* Content */}
</ScreenContent>
</Screen>
);tsx
const MyScreen = () => (
<Screen>
<ScreenHeader title="Page Title" />
<ScreenContent padding="$4">
{/* Content */}
</ScreenContent>
</Screen>
);Form Layout
表单布局
tsx
<VStack gap="$4" padding="$4">
<Input label="Name" {...nameProps} />
<Input label="Email" {...emailProps} />
<Button isLoading={loading}>Submit</Button>
</VStack>tsx
<VStack gap="$4" padding="$4">
<Input label="Name" {...nameProps} />
<Input label="Email" {...emailProps} />
<Button isLoading={loading}>Submit</Button>
</VStack>List Item Layout
列表项布局
tsx
<HStack
padding="$4"
gap="$3"
alignItems="center"
borderBottomWidth={1}
borderColor="$borderLight"
>
<Avatar source={{ uri: imageUrl }} size="md" />
<VStack flex={1}>
<Text fontWeight="$semibold">{title}</Text>
<Text color="$textSecondary" fontSize="$sm">{subtitle}</Text>
</VStack>
<Icon name="chevron-right" color="$textTertiary" />
</HStack>tsx
<HStack
padding="$4"
gap="$3"
alignItems="center"
borderBottomWidth={1}
borderColor="$borderLight"
>
<Avatar source={{ uri: imageUrl }} size="md" />
<VStack flex={1}>
<Text fontWeight="$semibold">{title}</Text>
<Text color="$textSecondary" fontSize="$sm">{subtitle}</Text>
</VStack>
<Icon name="chevron-right" color="$textTertiary" />
</HStack>Anti-Patterns
反模式
tsx
// WRONG - Hard-coded values
<View style={{ padding: 16, backgroundColor: '#fff' }}>
// CORRECT - Design tokens
<Box padding="$4" backgroundColor="$backgroundPrimary">
// WRONG - Raw platform components
import { View, Text } from 'react-native';
// CORRECT - Core components
import { Box, Text } from 'components/core';
// WRONG - Inline styles
<Text style={{ fontSize: 18, fontWeight: '600' }}>
// CORRECT - Token props
<Text fontSize="$lg" fontWeight="$semibold">tsx
// WRONG - Hard-coded values
<View style={{ padding: 16, backgroundColor: '#fff' }}>
// CORRECT - Design tokens
<Box padding="$4" backgroundColor="$backgroundPrimary">
// WRONG - Raw platform components
import { View, Text } from 'react-native';
// CORRECT - Core components
import { Box, Text } from 'components/core';
// WRONG - Inline styles
<Text style={{ fontSize: 18, fontWeight: '600' }}>
// CORRECT - Token props
<Text fontSize="$lg" fontWeight="$semibold">Component Props Pattern
组件属性模式
When creating components, use token-based props:
tsx
interface CardProps {
padding?: '$2' | '$4' | '$6';
variant?: 'elevated' | 'outlined' | 'filled';
children: React.ReactNode;
}
const Card = ({ padding = '$4', variant = 'elevated', children }: CardProps) => (
<Box
padding={padding}
backgroundColor="$backgroundPrimary"
borderRadius="$lg"
{...variantStyles[variant]}
>
{children}
</Box>
);创建组件时,请使用基于令牌的属性:
tsx
interface CardProps {
padding?: '$2' | '$4' | '$6';
variant?: 'elevated' | 'outlined' | 'filled';
children: React.ReactNode;
}
const Card = ({ padding = '$4', variant = 'elevated', children }: CardProps) => (
<Box
padding={padding}
backgroundColor="$backgroundPrimary"
borderRadius="$lg"
{...variantStyles[variant]}
>
{children}
</Box>
);Integration with Other Skills
与其他技能的集成
- react-ui-patterns: Use core components for UI states
- testing-patterns: Mock core components in tests
- storybook: Document component variants
- react-ui-patterns: 使用核心组件实现UI状态
- testing-patterns: 在测试中Mock核心组件
- storybook: 文档化组件变体