Sub topic 5
How shadcn/ui works: copy, own, and modify
This chapter assumes you know React function components, props, and basic TypeScript. It also assumes you can read Tailwind utility classes and have a working React project, such as Vite or Next.js. You should also know what a headless component is: a component that supplies behavior and accessibility but no visual styling.
The model in one paragraph
shadcn/ui is not a package you install and import from. It is a collection of component source files that you copy into your own repository. Each file combines two things you already know: a Radix UI primitive for behavior and accessibility, and Tailwind classes for appearance. After the copy, the files belong to you. You edit them like any other file in your project.
Library dependency versus owned code
A conventional library lives in node_modules. You configure it through props and theme options, and you upgrade by changing a version number. shadcn/ui inverts this.
| Aspect | Conventional library | shadcn/ui |
|---|---|---|
| Where the code lives | `node_modules` | Your `components/ui` folder |
| Changing a component's markup | Props, overrides, or a fork | Edit the file |
| Upgrading | Bump a version | Re-add or compare files yourself |
| Runtime dependencies | The whole library | Only the primitives you use |
| Styling hooks | Whatever the author exposes | Any class in the file |
The cost of this model is that updates are your job. The benefit is that you are never blocked by an API the author did not anticipate.
The three layers
Every shadcn/ui component stacks the same layers, each with one job.
- Radix primitive. Handles keyboard interaction, focus management, and ARIA attributes.
- Tailwind classes, with
cvaandcn. Provide appearance and variants. Later chapters cover both helpers. - The copied file. Wraps the two layers above into a component with a stable, small API.
What a copied component looks like
Here is a Radix dialog in the style shadcn/ui uses. The behavior comes from Radix. The only styling is the classes on the overlay and content.
import * as Dialog from "@radix-ui/react-dialog"
export function ConfirmDialog() {
return (
<Dialog.Root>
<Dialog.Trigger>Delete item</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay className="fixed inset-0 bg-black/50" />
<Dialog.Content className="fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 rounded-md bg-white p-6">
<Dialog.Title>Delete this item?</Dialog.Title>
<Dialog.Description>This action cannot be undone.</Dialog.Description>
<Dialog.Close>Cancel</Dialog.Close>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
)
}Without writing any code for it, this dialog traps focus, closes on Escape, and exposes the right ARIA attributes. Because the file is yours, changing the overlay color or the content width means editing one string.
Modifying a component
Suppose your design needs a darker overlay. In a conventional library you would look for a prop or a theme token. Here you change the class in the file:
<Dialog.Overlay className="fixed inset-0 bg-black/70" />That is the entire change. No override layer is involved.
Common mistakes
- Treating the files as untouchable. They are meant to be edited. Keep changes small and readable.
- Editing everywhere without a record. Heavy local changes make later comparison with upstream harder. Note significant changes in a comment or commit message.
- Expecting automatic updates. Nothing updates these files for you. A later chapter covers how to keep them current.
Exercise
A teammate says shadcn/ui is "just another component library, so we should wait for upstream to add a prop for our custom dialog width." What is wrong with that reasoning?
Show answer
The component source is copied into your repository, so you can change the width classes in the file yourself. You do not depend on upstream adding a prop. The trade-off is that you are responsible for tracking upstream changes afterward.