--- description: Definitive guidelines for building consistent, performant, and maintainable UIs with Ant Design v6.x, focusing on design tokens, layout, and component usage. globs: **/* --- # ant-design Best Practices This guide outlines the definitive best practices for using Ant Design v6.x in our projects. Adhering to these standards ensures consistency, accessibility, and maintainability across our codebase. ## 1. Theming: Design Token Driven Always customize Ant Design's appearance using its design token system via `ConfigProvider`. Avoid direct CSS overrides for visual properties that can be controlled by tokens. ### ✅ GOOD: Centralized Token Customization Create a `theme.ts` file to export your theme configuration and apply it at the root of your application. Prefer `seedToken` adjustments for global changes and `componentToken` for specific component overrides. Utilize preset algorithms for light/dark mode. ```typescript // src/theme.ts import { theme } from 'antd'; import type { ThemeConfig } from 'antd'; export const appTheme: ThemeConfig = { algorithm: theme.defaultAlgorithm, // Or theme.darkAlgorithm, [theme.defaultAlgorithm, theme.darkAlgorithm] token: { colorPrimary: '#0050B3', // Our primary brand color colorSuccess: '#52C41A', fontSize: 14, borderRadius: 4, // Disable motion globally for accessibility/performance motion: false, }, components: { Button: { colorPrimary: '#1890FF', // Override primary button color algorithm: true, // Enable algorithm for component token derivation }, Layout: { headerBg: '#001529', footerBg: '#F0F2F5', }, }, }; ``` ```tsx // src/App.tsx (or root layout) import React from 'react'; import { ConfigProvider, App as AntdApp } from 'antd'; import { appTheme } from './theme'; import HomePage from './pages/HomePage'; const App: React.FC = () => { return ( {/* Use AntdApp for context-aware static methods (message, Modal, notification) */} ); }; export default App; ``` ### ❌ BAD: Direct CSS Overrides Avoid overriding Ant Design component styles with custom CSS classes or inline styles for properties that are themeable. This leads to inconsistent UIs and breaks theme updates. ```css /* ❌ BAD: Avoid this for themeable properties */ .ant-btn-primary { background-color: #f00 !important; /* Breaks theme */ border-color: #f00 !important; } ``` ## 2. Layout: Unified Grid System Adhere to Ant Design's 8-pixel grid unit and the unified 1440px canvas width. Use `Row` and `Col` components for structural layout, leveraging their `span` and `gutter` props. ### ✅ GOOD: Responsive Layout with Grid ```tsx import React from 'react'; import { Layout, Row, Col } from 'antd'; const { Header, Content, Footer } = Layout; const MyLayout: React.FC = () => (
{/* Header content */}
{/* 16px horizontal and vertical gutter */}

Welcome to our Application

{/* Responsive columns */}
Card 1
Card 2
Card 3
); export default MyLayout; ``` ### ❌ BAD: Hardcoding Layout Dimensions Avoid using fixed pixel values or custom CSS for spacing and alignment that should be handled by the grid system or design tokens. ```tsx // ❌ BAD: Avoid custom margins for layout
{/* Content */}
``` ## 3. Component Usage: Standardized & Accessible Import components directly from `antd`. Always use TypeScript and configure ESLint with `eslint-plugin-antd` to enforce best practices. Prioritize accessibility by providing necessary `aria-*` attributes. ### ✅ GOOD: Proper Imports and Accessibility ```tsx import React from 'react'; import { Button, Space, Typography } from 'antd'; import { PlusOutlined } from '@ant-design/icons'; const { Text } = Typography; const MyComponent: React.FC = () => { const handleClick = () => { console.log('Button clicked'); }; return ( Important information. ); }; export default MyComponent; ``` ### ❌ BAD: Missing Accessibility Attributes Interactive components must have appropriate `aria-*` attributes for screen reader users. ```tsx // ❌ BAD: Missing aria-label for icon-only button ); }; // Wrap components that use static methods or useToken inside const RootComponent: React.FC = () => ( ); export default RootComponent; ``` ### ❌ BAD: Static Methods Without Context Calling `message.info()` or `Modal.confirm()` directly without `App` component or `useMessage`/`useModal` hooks will result in them not inheriting `ConfigProvider` context (e.g., theme, locale). ```tsx // ❌ BAD: This message will not inherit ConfigProvider context import { message, Button } from 'antd'; const BadComponent: React.FC = () => ( ); ```