shadcn/ui in React: Components You Own, Edit and Commit
shadcn/ui copies a button, a card and a dialog into your src folder as plain files. Set it up with init and add, read the cva variants in button.tsx, turn every default button indigo with one edit, and use Card and Dialog in a small page.
What you will learn
- Set up shadcn/ui in a Vite React app with init and add
- Explain what components.json, the cn helper and the theme variables in index.css are for
- Read the cva variants in button.tsx and change one variant for the whole app
- Decide when to own component code and when to use a library
Before you start
- Tailwind CSS 4 in React
- React components and props
Think of a sofa from a shop. You pick the size and the colour from the label, but the inside always stays the shop's. A normal component library works like that. It lives in node_modules, and you use what it offers.
Now think of flat-pack furniture. The parts arrive in your room. You screw them together, you repaint them, you can even saw a leg shorter. That is shadcn/ui. The components are copied into your own src folder as plain source files. After the copy, it is just your code.
In this article you add a button, a card and a dialog to a React app, look inside the button, and change it for the whole app with one edit.
Set it up in four commands
npm create vite@latest
npm i tailwindcss @tailwindcss/vite
npx shadcn@latest init
npx shadcn@latest add button card dialog
- A Vite project with the React and TypeScript template.
- Tailwind CSS 4, the styling engine. If it is new to you, start with Tailwind CSS 4 in React.
initasks you to pick a style and writes the setup, once.addcopies the components in.
Look at the output of the last two commands:
✔ Writing components.json.
✔ Updating src/index.css
✔ Created 3 files:
- src/components/ui/button.tsx
- src/components/ui/card.tsx
- src/components/ui/dialog.tsx
Three files created. There is no package to import the button from. The code is in your project now.
What init wrote: three small changes
First, components.json, a small map. It says where the files go and which style you picked:
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "radix-nova",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "",
"css": "src/index.css",
"baseColor": "neutral",
"cssVariables": true,
"prefix": ""
},
"iconLibrary": "lucide",
"rtl": false,
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "@/components/ui",
"lib": "@/lib",
"hooks": "@/hooks"
},
"menuColor": "default",
"menuAccent": "subtle",
"registries": {}
}
The ui alias is @/components/ui, so that is where add puts the files. The style here is radix-nova.
Second, a helper in src/lib/utils.ts called cn. It joins class names together. In this project the whole file is one line:
export { cn } from "cn"
Third, in src/index.css, the theme colours are stored as CSS variables, like --background and --primary. Change them and the look of every component changes. Here is the start of that block:
:root {
--background: oklch(1 0 0);
--foreground: oklch(0.145 0 0);
--card: oklch(1 0 0);
--card-foreground: oklch(0.145 0 0);
--popover: oklch(1 0 0);
--popover-foreground: oklch(0.145 0 0);
--primary: oklch(0.205 0 0);
--primary-foreground: oklch(0.985 0 0);
Open them, edit them, commit them
The three new files sit in src/components/ui: button.tsx, card.tsx and dialog.tsx. They are not hidden in node_modules. They are normal files. Open them, edit them, commit them to git, and your teammates review every change like the rest of your code.
Underneath, they use Radix UI, which takes care of focus and the keyboard. Tailwind takes care of how they look.
button.tsx: a plain file
The top of button.tsx is a call to cva (class variance authority). It is one list of looks: every variant of the button, written in one place. This is how the file arrived from add, lines 9 to 21:
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/80",
outline:
"border-border bg-background hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
secondary:
"bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
ghost:
"hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
destructive:
"bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
link: "text-primary underline-offset-4 hover:underline",
},
Line 11, the default variant, is just Tailwind classes: bg-primary text-primary-foreground. Nothing is hidden. Then outline, secondary, ghost, destructive and link.
Further down, line 43, is the Button function:
function Button({
className,
variant = "default",
size = "default",
asChild = false,
...props
}: React.ComponentProps<"button"> &
VariantProps<typeof buttonVariants> & {
asChild?: boolean
}) {
const Comp = asChild ? Slot.Root : "button"
return (
<Comp
data-slot="button"
data-variant={variant}
data-size={size}
className={cn(buttonVariants({ variant, size, className }))}
{...props}
/>
)
}
It takes a variant and a size, with "default" as the fallback, and passes them to buttonVariants. A normal function, nothing magical.
Change one variant, everywhere
Say you want the default button in indigo. Change line 11:
// before
default: "bg-primary text-primary-foreground hover:bg-primary/80",
// after
default: "bg-indigo-600 text-white hover:bg-indigo-500",
That is the whole edit.

