Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+272 -190
View File
@@ -1,207 +1,289 @@
---
description: This rule enforces best practices and coding standards for Chakra UI projects, including accessibility, styling, performance, and security. It aims to provide clear, actionable guidance for developers using Chakra UI.
globs: *.js,*.jsx,*.ts,*.tsx
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: **/*
---
- **Accessibility**: Ensure all components follow WAI-ARIA standards, including keyboard navigation and screen reader support.
# chakra-ui Best Practices
- Use semantic HTML elements where appropriate (e.g., `<button>`, `<input>`, `<nav>`).
- Provide alternative text for images using the `alt` prop on the `<Image>` component.
- Ensure sufficient color contrast between text and background colors. Use a tool like [WebAIM's Color Contrast Checker](https://webaim.org/resources/contrastchecker/) to verify.
- Use the `aria-label`, `aria-labelledby`, and `aria-describedby` props to provide descriptive information to assistive technologies.
- Test your components with a screen reader (e.g., NVDA, VoiceOver) to ensure they are accessible.
- **Prop-Based Styling**: Use style props instead of CSS classes for faster and more intuitive styling.
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.
- Leverage Chakra UI's extensive set of style props for common CSS properties (e.g., `m`, `p`, `bg`, `color`, `fontSize`, `fontWeight`).
- Use the `sx` prop for more advanced styling that goes beyond the available style props. However, prefer style props where applicable for consistency.
- Apply responsive styles using array or object syntax for different breakpoints.
- **Centralized Theming**: Manage colors, sizes, and styles in one place for consistency across the application.
## 1. Core Setup & Dependencies
- Extend the default Chakra UI theme using the `extendTheme` function to customize colors, fonts, sizes, and component styles.
- Use semantic tokens for theme values (e.g., `colors.brand.primary`, `sizes.spacing.md`) instead of hardcoding values directly in your components.
- Use the `useTheme` hook to access theme values within your components.
- Organize your theme file to ensure easy modifications and readability
- **Modular Components**: Build complex UIs by combining small, reusable components like `Box`, `Flex`, and `Stack`.
Always ensure correct installation and root provider setup for Chakra UI v2+ (React 18+).
- Follow atomic design principles to structure your components into atoms, molecules, and organisms.
- Create custom components that encapsulate specific UI patterns and logic.
- Use composition to build more complex components from simpler ones.
- Use the `forwardRef` API when building components that need to directly access a DOM node.
- **Responsive Design**: Use array syntax for responsive styles and leverage hooks like `useBreakpointValue` for adaptive layouts.
❌ 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+
}
- Use the array syntax to define different values for different breakpoints (e.g., `fontSize={['sm', 'md', 'lg']}`).
- Use the object syntax for more explicit breakpoint control (e.g., `templateColumns={{ base: '1fr', md: 'repeat(2, 1fr)' }}`).
- Use the `useBreakpointValue` hook to dynamically adjust layouts based on the current breakpoint.
- Utilize `useMediaQuery` hook for conditional rendering of components or sections.
- **Simplicity and Composition**: Keep component APIs simple and break them down into smaller parts to maintain flexibility.
// App.jsx (missing ChakraProvider)
function App() {
return <HomePage />;
}
```
- Design components with a minimal set of props to keep their APIs clean and easy to use.
- Use composition to allow consumers to customize the appearance and behavior of your components.
- Avoid creating monolithic components with too many responsibilities.
- **Custom Theme Creation**: Extend the default theme to match design requirements and ensure consistent styling.
✅ GOOD: Use React 18+ and all required peer dependencies. Wrap your app with `<ChakraProvider>`.
```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"
}
- Use `extendTheme` to customize the default Chakra UI theme and create consistent styling.
- Define custom color palettes, typography, spacing, and breakpoints.
- Override component styles to match your design system.
- **Testing and Documentation**: Document components and their usage, and ensure thorough testing for accessibility and functionality.
// 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
- Write unit tests for your components using Jest and React Testing Library.
- Test for accessibility using `axe-core` and `jest-axe`.
- Document your components with clear and concise JSDoc comments.
- Use a tool like Storybook to create a living style guide for your components.
- Use Typescript to ensure you have strongly typed components.
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<ChakraProvider theme={theme}>
<App />
</ChakraProvider>
</React.StrictMode>,
);
```
- **Code Organization and Structure:**
- **Directory Structure Best Practices:**
- Structure your project based on features or components.
- Common structure: `src/components`, `src/pages`, `src/utils`, `src/theme`, `src/hooks`.
- Place related components, hooks, and styles in the same directory.
- **File Naming Conventions:**
- Use PascalCase for component file names (e.g., `Button.tsx`).
- Use camelCase for hook file names (e.g., `useDisclosure.ts`).
- Use descriptive names for utility functions (e.g., `formatDate.ts`).
- **Module Organization:**
- Group related components and functions into modules.
- Use `index.ts` files to re-export members from a module.
- Avoid circular dependencies between modules.
- **Component Architecture:**
- Use a component-based architecture with reusable components.
- Separate presentational components from container components.
- Prefer functional components with hooks over class components.
- **Code Splitting Strategies:**
- Use dynamic imports to load components and modules on demand.
- Split your application into routes or feature modules.
- Use React.lazy and Suspense for code splitting at the component level.
## 2. Theming: Token-First & Recipes
- **Common Patterns and Anti-patterns:**
- **Design Patterns Specific to Chakra UI:**
- **Compound Components:** Create components like `Tabs` and `Accordion` where child components share state and logic.
- **Control Props:** Use props like `value` and `onChange` for controlled components, enabling external state management.
- **Render Props:** Provide a render prop to allow consumers to customize the rendering of a component.
- **Recommended Approaches for Common Tasks:**
- Use Chakra UI's layout components (`Box`, `Flex`, `Grid`, `Stack`) for structuring your UI.
- Use the `useDisclosure` hook for managing the state of modals, drawers, and other toggleable components.
- Use the `useColorMode` hook for implementing dark mode support.
- **Anti-patterns and Code Smells to Avoid:**
- Avoid using inline styles directly; prefer style props or the `sx` prop.
- Avoid mutating the Chakra UI theme directly; always use `extendTheme` to create a new theme.
- Avoid tightly coupling components to specific data sources; make them more generic and reusable.
- **State Management Best Practices:**
- Use local component state for simple UI state.
- Use context for sharing state between components that are not directly related.
- Use a state management library like Redux or Zustand for more complex application state.
- **Error Handling Patterns:**
- Use try-catch blocks to handle errors gracefully.
- Display user-friendly error messages to the user.
- Log errors to a monitoring service for debugging.
- Implement fallback components to handle unexpected errors during rendering.
Centralize your design system. Define semantic tokens and use `defineRecipe` for component variants.
- **Performance Considerations:**
- **Optimization Techniques:**
- Use `React.memo` to memoize functional components and prevent unnecessary re-renders.
- Use `useCallback` and `useMemo` to memoize functions and values that are used as props to child components.
- Avoid creating new objects or functions in render methods.
- **Memory Management:**
- Clean up event listeners and timers in `useEffect` hooks.
- Avoid storing large amounts of data in component state.
- Use weak references to prevent memory leaks.
- **Rendering Optimization:**
- Use virtualization libraries like `react-window` or `react-virtualized` to render large lists efficiently.
- Optimize expensive calculations and data transformations.
- Use the `shouldComponentUpdate` lifecycle method or `React.PureComponent` to prevent unnecessary re-renders in class components (avoid when using hooks).
- **Bundle Size Optimization:**
- Use tree shaking to remove unused code from your bundles.
- Use code splitting to load only the code that is needed for a particular page or feature.
- Use image optimization techniques to reduce the size of your images.
- **Lazy Loading Strategies:**
- Use `React.lazy` and `Suspense` for lazy loading components.
- Use intersection observers to lazy load images and other assets that are not initially visible.
### 2.1. Define Semantic Tokens
Avoid hardcoding values. Define colors, fonts, and spacing once with `defineTokens`.
- **Security Best Practices:**
- **Common Vulnerabilities and How to Prevent Them:**
- **Cross-Site Scripting (XSS):** Sanitize user input to prevent the execution of malicious scripts.
- **Cross-Site Request Forgery (CSRF):** Use CSRF tokens to protect against unauthorized requests.
- **SQL Injection:** Use parameterized queries or an ORM to prevent SQL injection attacks.
- **Input Validation:**
- Validate all user input on both the client and server sides.
- Use regular expressions or validation libraries to enforce data formats.
- Escape or encode user input before displaying it in the UI.
- **Authentication and Authorization Patterns:**
- Use a secure authentication protocol like OAuth 2.0 or OpenID Connect.
- Store passwords securely using a hashing algorithm like bcrypt.
- Implement role-based access control (RBAC) to restrict access to sensitive data and functionality.
- **Data Protection Strategies:**
- Encrypt sensitive data at rest and in transit.
- Use HTTPS to secure communication between the client and server.
- Protect API keys and other secrets by storing them in environment variables.
- **Secure API Communication:**
- Use HTTPS for all API requests.
- Validate API responses to ensure they are in the expected format.
- Implement rate limiting to prevent abuse of your APIs.
❌ BAD: Inline hex codes or magic numbers.
```javascript
<Box bg="#0FEE0F" p="16px" />
```
- **Testing Approaches:**
- **Unit Testing Strategies:**
- Write unit tests for individual components and functions.
- Test for different input values and edge cases.
- Use mocking and stubbing to isolate components from external dependencies.
- **Integration Testing:**
- Write integration tests to verify the interaction between components.
- Test the flow of data between components.
- Test the integration with external APIs and services.
- **End-to-End Testing:**
- Write end-to-end tests to simulate user interactions and verify the overall functionality of the application.
- Use a testing framework like Cypress or Puppeteer.
- Test for accessibility and performance.
- **Test Organization:**
- Organize your tests into separate directories for unit tests, integration tests, and end-to-end tests.
- Use descriptive names for your test files and test cases.
- Follow a consistent testing style.
- **Mocking and Stubbing:**
- Use mocking to replace external dependencies with mock objects that simulate their behavior.
- Use stubbing to replace specific methods or properties of a dependency with predefined values.
- Use a mocking library like Jest's `jest.mock` or `sinon.js`.
✅ 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' }
},
});
- **Common Pitfalls and Gotchas:**
- **Frequent Mistakes Developers Make:**
- Not following accessibility guidelines.
- Overusing inline styles.
- Mutating the Chakra UI theme directly.
- Not testing components thoroughly.
- Not optimizing performance.
- **Edge Cases to Be Aware Of:**
- Handling different screen sizes and orientations.
- Handling different input methods (keyboard, mouse, touch).
- Handling different locales and languages.
- Handling different network conditions.
- **Version-Specific Issues:**
- Be aware of breaking changes between Chakra UI versions.
- Read the release notes carefully when upgrading.
- Test your application thoroughly after upgrading.
- **Compatibility Concerns:**
- Ensure your application is compatible with different browsers and devices.
- Use a tool like BrowserStack or Sauce Labs to test your application on different environments.
- **Debugging Strategies:**
- Use the browser's developer tools to inspect the DOM and debug your JavaScript code.
- Use a debugger like VS Code's built-in debugger.
- Use logging statements to track the flow of data and execution.
// src/theme/index.js
import { extendTheme } from '@chakra-ui/react';
import { tokens } from './tokens';
const theme = extendTheme({ tokens });
export default theme;
- **Tooling and Environment:**
- **Recommended Development Tools:**
- VS Code with the ESLint, Prettier, and TypeScript extensions.
- Chrome Developer Tools or Firefox Developer Tools.
- React Developer Tools browser extension.
- **Build Configuration:**
- Use a build tool like Webpack, Parcel, or Rollup.
- Configure your build tool to optimize your bundles and perform tree shaking.
- Use environment variables to configure your application for different environments.
- **Linting and Formatting:**
- Use ESLint with a configuration like eslint-config-react-app to enforce code style and best practices.
- Use Prettier to format your code automatically.
- Configure your editor to automatically lint and format your code on save.
- **Deployment Best Practices:**
- Use a continuous integration and continuous deployment (CI/CD) pipeline to automate your deployments.
- Deploy your application to a hosting provider like Netlify, Vercel, or AWS.
- Use a content delivery network (CDN) to cache your static assets.
- **CI/CD Integration:**
- Integrate your CI/CD pipeline with your testing and linting tools.
- Automate the process of building, testing, and deploying your application.
- Use a CI/CD platform like GitHub Actions, CircleCI, or Travis CI.
// YourComponent.jsx
<Box bg="primary" p="md" />
```
### 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 <ChakraButton bg="teal.600" color="white">{children}</ChakraButton>;
}
return <ChakraButton bg="gray.200">{children}</ChakraButton>;
};
```
✅ 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';
<Button variant="primary">Click Me</Button>
```
## 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
<Box backgroundColor="red.500" margin="4" padding="16px" width="100%" />
```
✅ GOOD: Use `bg`, `m`, `p`, `w`.
```javascript
<Box bg="red.500" m="4" p="4" w="full" />
```
### 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
<Button className="my-button" />
```
✅ GOOD: Use `_hover`, `_focus`, `_active`, `_disabled`.
```javascript
<Button bg="tomato" _hover={{ bg: "green.500" }} _focus={{ shadow: "outline" }} />
```
### 3.3. `Box` as the Styling Primitive
Use `Box` for layout and atomic styling. It's the foundation for all Chakra components.
❌ BAD: Using `div` with inline styles or external CSS for basic layout.
```javascript
<div style={{ display: 'flex', padding: '16px' }}>...</div>
```
✅ GOOD: Use `Box` (or `Flex`, `Grid`) with Chakra props.
```javascript
import { Box, Flex } from '@chakra-ui/react';
<Flex p="4">...</Flex>
```
## 4. Component Architecture & Accessibility
Build composable, semantic, and accessible components.
### 4.1. Composition over Wrapper Divs
Compose Chakra components directly. Avoid unnecessary `div` wrappers.
❌ BAD: Redundant `div`s around Chakra components.
```javascript
<div>
<Text>Hello</Text>
</div>
```
✅ GOOD: Use Chakra components directly or `Box` when a wrapper is needed for styling.
```javascript
<Text>Hello</Text>
// Or, if styling is needed:
<Box p="2">
<Text>Hello</Text>
</Box>
```
### 4.2. Use the `as` Prop for Semantic HTML
Ensure your components render as the correct semantic HTML element.
❌ BAD: Using `Box` for everything, losing semantic meaning.
```javascript
<Box onClick={...}>Clickable text</Box> // Renders as <div>, not a button
```
✅ GOOD: Use `as` to render the appropriate HTML tag.
```javascript
<Box as="button" onClick={...}>Clickable text</Box>
<Box as="section" p="8">Section Content</Box>
```
### 4.3. Leverage Built-in Accessibility
Chakra components are accessible by default. Focus on content and semantic usage.
❌ BAD: Manually adding ARIA attributes to standard Chakra components.
```javascript
<Button aria-label="Submit form">Submit</Button> // Redundant for a standard button
```
✅ GOOD: Trust Chakra's defaults; add ARIA only for custom components or complex interactions.
```javascript
<Button>Submit</Button>
```
## 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 <Box p={padding}>...</Box>;
};
```
✅ GOOD: Use `useBreakpointValue`.
```javascript
import { Box, useBreakpointValue } from '@chakra-ui/react';
const MyComponent = () => {
const padding = useBreakpointValue({ base: '4', md: '8' });
return <Box p={padding}>...</Box>;
};
```
### 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';
<Box bg={bgColor} />
```
✅ 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 <head>
// <ColorModeScript initialColorMode={theme.config.initialColorMode} />
const MyComponent = () => {
const { colorMode, toggleColorMode } = useColorMode();
return (
<Box bg={colorMode === 'dark' ? 'gray.800' : 'white'}>
<Button onClick={toggleColorMode}>
Toggle {colorMode === 'light' ? 'Dark' : 'Light'}
</Button>
</Box>
);
};
```