diff --git a/.changeset/tricky-cooks-appear.md b/.changeset/tricky-cooks-appear.md new file mode 100644 index 00000000000..5f79140f328 --- /dev/null +++ b/.changeset/tricky-cooks-appear.md @@ -0,0 +1,5 @@ +--- +"shadcn-ui": patch +--- + +fix duplicate classnames diff --git a/packages/cli/src/utils/transformers/transform-css-vars.ts b/packages/cli/src/utils/transformers/transform-css-vars.ts index d0ad624b147..b5f1259c102 100644 --- a/packages/cli/src/utils/transformers/transform-css-vars.ts +++ b/packages/cli/src/utils/transformers/transform-css-vars.ts @@ -146,27 +146,27 @@ export function applyColorMapping( // Build color mappings. const classNames = input.split(" ") - const lightMode: string[] = [] - const darkMode: string[] = [] + const lightMode = new Set() + const darkMode = new Set() 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}` : "") @@ -174,10 +174,10 @@ export function applyColorMapping( 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() } diff --git a/packages/cli/test/utils/__snapshots__/transform-css-vars.test.ts.snap b/packages/cli/test/utils/__snapshots__/transform-css-vars.test.ts.snap index 9f5962a2a73..baedb74d2d5 100644 --- a/packages/cli/test/utils/__snapshots__/transform-css-vars.test.ts.snap +++ b/packages/cli/test/utils/__snapshots__/transform-css-vars.test.ts.snap @@ -23,3 +23,11 @@ export function Foo() { }\\"\\" " `; + +exports[`transform css vars 4`] = ` +"import * as React from \\"react\\" +export function Foo() { + return
foo
+}\\"\\" + " +`; diff --git a/packages/cli/test/utils/apply-color-mapping.test.ts b/packages/cli/test/utils/apply-color-mapping.test.ts index 1b21150d57b..8da6765c772 100644 --- a/packages/cli/test/utils/apply-color-mapping.test.ts +++ b/packages/cli/test/utils/apply-color-mapping.test.ts @@ -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: diff --git a/packages/cli/test/utils/transform-css-vars.test.ts b/packages/cli/test/utils/transform-css-vars.test.ts index 46ad6916a58..586f1cee2bc 100644 --- a/packages/cli/test/utils/transform-css-vars.test.ts +++ b/packages/cli/test/utils/transform-css-vars.test.ts @@ -56,6 +56,29 @@ export function Foo() { raw: `import * as React from "react" export function Foo() { return
foo
+}" + `, + 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
foo
}" `, config: {