Created
September 9, 2023 23:37
-
-
Save Jonnotie/921800092e79bc9befb201dcc9be3e77 to your computer and use it in GitHub Desktop.
getClasses.tsx
This file contains hidden or 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
// Add a prefix to a string of classNames. | |
// Example: prefixClasses("text-red-500 bg-blue-500", "hover") => ["hover:text-red-500", "hover:bg-blue-500"] | |
export function prefixClasses(classes: string, prefix: string): string[] { | |
const classNames = classes.split(" "); | |
return classNames.map((className) => `${prefix}:${className}`); | |
} | |
// Add "dark:" to a string of classNames. | |
// Example: darkClasses("text-red-500 bg-blue-500") => ["dark:text-red-500", "dark:bg-blue-500"] | |
export function darkClasses(classes: string): string[] { | |
const classNames = classes.split(" "); | |
return classNames.map((className) => `dark:${className}`); | |
} | |
// Remove a prefix from a string of classNames. | |
// Example: cleanClasses("dark:text-red-500 dark:bg-blue-500", "dark") => ["text-red-500", "bg-blue-500"] | |
export function cleanClasses(classes: string, prefix: string): string[] { | |
const classNames = classes.split(" "); | |
return classNames.map((className) => className.replace(`${prefix}:`, "")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment