--- description: This guide provides opinionated, actionable best practices for using Chakra UI effectively, focusing on token-first theming, responsive design, component architecture, and performance for React 18+ applications. globs: **/* --- # chakra-ui Best Practices Chakra UI is our definitive choice for building accessible, design-system-driven React applications. Adhere to these guidelines for a consistent, maintainable, and performant codebase. ## 1. Core Setup & Dependencies Always ensure correct installation and root provider setup for Chakra UI v2+ (React 18+). ❌ BAD: Incorrect or missing dependencies, old React versions. ```javascript // package.json (missing emotion/framer-motion) "dependencies": { "@chakra-ui/react": "^2.x.x", "react": "^17.0.0" // Chakra v2 requires React 18+ } // App.jsx (missing ChakraProvider) function App() { return ; } ``` ✅ GOOD: Use React 18+ and all required peer dependencies. Wrap your app with ``. ```javascript // package.json "dependencies": { "@chakra-ui/react": "^2.x.x", "@emotion/react": "^11.x.x", "@emotion/styled": "^11.x.x", "framer-motion": "^6.x.x", // or newer compatible versions "react": "^18.x.x", "react-dom": "^18.x.x" } // src/index.jsx or src/main.jsx import { ChakraProvider } from '@chakra-ui/react'; import React from 'react'; import ReactDOM from 'react-dom/client'; // Use createRoot for React 18 import App from './App'; import theme from './theme'; // Import your custom theme ReactDOM.createRoot(document.getElementById('root')).render( , ); ``` ## 2. Theming: Token-First & Recipes Centralize your design system. Define semantic tokens and use `defineRecipe` for component variants. ### 2.1. Define Semantic Tokens Avoid hardcoding values. Define colors, fonts, and spacing once with `defineTokens`. ❌ BAD: Inline hex codes or magic numbers. ```javascript ``` ✅ GOOD: Use `defineTokens` and reference them via theme keys. ```javascript // src/theme/tokens.js import { defineTokens } from "@chakra-ui/react"; export const tokens = defineTokens({ colors: { primary: { value: "#0FEE0F" }, secondary: { value: "#EE0F0F" }, }, space: { 'md': { value: '16px' } }, }); // src/theme/index.js import { extendTheme } from '@chakra-ui/react'; import { tokens } from './tokens'; const theme = extendTheme({ tokens }); export default theme; // YourComponent.jsx ``` ### 2.2. Use `defineRecipe` for Component Variants Encapsulate component styles, especially for variants, using `defineRecipe`. ❌ BAD: Scattering variant styles as props or conditional logic in JSX. ```javascript const Button = ({ variant, children }) => { if (variant === 'primary') { return {children}; } return {children}; }; ``` ✅ GOOD: Define component recipes in your theme. ```javascript // src/theme/recipes/button.js import { defineRecipe } from "@chakra-ui/react"; export const buttonRecipe = defineRecipe({ base: { borderRadius: "md", fontWeight: "semibold", }, variants: { variant: { primary: { bg: "primary", color: "white", _hover: { bg: "primary.700" } }, secondary: { bg: "gray.200", color: "gray.800", _hover: { bg: "gray.300" } }, }, }, defaultVariants: { variant: "secondary", }, }); // src/theme/index.js (add to components) import { extendTheme } from '@chakra-ui/react'; import { buttonRecipe } from './recipes/button'; const theme = extendTheme({ components: { Button: buttonRecipe, }, }); // YourComponent.jsx import { Button } from '@chakra-ui/react'; ``` ## 3. Styling: Shorthand, Pseudo-Props & `Box` Leverage Chakra's powerful style props for declarative, responsive styling. ### 3.1. Prefer Shorthand Props Use concise shorthand for common CSS properties. ❌ BAD: Verbose CSS property names. ```javascript ``` ✅ GOOD: Use `bg`, `m`, `p`, `w`. ```javascript ``` ### 3.2. Use Pseudo Props for Interaction States Handle hover, focus, active, and disabled states directly in JSX. ❌ BAD: Separate CSS classes or conditional styling. ```javascript // styles.css .my-button:hover { background: green; } // MyButton.jsx // Redundant for a standard button ``` ✅ GOOD: Trust Chakra's defaults; add ARIA only for custom components or complex interactions. ```javascript ``` ## 5. Hooks Best Practices Utilize Chakra's hooks for dynamic and responsive behavior. ### 5.1. `useBreakpointValue` for Responsive Props Dynamically adjust values based on breakpoints. ❌ BAD: Manual media queries or `isLargerThan` checks. ```javascript const MyComponent = () => { const { width } = useWindowSize(); // Custom hook for window size const padding = width > 768 ? '8' : '4'; return ...; }; ``` ✅ GOOD: Use `useBreakpointValue`. ```javascript import { Box, useBreakpointValue } from '@chakra-ui/react'; const MyComponent = () => { const padding = useBreakpointValue({ base: '4', md: '8' }); return ...; }; ``` ### 5.2. `useColorMode` for Dark/Light Mode Manage color mode consistently across your application. ❌ BAD: Manually tracking theme state or using global CSS variables. ```javascript // Some custom context or global state for theme const { themeMode, toggleThemeMode } = useThemeContext(); const bgColor = themeMode === 'dark' ? 'gray.800' : 'white'; ``` ✅ GOOD: Use `useColorMode` and `ColorModeScript`. ```javascript import { Box, Button, useColorMode, ColorModeScript } from '@chakra-ui/react'; import theme from './theme'; // In your root index.html or equivalent, within // const MyComponent = () => { const { colorMode, toggleColorMode } = useColorMode(); return ( ); }; ```