1---2name: frontend-patterns3description: >4 Frontend development patterns and best practices. Use for component architecture, state management, CSS patterns, accessibility, performance optimization, and design system integration.5---67# Frontend Patterns89## 1. Component Architecture10- **Atomic Design**: Organize components into Atoms, Molecules, Organisms, Templates, and Pages.11- **Compound Components**: Use context to share implicit state between parent and child components (e.g., `<Select>`, `<Select.Option>`).12- **Render Props**: Share code between React components using a prop whose value is a function.13- **Custom Hooks**: Extract stateful logic from a component so it can be tested independently and reused.1415## 2. State Management16- **Local State**: Use `useState` or `useReducer` for state that is isolated to a single component or small subtree.17- **Global State**: Use context API, Redux, Zustand, or Jotai for application-wide state.18- **Server State**: Use React Query, SWR, or Apollo Client to manage asynchronous data fetching, caching, synchronization, and error handling.19- **Form State**: Use libraries like React Hook Form or Formik for complex forms.2021## 3. CSS Patterns22- **Utility-first CSS**: Tailwind CSS conventions for rapid styling and consistency.23- **CSS Modules**: Scope CSS locally by default to avoid class name collisions.24- **Styled-components**: Write actual CSS in JavaScript, leveraging tagged template literals.2526## 4. Performance Optimization27- **Code Splitting**: Use `React.lazy` and Suspense to lazy load components.28- **Memoization**: Use `React.memo`, `useMemo`, and `useCallback` to prevent unnecessary re-renders.29- **Virtual Scrolling**: Render only the visible items in a long list to save memory and processing time.3031## 5. Accessibility (A11y)32- **WCAG 2.1 Checklist**: Follow guidelines for contrast, text resizing, and keyboard navigability.33- **ARIA Patterns**: Use ARIA attributes appropriately to enhance semantics for screen readers.34- **Focus Management**: Ensure logical tab order and visible focus states.3536## 6. Testing37- **Component Testing**: Use React Testing Library to test components from the user's perspective.38- **Visual Regression**: Compare screenshots of components to detect unintended visual changes (e.g., Chromatic, Percy).39- **E2E Testing**: Automate browser actions to test critical user flows (e.g., Playwright, Cypress).4041## 7. Design System Integration42- **Token Consumption**: Map design tokens (colors, typography, spacing) to CSS variables or a theme provider.43- **Component Composition**: Build complex UIs by composing simpler, reusable design system components.4445## 8. SEO46- **Meta Tags**: Use next/head or react-helmet for dynamic meta tags.47- **Structured Data**: Inject JSON-LD to help search engines understand page content.48- **SSR/SSG**: Leverage Next.js or Remix for server-side rendering or static site generation.