-
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tailwind): improve support for Tailwind CSS v4
- Loading branch information
1 parent
0bf4df7
commit 68444e0
Showing
16 changed files
with
217 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { applyPrefixV3 } from "./apply-prefix-v3"; | ||
|
||
describe("applyPrefix_v3", () => { | ||
it("should return empty string when classNames is empty", () => { | ||
expect(applyPrefixV3("", "tw-")).toBe(""); | ||
}); | ||
|
||
it("should return original classNames when prefix is empty", () => { | ||
expect(applyPrefixV3("text-lg", "")).toBe("text-lg"); | ||
}); | ||
|
||
it("should add prefix to single class", () => { | ||
expect(applyPrefixV3("text-lg", "tw-")).toBe("tw-text-lg"); | ||
}); | ||
|
||
it("should add prefix to multiple classes", () => { | ||
expect(applyPrefixV3("text-lg font-bold", "tw-")).toBe("tw-text-lg tw-font-bold"); | ||
}); | ||
|
||
it("should handle classes with modifiers", () => { | ||
expect(applyPrefixV3("dark:text-xl hover:text-lg", "tw-")).toBe("dark:tw-text-xl hover:tw-text-lg"); | ||
}); | ||
|
||
it("should preserve ! modifier", () => { | ||
expect(applyPrefixV3("!text-lg", "tw-")).toBe("!tw-text-lg"); | ||
}); | ||
|
||
it("should preserve - modifier", () => { | ||
expect(applyPrefixV3("-mt-2", "tw-")).toBe("-tw-mt-2"); | ||
}); | ||
|
||
it("should preserve ! and - modifiers", () => { | ||
expect(applyPrefixV3("!-mt-2", "tw-")).toBe("!-tw-mt-2"); | ||
}); | ||
|
||
it("should handle arbitrary values", () => { | ||
expect(applyPrefixV3("[&>*]:gap-4", "tw-")).toBe("[&>*]:tw-gap-4"); | ||
}); | ||
|
||
it("should handle custom separator", () => { | ||
expect(applyPrefixV3("dark__text-xl", "tw-", "__")).toBe("dark__tw-text-xl"); | ||
}); | ||
|
||
it("should not add prefix if class already has it", () => { | ||
expect(applyPrefixV3("tw-text-lg", "tw-")).toBe("tw-text-lg"); | ||
}); | ||
|
||
it("should handle extra whitespace", () => { | ||
expect(applyPrefixV3(" text-lg font-bold ", "tw-")).toBe("tw-text-lg tw-font-bold"); | ||
}); | ||
|
||
it("should handle extra whitespace in prefix", () => { | ||
expect(applyPrefixV3("text-lg", " tw- ")).toBe("tw-text-lg"); | ||
}); | ||
|
||
it("should handle multiple modifiers", () => { | ||
expect(applyPrefixV3("dark:hover:!text-lg", "tw-")).toBe("dark:hover:!tw-text-lg"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const cache = new Map<string, string>(); | ||
|
||
/** | ||
* Applies a prefix to class names while preserving modifiers and arbitrary values. | ||
* | ||
* @param classNames - A string containing one or more CSS class names separated by spaces | ||
* @param prefix - The prefix to be added to each class name | ||
* @param separator - The separator used between class name parts (default ":") | ||
* @returns A new string with the prefix applied to each class name | ||
*/ | ||
export function applyPrefixV3(classNames: string, prefix: string, separator = ":"): string { | ||
if (!classNames.trim().length || !prefix.trim().length) { | ||
return classNames; | ||
} | ||
|
||
classNames = classNames.trim(); | ||
prefix = prefix.trim(); | ||
separator = separator.trim(); | ||
|
||
const cacheKey = classNames; | ||
const cacheValue = cache.get(cacheKey); | ||
|
||
if (cacheValue) { | ||
return cacheValue; | ||
} | ||
|
||
const result = classNames | ||
.split(/\s+/) | ||
.map((className) => { | ||
className = className.trim(); | ||
|
||
if (!className.length) { | ||
return className; | ||
} | ||
|
||
if (className.startsWith("[") && className.endsWith("]")) { | ||
return className; | ||
} | ||
|
||
const parts = className.split(separator); | ||
const baseClass = parts.pop() ?? ""; | ||
|
||
let prefixedBaseClass = baseClass; | ||
|
||
let modifiers = ""; | ||
if (prefixedBaseClass[0] === "!") { | ||
modifiers = "!"; | ||
prefixedBaseClass = prefixedBaseClass.slice(1); | ||
} | ||
if (prefixedBaseClass[0] === "-") { | ||
modifiers += "-"; | ||
prefixedBaseClass = prefixedBaseClass.slice(1); | ||
} | ||
|
||
if (prefixedBaseClass.startsWith(prefix)) { | ||
return className; | ||
} | ||
|
||
prefixedBaseClass = modifiers + prefix + prefixedBaseClass; | ||
|
||
if (!parts.length) { | ||
return prefixedBaseClass; | ||
} | ||
|
||
return `${parts.join(separator)}${separator}${prefixedBaseClass}`; | ||
}) | ||
.join(" "); | ||
|
||
cache.set(cacheKey, result); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.