Skip to content

Commit a5122f9

Browse files
authored
fix(shadcn): do not override existing vars (#6721)
* fix(shadcn): do not override existing vars * test(shadcn): update snapshots * chore: changeset
1 parent 3b90317 commit a5122f9

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

.changeset/ninety-needles-brake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"shadcn": patch
3+
---
4+
5+
do not overwrite user defined vars

packages/shadcn/src/utils/updaters/update-css-vars.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,13 @@ function updateCssVarsPluginV4(
392392
(node): node is postcss.Declaration =>
393393
node.type === "decl" && node.prop === prop
394394
)
395-
existingDecl
396-
? existingDecl.replaceWith(newDecl)
397-
: ruleNode?.append(newDecl)
395+
396+
// Do not override existing declarations.
397+
// We do not want new components to override existing vars.
398+
// Keep user defined vars.
399+
if (!existingDecl) {
400+
ruleNode?.append(newDecl)
401+
}
398402
})
399403
})
400404
},

packages/shadcn/test/utils/updaters/update-css-vars.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe("transformCssVarsV4", () => {
274274
275275
@custom-variant dark (&:is(.dark *));
276276
:root {
277-
--background: hsl(215 20.2% 65.1%);
277+
--background: hsl(210 40% 98%);
278278
--foreground: hsl(222.2 84% 4.9%);
279279
--primary: hsl(215 20.2% 65.1%);
280280
}
@@ -338,7 +338,7 @@ describe("transformCssVarsV4", () => {
338338
339339
@custom-variant dark (&:is(.dark *));
340340
:root {
341-
--background: hsl(215 20.2% 65.1%);
341+
--background: hsl(210 40% 98%);
342342
--foreground: hsl(222.2 84% 4.9%);
343343
--primary: hsl(215 20.2% 65.1%);
344344
}
@@ -403,7 +403,7 @@ describe("transformCssVarsV4", () => {
403403
404404
@custom-variant dark (&:is(.dark *));
405405
:root {
406-
--background: hsl(215 20.2% 65.1%);
406+
--background: hsl(210 40% 98%);
407407
--foreground: hsl(222.2 84% 4.9%);
408408
--primary: hsl(215 20.2% 65.1%);
409409
--foo: 0.5rem;
@@ -478,7 +478,7 @@ describe("transformCssVarsV4", () => {
478478
479479
@custom-variant dark (&:is(.dark *));
480480
:root {
481-
--background: hsl(215 20.2% 65.1%);
481+
--background: hsl(210 40% 98%);
482482
--foreground: hsl(222.2 84% 4.9%);
483483
--primary: hsl(215 20.2% 65.1%);
484484
}

0 commit comments

Comments
 (0)