shadcn/ui Community Best Practices
Comprehensive best practices guide for shadcn/ui applications, maintained by the shadcn/ui community. Contains 58 rules across 10 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Installing and configuring shadcn/ui in a project
- Writing new shadcn/ui components or composing primitives
- Implementing forms with React Hook Form and Zod validation
- Building data tables or handling large dataset displays
- Customizing themes or adding dark mode support
- Reviewing code for accessibility compliance
Rule Categories by Priority
Quick Reference
1. CLI & Project Setup (CRITICAL)
setup-components-json[blocked] - Configure components.json before adding componentssetup-path-aliases[blocked] - Configure TypeScript path aliases to match components.jsonsetup-cn-utility[blocked] - Create the cn utility before using componentssetup-use-cli-not-copy[blocked] - Use CLI to add components instead of copy-pastesetup-css-variables-theme[blocked] - Enable CSS variables for consistent themingsetup-rsc-configuration[blocked] - Set RSC flag based on framework support
2. Component Architecture (CRITICAL)
arch-use-asChild-for-custom-triggers[blocked] - Use asChild prop for custom trigger elementsarch-preserve-radix-primitive-structure[blocked] - Maintain Radix compound component hierarchyarch-extend-variants-with-cva[blocked] - Use Class Variance Authority for type-safe variantsarch-use-cn-for-class-merging[blocked] - Use cn() utility for safe Tailwind class mergingarch-forward-refs-for-composable-components[blocked] - Forward refs for form and focus integrationarch-isolate-component-variants[blocked] - Separate base styles from variant-specific styles
3. Accessibility Preservation (CRITICAL)
ally-preserve-aria-attributes[blocked] - Keep Radix ARIA attributes intactally-provide-sr-only-labels[blocked] - Add screen reader labels for icon buttonsally-maintain-focus-management[blocked] - Preserve focus trapping in modalsally-preserve-keyboard-navigation[blocked] - Keep WAI-ARIA keyboard patternsally-ensure-color-contrast[blocked] - Maintain WCAG color contrast ratiosally-dialog-title-required[blocked] - Always include DialogTitle for screen readersally-form-field-labels[blocked] - Associate labels with form controlsally-aria-invalid-errors[blocked] - Use aria-invalid for form error statesally-checkbox-label-association[blocked] - Wrap Checkbox with Label for click targetally-focus-visible-styles[blocked] - Preserve focus visible styles for keyboard navigation
4. Styling & Theming (HIGH)
style-use-css-variables-for-theming[blocked] - Use CSS variables for theme colorsstyle-avoid-important-overrides[blocked] - Never use !important for style overridesstyle-use-tailwind-theme-extend[blocked] - Extend Tailwind theme for design tokensstyle-consistent-spacing-scale[blocked] - Use consistent Tailwind spacing scalestyle-responsive-design-patterns[blocked] - Apply mobile-first responsive designstyle-dark-mode-support[blocked] - Support dark mode with CSS variables
5. Form Patterns (HIGH)
form-use-react-hook-form-integration[blocked] - Integrate with React Hook Formform-use-zod-for-schema-validation[blocked] - Use Zod for type-safe validationform-show-validation-errors-correctly[blocked] - Show errors at appropriate timesform-handle-async-validation[blocked] - Debounce async validation callsform-reset-form-state-correctly[blocked] - Reset form state after submission
6. Data Display (MEDIUM-HIGH)
data-use-tanstack-table-for-complex-tables[blocked] - Use TanStack Table for sorting/filteringdata-virtualize-large-lists[blocked] - Virtualize lists with 100+ itemsdata-use-skeleton-loading-states[blocked] - Use Skeleton for loading statesdata-paginate-server-side[blocked] - Paginate large datasets server-sidedata-empty-states-with-guidance[blocked] - Provide actionable empty states
7. Layout & Navigation (MEDIUM)
layout-sidebar-provider[blocked] - Wrap layout with SidebarProviderlayout-sidebar-collapsible[blocked] - Configure sidebar collapsible behaviorlayout-sidebar-groups[blocked] - Organize sidebar navigation with groupslayout-sheet-mobile-nav[blocked] - Use Sheet for mobile navigation overlaylayout-breadcrumb-navigation[blocked] - Implement breadcrumbs for deep navigation
8. Component Composition (MEDIUM)
comp-compose-with-compound-components[blocked] - Use compound component patternscomp-use-drawer-for-mobile-modals[blocked] - Use Drawer on mobile devicescomp-combine-command-with-popover[blocked] - Create searchable selects with Commandcomp-nest-dialogs-correctly[blocked] - Manage nested dialog focus correctlycomp-create-reusable-form-fields[blocked] - Extract reusable form field componentscomp-use-slot-pattern-for-flexibility[blocked] - Use slot pattern for flexible content
9. Performance Optimization (MEDIUM)
perf-lazy-load-heavy-components[blocked] - Lazy load components over 50KBperf-memoize-expensive-renders[blocked] - Memoize list items and expensive componentsperf-optimize-icon-imports[blocked] - Use direct imports for Lucide iconsperf-avoid-unnecessary-rerenders-in-forms[blocked] - Isolate form field watchingperf-debounce-search-inputs[blocked] - Debounce search and filter inputs
10. State Management (LOW-MEDIUM)
state-prefer-uncontrolled-for-simple-inputs[blocked] - Use uncontrolled for simple formsstate-lift-state-to-appropriate-level[blocked] - Lift state to lowest common ancestorstate-use-controlled-dialog-state[blocked] - Control dialogs for programmatic accessstate-colocate-state-with-components[blocked] - Keep state close to where it's used
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions [blocked] - Category structure and impact levels
- Rule template [blocked] - Template for adding new rules
Full Compiled Document
For a single-file reference containing all rules, see AGENTS.md [blocked].


