Sub topic 11
Customizing components you own
This chapter assumes you can read the Button from the previous chapters: cva variants, the cn helper, and asChild. You should also know basic Tailwind and TypeScript.
Why editing is allowed
npx shadcn@latest add button copied components/ui/button.tsx into your repository. It is your file, so you change it directly. You don't need wrapper components or overrides to bend a package to your needs.
Not every change belongs in the same place, though. Pick the lowest-effort option that fits.
Level 1: className at the call site
Button passes className through cn, so the caller's classes come last and win conflicts.
<Button className="w-full">Save</Button>If w-full conflicted with a width in the base classes, tailwind-merge would drop the earlier one. This works only because cn(buttonVariants(...), className) keeps className last. If you reorder those arguments, call-site overrides stop working.
Level 2: add a variant
When a look repeats, name it. Edit the cva call in button.tsx:
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
outline: "border border-input bg-background hover:bg-accent",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 px-3",
lg: "h-11 px-8",
},
},ButtonProps extends VariantProps<typeof buttonVariants>, so TypeScript now accepts variant="destructive" and size="lg" with no further changes. A typo such as variant="danger" becomes a compile error.
Use semantic color names like bg-destructive, not raw values like bg-red-600. They read from the CSS variables you set up for theming, so dark mode and rebranding keep working. Define --destructive and --destructive-foreground in your stylesheet and map them in your Tailwind config if they aren't there yet.
Level 3: change structure or behavior
Sometimes the markup needs to change. For example, you might add a loading spinner to Button:
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean
loading?: boolean
}Then destructure loading in the component. Set disabled={loading || props.disabled} and aria-busy={loading} on the element. Edit the file and keep {...props} and ref forwarding in place.
Common errors
- Overrides ignored. The merge order in
cnwas changed, or the class is applied to a parent instead of the element. - Variant has no effect. The variant key is missing from
cva, or the class uses a color token that isn't defined in your theme. - Dynamic class names disappear. Tailwind scans source text. `
bg-${color}-500` is never generated, so write complete class strings in your variants. - Silent drift. You edited a component and left no trace. Add a short comment or commit message such as
button: added destructive variant. This helps when you later compare against upstream.
Exercise
You add variant="success" usage to <Button>, but TypeScript reports an error and no styles appear. What do you change in button.tsx, and why does that fix both problems?
Show answer
Add a success entry under variants.variant in the cva call, such as success: "bg-success text-success-foreground hover:bg-success/90", and define the matching CSS variables. ButtonProps takes its variant types from VariantProps<typeof buttonVariants>, so the new key makes the prop type-check. The cva entry also supplies the classes that were missing.