Skip to content

Commit

Permalink
fix(shadcn): do not override existing vars (#6721)
Browse files Browse the repository at this point in the history
* fix(shadcn): do not override existing vars

* test(shadcn): update snapshots

* chore: changeset
  • Loading branch information
shadcn authored Feb 21, 2025
1 parent 3b90317 commit a5122f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-needles-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": patch
---

do not overwrite user defined vars
10 changes: 7 additions & 3 deletions packages/shadcn/src/utils/updaters/update-css-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,13 @@ function updateCssVarsPluginV4(
(node): node is postcss.Declaration =>
node.type === "decl" && node.prop === prop
)
existingDecl
? existingDecl.replaceWith(newDecl)
: ruleNode?.append(newDecl)

// Do not override existing declarations.
// We do not want new components to override existing vars.
// Keep user defined vars.
if (!existingDecl) {
ruleNode?.append(newDecl)
}
})
})
},
Expand Down
8 changes: 4 additions & 4 deletions packages/shadcn/test/utils/updaters/update-css-vars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe("transformCssVarsV4", () => {
@custom-variant dark (&:is(.dark *));
:root {
--background: hsl(215 20.2% 65.1%);
--background: hsl(210 40% 98%);
--foreground: hsl(222.2 84% 4.9%);
--primary: hsl(215 20.2% 65.1%);
}
Expand Down Expand Up @@ -338,7 +338,7 @@ describe("transformCssVarsV4", () => {
@custom-variant dark (&:is(.dark *));
:root {
--background: hsl(215 20.2% 65.1%);
--background: hsl(210 40% 98%);
--foreground: hsl(222.2 84% 4.9%);
--primary: hsl(215 20.2% 65.1%);
}
Expand Down Expand Up @@ -403,7 +403,7 @@ describe("transformCssVarsV4", () => {
@custom-variant dark (&:is(.dark *));
:root {
--background: hsl(215 20.2% 65.1%);
--background: hsl(210 40% 98%);
--foreground: hsl(222.2 84% 4.9%);
--primary: hsl(215 20.2% 65.1%);
--foo: 0.5rem;
Expand Down Expand Up @@ -478,7 +478,7 @@ describe("transformCssVarsV4", () => {
@custom-variant dark (&:is(.dark *));
:root {
--background: hsl(215 20.2% 65.1%);
--background: hsl(210 40% 98%);
--foreground: hsl(222.2 84% 4.9%);
--primary: hsl(215 20.2% 65.1%);
}
Expand Down

0 comments on commit a5122f9

Please sign in to comment.