Skip to content

Instantly share code, notes, and snippets.

@mflorida
Created April 15, 2024 23:52
Show Gist options
  • Save mflorida/58c5169f964ee150193aeb1e3e145d08 to your computer and use it in GitHub Desktop.
Save mflorida/58c5169f964ee150193aeb1e3e145d08 to your computer and use it in GitHub Desktop.
Don't use clsx. Resolve className for component from a series of arguments as strings or arrays of strings.
function resolveClassName(className: string | string[], ...more: (string | string[])[]): string {
return Array.from(
new Set(
[''].concat(className, more.flat()).filter(Boolean).join(' ').split(/\s+/)
)
).reduce((classes, cur) => {
if ((cur = cur.trim())) {
classes.push(cur);
}
return classes;
}, [] as string[]).join(' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment