Skip to content

Instantly share code, notes, and snippets.

@dsdeur
Created October 9, 2021 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsdeur/6a096eb12e1bea128c69e4bcf36fd763 to your computer and use it in GitHub Desktop.
Save dsdeur/6a096eb12e1bea128c69e4bcf36fd763 to your computer and use it in GitHub Desktop.
Util function to combine classNames conditionally
function join(
...classNames: ([boolean, string] | string | undefined | null)[]
) {
let result = "";
for (let x = 0; x < classNames.length; x++) {
const value = classNames[x];
if (!value) continue;
if (typeof value === "string") {
result += " " + value;
continue;
}
if (value[0]) result += " " + value[1];
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment