[bug]: apply --only font deletes --background and --foreground from :root in Next.js projects
Describe the bug
In a Next.js App Router project, shadcn apply <preset> --only font deletes --background and --foreground from the top-level :root rule in globals.css. --only font writes no theme variables, so nothing puts them back. After that, bg-background and text-foreground resolve to nothing: the body background is transparent and the text is the browser's default black. .dark is not touched.
A top-level @media (prefers-color-scheme: dark) { :root { … } } block that sets the same two variables is deleted entirely.
Cause (from reading main at 98a1fe6): apply runs through runInit, which calls addComponents with isNewProject: options.isNewProject || projectInfo?.framework.name === "next-app" (packages/shadcn/src/commands/init.ts:832-833). addComponents turns that into cleanupDefaultNextStyles (packages/shadcn/src/utils/add-components.ts:143), so cleanupDefaultNextStylesPlugin (packages/shadcn/src/utils/updaters/update-css-vars.ts:190) runs on every apply in a Next app. Its removeConflictVars (line 169) removes --background and --foreground from the first top-level :root whatever their values. It is meant for create-next-app's #ffffff / #171717 defaults; the comment at line 216 says "This is only going to run on create project". A full apply or --only theme writes the preset's values back afterwards, so only --only font shows the loss.
Possible fix: run the cleanup only when the project is actually new, or remove the declarations only when they still hold create-next-app's default values.
Affected component/components
CLI: shadcn apply --only font (globals.css)
How to reproduce
From an empty directory:
pnpm dlx [email protected] repro --ts --tailwind --eslint --app --no-src-dir \
--import-alias "@/*" --use-pnpm --no-react-compiler --yes
cd repro
pnpm dlx [email protected] init button # writes components.json with "style": "new-york"
git add -A && git commit -m "shadcn init"
pnpm dlx [email protected] apply vega --only font --yes
git diff app/globals.css
pnpm build && pnpm start
# in the browser console on http://localhost:3000:
# getComputedStyle(document.body).backgroundColorvega isolates this bug: it adds Inter({ variable: '--font-sans' }) to layout.tsx, so fonts keep working. nova shows the same deletion, but its font also breaks (#10391).
Expected: --only font changes only font-related lines. --background and --foreground stay in :root.
Actual: the globals.css diff from apply vega --only font (the layout.tsx Inter change is omitted):
@@ -7,7 +7,7 @@
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
- --font-sans: var(--font-geist-sans);
+ --font-sans: var(--font-sans);
--font-mono: var(--font-geist-mono);
--color-sidebar-ring: var(--sidebar-ring);
--color-sidebar-border: var(--sidebar-border);
@@ -45,12 +45,11 @@
--radius-2xl: calc(var(--radius) * 1.8);
--radius-3xl: calc(var(--radius) * 2.2);
--radius-4xl: calc(var(--radius) * 2.6);
+ --font-heading: var(--font-sans);
}
:root {
--radius: 0.625rem;
- --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);
@@ -123,4 +122,7 @@
body {
@apply bg-background text-foreground;
}
+ html {
+ @apply font-sans;
+ }
}
\ No newline at end of fileComputed styles (production build, headless Chrome 154):
before apply |
after apply vega --only font |
|
|---|---|---|
body background-color |
lab(100 0 0) |
rgba(0, 0, 0, 0) |
body color |
lab(2.75381 0 0) |
rgb(0, 0, 0) |
--background on <html> |
lab(100% 0 0) |
(empty, not defined) |
--foreground on <html> |
lab(2.75381% 0 0) |
(empty, not defined) |
--card on <html> (control) |
lab(100% 0 0) |
lab(100% 0 0) |
html font-family |
Geist, "Geist Fallback" |
Inter, "Inter Fallback" |
Other variations I tried, all with apply nova --only font:
- Position: moving the two declarations to the end of
:rootmakes no difference; they are still removed. - Custom values:
--background: oklch(0.98 0.02 90)is removed too. - Dark-mode media query: a top-level
@media (prefers-color-scheme: dark) { :root { --background: …; --foreground: … } }is deleted entirely. - Nested
:root: inside@layer base { :root { … } }, the declarations are kept. - Other commands:
apply vega --only themeremoves and then re-adds them, for no net change.shadcn add carddoes not touchglobals.css.
Codesandbox/StackBlitz link
No response. The steps above start from an empty directory.
Logs
$ pnpm dlx [email protected] apply vega --only font --yes
✔ Preflight checks.
✔ Verifying framework. Found Next.js.
✔ Validating Tailwind CSS. Found v4.
✔ Validating import alias.
✔ Writing components.json.
✔ Checking registry.
✔ Updating fonts.
✔ Updating app/globals.css
Preset applied successfully.System Info
shadcn 4.21.0
next 16.3.6 (create-next-app 16.3.6), react 19.2.8
tailwindcss 4.3.3, @tailwindcss/postcss 4.3.3
node 26.8.2, pnpm 12.4.1
macOS 27.0
HeadlessChrome 154 (Playwright)Before submitting
- I've made research efforts and searched the documentation
- I've searched for existing issues
Source: shadcn-ui/ui