Sub topic 16
Keeping copied components up to date
This chapter assumes you have run npx shadcn@latest init, added components with the CLI, and edited at least one of them. It also assumes you use Git.
Why updates are manual
A normal library updates when you bump its version. shadcn/ui copies component source into your repository, so no package version tracks those files. Once you edit components/ui/button.tsx, it is your code. The upstream version can change, but nothing tells your copy to follow.
This trade-off is deliberate. You get full control, and in return you decide when and how to take upstream changes.
What does update through npm
Not everything is copied. Each component depends on packages that do update normally:
- Radix UI packages such as
@radix-ui/react-dialog, which hold the behavior and accessibility logic class-variance-authority,clsx, andtailwind-mergetailwindcssitself
Bug fixes in focus handling or keyboard behavior usually arrive through the Radix packages. Your copied files mostly hold markup, classes, and variants. So the files you must merge by hand are mostly the styling and structure layer.
A safe update workflow
Treat an upstream update as a code merge. Git gives you the diff, so you need no special tooling.
The commands look like this:
git status # must be clean first
npx shadcn@latest add button --overwrite # replaces components/ui/button.tsx
git diff components/ui/button.tsx # see upstream changes and lost editsWithout --overwrite, the CLI asks before replacing an existing file. The flag skips that question, so a clean working tree is your protection. Lines you customized appear as removals in the diff. Re-apply them with git checkout -p or by editing by hand. To discard everything, run git restore components/ui/button.tsx.
Habits that make updates cheap
- Keep customizations small and local. Change variants in the
cvacall rather than rewriting the JSX. A small diff is easy to re-apply. - Wrap instead of editing. If you need a
SubmitButtonwith a loading spinner, build it in a separate file that importsButton. The original file then stays identical to upstream. - Commit CLI-added files on their own. A commit like
chore: add dialog from shadcngives you a clean baseline to compare against later. - Mark intentional edits. A short comment such as
// local: added "destructive-outline" variantshows you what to re-apply. - Update on purpose. Do it when you need a fix or a new feature, not on a schedule. Components that work and pass tests do not need refreshing.
Common errors
- Running
add --overwritewith uncommitted changes, which makes your edits hard to recover. - Updating every component at once, which produces a diff too large to review.
- Forgetting that the theme variables in
globals.csscan change. Compare that file as well when a component expects a new token such as--sidebar.
Exercise
You added a destructive-outline variant directly inside components/ui/button.tsx. Upstream fixed a focus ring bug in the same file. Describe how to take the fix without losing your variant, and one change that makes this easier next time.
Show answer
Commit your work, run npx shadcn@latest add button --overwrite, and read git diff to see the upstream fix and the removed variant. Keep the fix and re-add your variant, or restore the file and copy over only the fix. To make this easier, keep the variant as a small addition inside the cva call with a comment marking it as local. Alternatively, build a wrapper component in another file so button.tsx stays identical to upstream.