Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

React
  • One Request, Two Answers: the Same Post as an HTML Page and as JSON
  • A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
  • How a Web Page Works: HTML Tags, Elements and Attributes
  • Semantic HTML: header, nav, main, aside and footer as Page Landmarks
  • HTML Forms: Labels, Names, Input Types and Where the Data Goes (GET vs POST)
  • CSS Basics: Selectors, Which Rule Wins, and the Box Model
  • CSS Flexbox vs Grid: A Nav Bar, a Card Row and a Dashboard
  • Responsive CSS: Viewport, Mobile-First Media Queries, clamp() and Dark Mode with Tokens
  • JavaScript for People Who Already Code: Syntax, Modules and Five Surprises
  • JavaScript Arrays the Functional Way: map, filter, reduce, find, sort and Object.entries
  • Async JavaScript: Promises, async/await, fetch and Promise.all
  • JavaScript in the Page: the DOM, querySelector and Events
  • npm, package.json and Vite: What npm install Fetches and How Vite Serves and Builds Your Page
  • TypeScript Essentials: Types Checked, Then Erased (Interfaces, Unions, Generics)
  • Why React, JSX and Components: The Page as a Function of Data
  • React Props and Composition: Data Down, Calls Up, Children in the Slot
  • Routing With React Router: One Page, Many Addresses
  • React Component Tests With Vitest and Testing Library: Test What the User Sees
  • End-to-End Tests for a React App With Playwright: A Full Dress Rehearsal
  • Why AI-Built Websites All Look the Same, and Four Levels That Fix It
  • Visual Hierarchy, Spacing and the 8-Point Grid: Tell the Eye What to Read First
  • Colour, Contrast and Dark Palettes: 60-30-10, Semantic Tokens and Ratios You Can Compute
  • Typography That Sells: Type Scale, Line Length and Free Font Pairings
  • The UI Element Catalogue: Buttons to Toasts, Their Jobs, States and shadcn/ui Names
  • Design Tokens: Primitive and Semantic Tokens, from Figma to CSS to Tailwind
  • Tailwind CSS 4 in React: Utility Classes, Responsive Prefixes, Hover and Dark Mode
  • shadcn/ui in React: Components You Own, Edit and Commit
  • Design Tokens to a Tailwind Theme: Light and Dark Mode with @theme inline
  • Motion in React: Entrance, Hover, Exit and Layout Animations
  • A React Dashboard: Stat Cards, a Recharts Line Chart and a Sortable Table
  • Accessibility in Practice: Dialogs, Menus and the Keyboard in React
  • Reference-Driven Design: Extract a DESIGN.md From a Site You Admire and Build From It
  • Components, Variants and Variables: One Design System in Figma and in CSS
  • Figma MCP Server: Build a React Component From a Frame, Then Write It Back
  • Claude Design: From a Brief to a Clickable Prototype, Then Hand It Off to Claude Code
  • Critiquing AI-Generated UI: Six Checks for Every Screen
  • The AI Design Landscape: Prompt to UI, Prompt to App, and Design to Code
  • Your Brand Rules Folder: One Folder Every AI-Built Page Follows
  • The Best-Practice UI Prompt: Plain Sentences That Beat the Average AI Page
  • UI Ideas From Figma Make, Google Stitch and ChatGPT: One Prompt, Three Designs
  • Google Stitch From a Sketch or Screenshot: Show the Layout, Say What to Keep
  • Stitch to Figma to Code: Turn a Stitch Screen Into Code That Uses Your Tokens
  • Figma Make: Prompt to Prototype With a Four-Part Prompt
  • Upgrade a Dated UI From One Screenshot (or a Reference Image)
  • States and Feedback: Make Every Click Get an Answer
  • A Design-Loop Skill: Claude Screenshots, Scores and Fixes Its Own UI
  • Copy Without the AI Tells: A Copy Pass on a Landing Page
  • React State, Events, Lists and Conditional Rendering: Follow One Click
  • React useEffect: When It Runs, What Cleanup Is For, and When You Don't Need One
  • Lifting State Up, Refs and React DevTools: Put State Where It Is Needed
  • Forms and Actions in React 19: Controlled Inputs, Form Actions, useActionState and useFormStatus
  • React Context and Custom Hooks: Share One Value, Reuse the Logic
  • Server State With TanStack Query: One Key, One Request, One Cache
  • Mutations and Optimistic Updates in TanStack Query: Change Data, Then Tell the Cache
  • Generate TypeScript Types, Query Hooks and Zod From an OpenAPI Spec With Orval
  • A Mock Backend in the Browser With Mock Service Worker, Generated From the Spec
  • A React Client for a FastAPI API: The CORS Wall, CORSMiddleware and the Vite Proxy
  • Forms With React Hook Form and Zod: The API's Rules in the Browser, the Server's Errors Under the Field
  • JWT Auth in a React Client With a FastAPI Backend: Log In, Carry the Token, Let the Server Decide
  1. Blog
  2. /
  3. React
  4. /
  5. Fundamentals

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Fundamentals

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
  1. A Vite project with the React and TypeScript template.
  2. Tailwind CSS 4, the styling engine. If it is new to you, start with Tailwind CSS 4 in React.
  3. init asks you to pick a style and writes the setup, once.
  4. add copies 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.

The Team tasks page twice. Left: the New task button is black. Right: after the edit, the New task button is indigo. The three task cards with outline Open buttons look the same in both.
Before the edit the New task button is black. After it, indigo.

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} &middot; 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>
  )
}
The New task dialog open over the blurred Team tasks page: a title New task, the text Give the task a short title, an input with the placeholder Plan the offsite, a Save button and a Cancel button, and a close X in the corner.
The dialog open, with the page behind it blurred. This capture was taken before the indigo edit, so Save is still black.

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?

QuestionComponent libraryComponents you own
Change the lookProps and themes onlyEdit the file
Updatesnpm update brings fixesYou merge changes yourself
Good forBig parts you never touchEveryday 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 in src/components/ui. That is the point.
  • Overriding the same classes on every <Button>. If every default button should be indigo, change the variant once in button.tsx.
  • Forgetting the @/ alias. The imports use @/components/ui/button, so the alias must be set in tsconfig and vite.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.

Keep going in React · 27 of 59

Previous
Tailwind CSS 4 in React: Utility Classes, Responsive Prefixes, Hover and Dark Mode
Next post
Design Tokens to a Tailwind Theme: Light and Dark Mode with @theme inline
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • Set it up in four commands
  • What init wrote: three small changes
  • Open them, edit them, commit them
  • button.tsx: a plain file
  • Change one variant, everywhere
  • Cards and buttons, put to work
  • A dialog that behaves
  • A library or your own copy?
  • Common mistakes
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.