fix(cli): deduplicate classNames in applyColorMapping (#1089)

* fix(cli): deduplicate classNames in applyColorMapping

* refactor: simplify applyColorMapping return

* chore: add changeset

---------

Co-authored-by: shadcn <m@shadcn.com>
This commit is contained in:
Santi Dalmasso
2023-09-19 00:04:57 -03:00
committed by GitHub
parent ccb2d695a7
commit ae845788f6
5 changed files with 46 additions and 10 deletions

View File

@@ -146,27 +146,27 @@ 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}` : "")
@@ -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()
}