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