Sub topic 16
Keeping copied components up to date
This chapter assumes you have run npx shadcn@latest init, added a few components, and edited at least one of them. It also assumes you are comfortable with Git. It covers what updates itself, what doesn't, and how to pull in upstream changes without losing your edits.
What you own and what you depend on
A conventional library gives you one version number. You run npm update and every component changes. shadcn/ui splits the stack in two:
- The source files in
components/uiare copies. They live in your repository, so only you change them. - The packages those files import are normal npm dependencies. They update through your package manager.
| Piece | Where it lives | How it updates |
|---|---|---|
| `@radix-ui/react-*` packages | `package.json` | `npm update` or a dependency bot |
| `class-variance-authority`, `clsx`, `tailwind-merge` | `package.json` | `npm update` |
| Tailwind CSS | `package.json` and your CSS entry file | Package manager, plus its own migration guide for major versions |
| `components/ui/*.tsx` | Your repository | You, with the CLI as a source of new versions |
| `lib/utils.ts` (the `cn` helper) | Your repository | You |
| Theme variables in your global CSS | Your repository | You |
| `components.json` | Your repository | You |
The Radix row is the one people overlook. Behavior such as focus trapping, Escape handling, and ARIA attributes comes from Radix. Updating those packages gives you accessibility and bug fixes in your components without touching a copied file.
Why updates are a manual decision
There is no automatic update, and that is by design. The CLI cannot know which lines of button.tsx you changed, so a silent update could erase your work. You decide when an upstream change is worth taking.
Many upstream changes are not worth taking. A component that works and passes your accessibility checks does not need a rewrite because a newer version exists. Update when one of these applies:
- A bug fix or accessibility fix affects a component you use.
- A new Radix or React major version requires a change in the component source.
- Upstream added a variant or prop you want.
- You are adding a new component that depends on a newer shape of an existing one, for example a changed
cnimport path.
Check what the CLI would change
The CLI replaces files only when you ask. If a file already exists, add prompts before replacing it. The --overwrite flag skips that prompt.
npx shadcn@latest add button --overwriteSome CLI versions also offer flags to preview the result without writing files. Their names have changed between releases, so check what your installed version supports:
npx shadcn@latest add --helpWhatever your version offers, Git gives you the same safety. Use it as the main tool instead of relying on a CLI-specific diff command.
A safe update workflow with Git
Do the update on a branch, from a clean working tree, one component at a time.
git status --short
git switch -c update-shadcn-button
npx shadcn@latest add button --overwrite
git diff -- components/ui/button.tsxThe first command should print nothing. If it prints changed files, commit or stash them first so the diff you read contains only upstream changes.
The diff shows upstream's version replacing yours. Lines you added appear as removals and upstream lines appear as additions. Read it in three passes:
- Identify your own edits that were removed, such as a custom variant or changed spacing.
- Identify upstream changes you want, such as a new attribute or an updated import.
- Restore your edits and keep upstream's changes.
Git can restore your edits hunk by hunk. The command below asks about each changed chunk and lets you discard the ones that undo your work:
git restore -p components/ui/button.tsxAnswer y to throw away an upstream hunk and return to your committed version. Answer n to keep the upstream hunk. When you finish, run the type checker and your tests, then commit:
npx tsc --noEmit
git add components/ui/button.tsx
git commit -m "Update button from upstream, keep brand variant"Reduce future merge pain
Merges hurt most when your edits and upstream's edits touch the same lines. You can lower the odds with a few habits.
Prefer wrappers to edits
If you need a button that shows a loading state, don't change button.tsx. Compose it in a new file:
import * as React from "react"
import { Button } from "@/components/ui/button"
type SubmitButtonProps = React.ComponentProps<typeof Button> & {
pending?: boolean
}
export function SubmitButton({
pending = false,
children,
disabled,
...props
}: SubmitButtonProps) {
return (
<Button type="submit" disabled={disabled || pending} aria-busy={pending} {...props}>
{pending ? "Saving..." : children}
</Button>
)
}An upstream update to button.tsx can now overwrite the file cleanly. SubmitButton keeps working as long as the public props of Button stay compatible.
Put additions where the diff stays small
When you must edit a copied file, make the change easy to spot and easy to carry forward:
- Add a new variant as one extra line in the
cvavariants object instead of rewriting existing ones. - Pass extra classes through the
classNameprop at the call site, wherecnresolves conflicts, instead of changing the base class string. - Change colors through CSS variables in your global stylesheet. Then no component file changes at all.
- Add a short comment above each deliberate edit, such as
// local: brand variant, so you can find it during a merge.
Keep a record of local changes
For a small team, a comment is enough. For a larger codebase, add a file such as components/ui/CHANGES.md that lists each modified component and why. It takes a minute to write, and it saves you from reverse-engineering your own intent a year later.
Updating the dependencies around the files
Run the usual dependency update on its own schedule, and separate it from component updates so a failure has one cause.
npm outdated
npm update @radix-ui/react-dialog @radix-ui/react-slotAfter a Radix minor update, run your tests and try the dialogs, menus, and popovers by keyboard. Behavior changes there come from the package, not from your copy, so a regression after npm update points to the dependency. A regression after add --overwrite points to the copied file.
Major versions need more care. A new Tailwind major version can change how configuration and theme variables work. A new React major version can change patterns such as forwardRef, and upstream components may be rewritten to match. Read the release notes for each, upgrade the framework first, and then decide which components to re-fetch.
Common mistakes
- Overwriting every component at once. You then have to review a large diff and cannot tell which change caused a failure.
- Updating copied files but forgetting
components.json, import aliases, orlib/utils.ts. A new component may expect a helper or alias that your project does not have. - Treating a clean merge as a passing update. Git cannot tell whether a changed
aria-*attribute ordata-stateselector still matches your styles. Always run the app and use the keyboard. - Never updating at all. Radix fixes accessibility bugs regularly, and a copy that is two years old misses them unless you also update the packages.
Exercise
You customized components/ui/button.tsx by adding a brand variant. Upstream released a fix for the button. Describe the steps you take to take the fix and keep your variant, and say which command lets you discard only the upstream hunks that remove your variant.
Show answer
Start from a clean working tree and create a branch. Run npx shadcn@latest add button --overwrite to bring in the upstream file. Read git diff -- components/ui/button.tsx to see which of your lines were removed and which upstream lines are new. Run git restore -p components/ui/button.tsx and answer y for the hunks that remove your brand variant, which returns them to your committed version, and n for the upstream fix. Run npx tsc --noEmit and your tests, check the button by keyboard, then commit.
Exercise
After running npm update, a dialog no longer closes on Escape. You have not run any shadcn CLI command. Is the cause more likely in your copied dialog.tsx or in a package, and how do you confirm it?
Show answer
It is more likely in a package, because Escape handling comes from @radix-ui/react-dialog and you changed no copied file. Confirm by running git status to check that no component files changed, then check package-lock.json for the Radix version that changed. Reinstalling the previous Radix version and seeing the behavior return confirms it.