Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Browse all series

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

  1. Introduction (2)
    1. 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.
    2. 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.
  2. Styling Utilities (4)
    1. 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!
    2. 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.
    3. 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.
    4. 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.
  3. Composition and Theming (4)
    1. 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.
    2. 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.
    3. 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.
    4. 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.

Continue Learning