Skip to content

Commit cdf58be

Browse files
imskyleenshadcn
andauthored
feat(shadcn): fix transformCssVars function (#8186)
* feat(shadcn): fix transformCssVars function * test(shadcn): update snapshots * chore: add changeset --------- Co-authored-by: shadcn <[email protected]>
1 parent fae1a81 commit cdf58be

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

.changeset/hip-pianos-fry.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+
fix transformCssVars function with prefix

packages/shadcn/src/utils/transformers/transform-css-vars.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ export const transformCssVars: Transformer = async ({
3131
// }
3232
// }
3333
sourceFile.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach((node) => {
34-
const value = node.getText()
35-
if (value) {
36-
const valueWithColorMapping = applyColorMapping(
37-
value.replace(/"/g, ""),
38-
baseColor.inlineColors
39-
)
40-
node.replaceWithText(`"${valueWithColorMapping.trim()}"`)
34+
const raw = node.getLiteralText()
35+
const mapped = applyColorMapping(raw, baseColor.inlineColors).trim()
36+
if (mapped !== raw) {
37+
node.setLiteralValue(mapped)
4138
}
4239
})
4340

packages/shadcn/test/utils/__snapshots__/transform-css-vars.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ exports[`transform css vars 2`] = `
1212
"import * as React from "react"
1313
export function Foo() {
1414
return <div className="bg-white hover:bg-stone-100 text-stone-50 sm:focus:text-stone-900 dark:bg-stone-950 dark:hover:bg-stone-800 dark:text-stone-900 dark:sm:focus:text-stone-50">foo</div>
15-
}""
15+
}"
1616
"
1717
`;
1818

1919
exports[`transform css vars 3`] = `
2020
"import * as React from "react"
2121
export function Foo() {
2222
return <div className={cn("bg-white hover:bg-stone-100 dark:bg-stone-950 dark:hover:bg-stone-800", true && "text-stone-50 sm:focus:text-stone-900 dark:text-stone-900 dark:sm:focus:text-stone-50")}>foo</div>
23-
}""
23+
}"
2424
"
2525
`;
2626

2727
exports[`transform css vars 4`] = `
2828
"import * as React from "react"
2929
export function Foo() {
3030
return <div className={cn("bg-white border border-stone-200 dark:bg-stone-950 dark:border-stone-800")}>foo</div>
31-
}""
31+
}"
3232
"
3333
`;

packages/shadcn/test/utils/__snapshots__/transform-tw-prefix.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function Foo() {
2727
exports[`transform tailwind prefix 4`] = `
2828
"import * as React from "react"
2929
export function Foo() {
30-
return <div className={cn("tw:bg-background hover:tw:bg-muted", true && "tw:text-primary-foreground sm:focus:tw:text-accent-foreground")}>foo</div>
30+
return <div className={cn("tw:bg-white hover:tw:bg-stone-100 dark:tw:bg-stone-950 dark:hover:tw:bg-stone-800", true && "tw:text-stone-50 sm:focus:tw:text-stone-900 dark:tw:text-stone-900 dark:sm:focus:tw:text-stone-50")}>foo</div>
3131
}
3232
"
3333
`;

0 commit comments

Comments
 (0)