name: frontend-design-systems description: 系统化方法,用于构建一致、可维护的前端UI组件,采用设计系统和组件库 license: MIT metadata: adapted-by: ai-skills category: frontend-development
前端设计系统
使用系统化设计模式构建一致、可扩展的UI组件。
设计令牌系统
颜色令牌
// tokens/colors.ts
export const colors = {
primary: {
50: '#e3f2fd',
100: '#bbdefb',
500: '#2196f3',
900: '#0d47a1',
},
neutral: {
50: '#fafafa',
100: '#f5f5f5',
500: '#9e9e9e',
900: '#212121',
},
semantic: {
success: '#4caf50',
warning: '#ff9800',
error: '#f44336',
info: '#2196f3',
}
};
间距比例
export const spacing = {
xs: '0.25rem', // 4px
sm: '0.5rem', // 8px
md: '1rem', // 16px
lg: '1.5rem', // 24px
xl: '2rem', // 32px
'2xl': '3rem', // 48px
};
排版系统
export const typography = {
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif'],
mono: ['Fira Code', 'monospace'],
},
fontSize: {
xs: ['0.75rem', { lineHeight: '1rem' }],
sm: ['0.875rem', { lineHeight: '1.25rem' }],
base: ['1rem', { lineHeight: '1.5rem' }],
lg: ['1.125rem', { lineHeight: '1.75rem' }],
xl: ['1.25rem', { lineHeight: '1.75rem' }],
},
fontWeight: {
normal: '400',
medium: '500',
semibold: '600',
bold: '700',
}
};
组件模式
基础按钮组件
interface ButtonProps {
variant?: 'primary' | 'secondary' | 'outline';
size?: 'sm' | 'md' | 'lg';
disabled?: boolean;
loading?: boolean;
children: React.ReactNode;
onClick?: () => void;
}
export function Button({
variant = 'primary',
size = 'md',
disabled = false,
loading = false,
children,
onClick,
}: ButtonProps) {
const variants = {
primary: 'bg-primary-500 text-white hover:bg-primary-600',
secondary: 'bg-neutral-200 text-neutral-900 hover:bg-neutral-300',
outline: 'border-2 border-primary-500 text-primary-500 hover:bg-primary-50',
};
const sizes = {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg',
};
return (
<button
className={`rounded-lg font-medium transition-colors ${variants[variant]} ${sizes[size]}`}
disabled={disabled || loading}
onClick={onClick}
>
{loading ? <Spinner /> : children}
</button>
);
}
复合组件
// 卡片复合组件模式
export function Card({ children }: { children: React.ReactNode }) {
return <div className="rounded-lg border shadow-sm">{children}</div>;
}
Card.Header = function CardHeader({ children }: { children: React.ReactNode }) {
return <div className="px-6 py-4 border-b">{children}</div>;
};
Card.Body = function CardBody({ children }: { children: React.ReactNode }) {
return <div className="px-6 py-4">{children}</div>;
};
Card.Footer = function CardFooter({ children }: { children: React.ReactNode }) {
return <div className="px-6 py-4 border-t bg-neutral-50">{children}</div>;
};
// 使用示例
<Card>
<Card.Header>标题</Card.Header>
<Card.Body>内容</Card.Body>
<Card.Footer>操作</Card.Footer>
</Card>
布局模式
响应式网格系统
function GridLayout({ children }: { children: React.ReactNode }) {
return (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{children}
</div>
);
}
容器模式
function Container({ children }: { children: React.ReactNode }) {
return (
<div className="container mx-auto px-4 sm:px-6 lg:px-8 max-w-7xl">
{children}
</div>
);
}
状态管理模式
表单状态
function useForm<T>(initialValues: T) {
const [values, setValues] = useState(initialValues);
const [errors, setErrors] = useState<Partial<Record<keyof T, string>>>({});
const handleChange = (name: keyof T, value: any) => {
setValues(prev => ({ ...prev, [name]: value }));
setErrors(prev => ({ ...prev, [name]: undefined }));
};
const validate = (schema: z.ZodSchema<T>) => {
const result = schema.safeParse(values);
if (!result.success) {
const fieldErrors = result.error.formErrors.fieldErrors;
setErrors(fieldErrors as any);
return false;
}
return true;
};
return { values, errors, handleChange, validate };
}
可访问性模式
// 焦点管理
function Dialog({ open, onClose, children }: DialogProps) {
const dialogRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (open) {
dialogRef.current?.focus();
}
}, [open]);
return (
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
tabIndex={-1}
onKeyDown={(e) => {
if (e.key === 'Escape') onClose();
}}
>
{children}
</div>
);
}
集成点
互补技能:
- canvas-design:用于设计集成
- accessibility-patterns:用于ARIA/WCAG标准
- responsive-design-patterns:用于移动优先设计
- testing-patterns:用于组件测试