Every default button in the app follows, because they all use this one file. That is the power of owning the code. You do not wait for a library to add an option. You change the file.
Cards and buttons, put to work
Now a small page. In App.tsx, the task data:
const tasks = [
{ title: "Write report", owner: "Emma", due: "Friday" },
{ title: "Email Jack", owner: "Priya", due: "Monday" },
{ title: "Book flights", owner: "Lucas", due: "Next week" },
]
And the page itself:
export default function App() {
return (
<main className="mx-auto max-w-md space-y-4 p-8">
<header className="flex items-center justify-between">
<h1 className="text-2xl font-semibold">Team tasks</h1>
<NewTask />
</header>
{tasks.map((t) => (
<Card key={t.title}>
<CardHeader>
<CardTitle>{t.title}</CardTitle>
<CardDescription>
{t.owner} · due {t.due}
</CardDescription>
<CardAction>
<Button variant="outline">Open</Button>
</CardAction>
</CardHeader>
</Card>
))}
</main>
)
}
You import Button and use Card, CardHeader, CardTitle and CardDescription like any other component. Inside CardAction is a button with the outline variant. The layout classes on main, like max-w-md and space-y-4, are plain Tailwind. The page shows three task cards for Emma, Priya and Lucas, with the indigo New task button on top.
A dialog that behaves
The New task button opens a dialog. Here is the component:
function NewTask() {
return (
<Dialog>
<DialogTrigger asChild>
<Button>New task</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>New task</DialogTitle>
<DialogDescription>Give the task a short title.</DialogDescription>
</DialogHeader>
<input
className="h-9 w-full rounded-md border px-3 text-sm"
placeholder="Plan the offsite"
/>
<DialogFooter>
<DialogClose asChild>
<Button variant="outline">Cancel</Button>
</DialogClose>
<Button>Save</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}

DialogTrigger is the button that opens it. DialogContent is the box in the middle. And the good part: Escape closes it, and keyboard focus stays inside while it is open. Radix does that work, and since the file is yours, you can still change it. There is more on that keyboard behaviour in accessibility in practice.
A library or your own copy?
| Question | Component library | Components you own |
|---|---|---|
| Change the look | Props and themes only | Edit the file |
| Updates | npm update brings fixes | You merge changes yourself |
| Good for | Big parts you never touch | Everyday UI in your own style |
Merging updates yourself is the price. If you need a custom look, own the code. If you need a heavy widget exactly as it is, use a library.
Common mistakes
- Looking for the button in
node_modules. It is insrc/components/ui. That is the point. - Overriding the same classes on every
<Button>. If every default button should be indigo, change the variant once inbutton.tsx. - Forgetting the
@/alias. The imports use@/components/ui/button, so the alias must be set intsconfigandvite.config.ts.
Recap
The parts are copied into your project as plain files. init once, add a component, then edit it, because it is your file now. Edit one variant and every button follows. We covered the foundations, and with them AI can generate the rest. Try it yourself: add a success variant in green to buttonVariants and use it on the Save button.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
One Request, Two Answers: the Same Post as an HTML Page and as JSON
Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.
A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.
How a Web Page Works: HTML Tags, Elements and Attributes
A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.
Semantic HTML: header, nav, main, aside and footer as Page Landmarks
The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.
