AI summary by TwinMind
How Shadcn/ui ACTUALLY Works
Theo - t3․gg
32m 37s
Introduction and Context
- Author excited about Manuba's blog post on Shad CN UI anatomy
- Shad UI distributed via CLI that puts source code directly into project (not npm package)
- Previously required copy-paste installation before CLI introduction
- Key benefit: developers own the code and can customize unlike traditional packages (Bootstrap, MUI)
Shad UI Philosophy and Design Principles
- Copy-paste approach gives ownership and control over code
- Design should be separate from implementation
- Style coupled with implementation is a drawback of npm packages
- Allows customization while starting with sensible defaults
CSS Library Classification Framework
- CSS Plus+ libraries: Extend CSS rather than provide it (Tailwind)
- Most Tailwind classes are one line of CSS
- Tailwind doesn't have inherent "look" - websites look same due to inspiration, not framework
- Behavior libraries: Define component behaviors (Radix, Headless UI)
- Handle accessibility, browser standards, complex interactions
- Critical for proper radio button, dropdown behaviors
- Style systems: Pre-built looks and styles (Bootstrap, Tailwind UI)
- Bootstrap makes websites look same due to same classes/properties
Shad UI's Unique Position
- Touches all three categories: uses Tailwind + Radix + acts as style system
- Puts code in codebase so no style system opinions to override
- Uses Radix for all primitive behaviors
- Uses Tailwind so not building alternative CSS framework
UI Component Architecture Breakdown
Behavior Layer
- UI components need user interaction awareness beyond native browser elements
- Custom components (tabs, accordion, date pickers) require complex behaviors
- Focus/blur states, keyboard navigation, ARIA compliance often overlooked
- Getting accessibility right is hard and slows development
- Tooling problem - should be easier to build accessible than inaccessible
Style Layer - Two Aspects
- Structural: border radius, dimensions, spacing, font sizes/weights
- Visual: foreground/background colors, outlines, borders
- Components need different states and variants based on user interactions
- Variants communicate state through visual adjustments
Accessibility Discussion
- Author advocates for better tooling to make accessibility easier
- Criticizes both sides: developers who ignore accessibility, advocates who pretend it's cost-free
- Tools like Radix, React Aria, Headless UI working to solve tooling problems
- Shad UI important piece of accessibility puzzle
Design-Development Relationship
- Author's experience with designer Iris at Twitch
- Designer asked good questions about CSS constraints (overflow rules vs popouts)
- Designer created layered solution that looked better and met technical constraints
- Importance of designers understanding engineering constraints
- Author became design advocate, bridging gap between design and engineering
Shad UI Architecture Overview
Style Layer Components
- Class Variance Authority (CVA): Manages component variants
- Tailwind: Core styling framework
- clsx and Tailwind merge: Handle conflicting styles and utility application
- Global style variables: Configure appearance via primary color variables
Structure and Behavior Layer
- Radix UI: Core headless components with proper behaviors
- TanStack React Table: Table functionality
- React Hook Form: Form state management
- React Day Picker: Calendar/date functionality
Implementation Deep Dive
Badge Component Analysis
- Simple starting point demonstrating Shad UI concepts
- Uses CVA for variant management (default, secondary, destructive, outline)
- Exports types extending HTML div attributes plus custom variant props
- CN helper applies badge variants and handles class name overrides
Class Name Utility (CN) Function
- Combines clsx and Tailwind merge
- clsx: Conditional class application via objects/arrays
- Tailwind merge: Resolves conflicting Tailwind classes by keeping last one
- Handles CSS cascading issues where HTML class order doesn't affect application order
Author's Disagreement on Tailwind Merge
- Disagrees with suggestion to use only clsx without Tailwind merge
- Problem: overrides work inconsistently based on CSS cascade order
- Better solution: remove className prop entirely if overrides not desired
- Half-working overrides worse than always-working or never-working
SOLID Principles Application
- Single Responsibility: Badge renders with different styles based on variant
- Open/Closed: New variants addable without modifying existing code
- Dependency Injection: Badge component and styling defined separately
- Consistency/Reusability: CVA promotes consistent variant management
- Separation of Concerns: Styling logic separate from rendering logic
Switch Component Analysis
- Interactive component unlike purely presentational badge
- Built on Radix UI switch primitives (root and thumb)
- Uses React.forwardRef for proper ref handling
- Expected behaviors: tab navigation, enter key toggle, screen reader announcements
- Author grateful for pre-built forward ref implementation (complex to get right)
Complex Components Overview
- Calendar, data table, form components deserve separate articles
- Table uses TanStack React Table (least painful table solution)
- Forms combine React Hook Form with Zod validation
- Date/time components use React Day Picker
Conclusion and Impact
- Shad UI represents paradigm shift in frontend development
- Own component implementation vs relying on third-party abstractions
- Build design system with customizable defaults
- Hasn't invented much but assembled pieces in optimal way
- Monumental/generational shift in component library thinking
