shadcn/ui Deconstructed
Shadcn/ui is not only great for building beautiful user interfaces, but it’s also an excellent way to learn coding patterns and techniques to build a robust set of UI components or a design system. In this series, we will reverse-engineer the code generated by shadcn/ui to explore various composition patterns and styling techniques.
Progress
Series Info
- Episodes
- 10
- Run Time
- 2h 28m
- Difficulty
- Advanced
- Last Updated
- May 20, 2025
- Version
- Latest
Series Episodes
- Introduction (2)
Introduction
Shadcn/ui is not only great for building beautiful user interfaces, but it’s also an excellent way to learn coding patterns and techniques to build a robust set of UI components or a design system. In this series, we will reverse-engineer the code generated by shadcn/ui to explore various composition patterns and styling techniques.CLI-Driven Code Generation
Let’s create a fresh Laravel project using the React Starter Kit. After taking a brief look at what’s included, we’ll delete unnecessary files and pretend we’re adding shadcn/ui components from scratch. We’ll use Git diffs to observe what happens when we install a Button component with the shadcn/ui CLI.
- Styling Utilities (4)
The cn Function, Part 1: clsx
Our first exploration focuses on the cn utility function exported by shadcn/ui. It’s used extensively across components, and it’s important to understand how it works. Let’s dive in!The cn Function, Part 2: Tailwind Merge
By combining class strings with clsx, we encountered issues where certain styles weren’t applied correctly. Let’s inspect the class attribute in the DOM to understand the problem and explore how the Tailwind Merge library can resolve it.Multi-Style Variant Components
Let’s examine what it takes to support multiple style variants for a UI component. We’ll analyze the anatomy of the Button component in shadcn/ui and recreate our own multi-style button component from scratch.Class Variance Authority
The manual setup of multi-style variant support we implemented in the previous episode provides the perfect context to demonstrate how Class Variance Authority (cva) streamlines managing multiple style combinations for a component.
- Composition and Theming (4)
The asChild Prop
The asChild prop in shadcn/ui is useful for rendering a component as a different HTML element. It leverages Radix UI’s Slot utility under the hood. Let’s break it down and recreate a basic version from scratch.Component Composition
It’s easy to fall into the trap of creating a complicated ad-hoc props API for UI components. React’s children prop enables simpler, more flexible component composition.Compound Components
Compound components elevate composition by embedding implicit state and logic within the root component. Sub-components are intended for use only inside the root component, as they share internal behavior.Theming
The theming setup in shadcn/ui is conceptually simple yet powerful. Let’s explore how semantic token names, combined with theme scopes, enable an elaborate multi-theme strategy.
