Skip to content

Commit

Permalink
fix(cli): deduplicate classNames in applyColorMapping (#1089)
Browse files Browse the repository at this point in the history
* fix(cli): deduplicate classNames in applyColorMapping

* refactor: simplify applyColorMapping return

* chore: add changeset

---------

Co-authored-by: shadcn <[email protected]>
  • Loading branch information
santidalmasso and shadcn committed Sep 19, 2023
1 parent ccb2d69 commit ae84578
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-cooks-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-ui": patch
---

fix duplicate classnames
18 changes: 9 additions & 9 deletions packages/cli/src/utils/transformers/transform-css-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,38 +146,38 @@ export function applyColorMapping(

// Build color mappings.
const classNames = input.split(" ")
const lightMode: string[] = []
const darkMode: string[] = []
const lightMode = new Set<string>()
const darkMode = new Set<string>()
for (let className of classNames) {
const [variant, value, modifier] = splitClassName(className)
const prefix = PREFIXES.find((prefix) => value?.startsWith(prefix))
if (!prefix) {
if (!lightMode.includes(className)) {
lightMode.push(className)
if (!lightMode.has(className)) {
lightMode.add(className)
}
continue
}

const needle = value?.replace(prefix, "")
if (needle && needle in mapping.light) {
lightMode.push(
lightMode.add(
[variant, `${prefix}${mapping.light[needle]}`]
.filter(Boolean)
.join(":") + (modifier ? `/${modifier}` : "")
)

darkMode.push(
darkMode.add(
["dark", variant, `${prefix}${mapping.dark[needle]}`]
.filter(Boolean)
.join(":") + (modifier ? `/${modifier}` : "")
)
continue
}

if (!lightMode.includes(className)) {
lightMode.push(className)
if (!lightMode.has(className)) {
lightMode.add(className)
}
}

return lightMode.join(" ") + " " + darkMode.join(" ").trim()
return [...Array.from(lightMode), ...Array.from(darkMode)].join(" ").trim()
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ export function Foo() {
}\\"\\"
"
`;
exports[`transform css vars 4`] = `
"import * as React from \\"react\\"
export function Foo() {
return <div className={cn(\\"bg-white border border-stone-200 dark:bg-stone-950 dark:border-stone-800\\")}>foo</div>
}\\"\\"
"
`;
2 changes: 1 addition & 1 deletion packages/cli/test/utils/apply-color-mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("apply color mapping", async () => {
input:
"text-destructive border-destructive/50 dark:border-destructive [&>svg]:text-destructive text-destructive",
output:
"text-red-500 border-red-500/50 dark:border-red-500 [&>svg]:text-red-500 text-red-500 dark:text-red-900 dark:border-red-900/50 dark:dark:border-red-900 dark:[&>svg]:text-red-900 dark:text-red-900",
"text-red-500 border-red-500/50 dark:border-red-500 [&>svg]:text-red-500 dark:text-red-900 dark:border-red-900/50 dark:dark:border-red-900 dark:[&>svg]:text-red-900",
},
{
input:
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/test/utils/transform-css-vars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ export function Foo() {
raw: `import * as React from "react"
export function Foo() {
return <div className={cn("bg-background hover:bg-muted", true && "text-primary-foreground sm:focus:text-accent-foreground")}>foo</div>
}"
`,
config: {
tsx: true,
tailwind: {
baseColor: "stone",
cssVariables: false,
},
aliases: {
components: "@/components",
utils: "@/lib/utils",
},
},
baseColor: stone,
})
).toMatchSnapshot()

expect(
await transform({
filename: "test.ts",
raw: `import * as React from "react"
export function Foo() {
return <div className={cn("bg-background border border-input")}>foo</div>
}"
`,
config: {
Expand Down

0 comments on commit ae84578

Please sign in to comment.