Skip to content

Instantly share code, notes, and snippets.

@sanusart
Last active January 27, 2025 09:21
Show Gist options
  • Save sanusart/a7865e6ab0ed53ca0e9715763e2071d1 to your computer and use it in GitHub Desktop.
Save sanusart/a7865e6ab0ed53ca0e9715763e2071d1 to your computer and use it in GitHub Desktop.
Custom cn() function #util #javascript #typescript #js
export function cn(...classes: (string | undefined | null | false)[]) {
return classes.filter(Boolean).join(' ');
}
@sanusart
Copy link
Author

This function meant to replace tailwind provided util:

import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

By using this function we can get rid of 2 dependencies - clsx and tailwind-merge, but we loose the abilities of tailwind-merge like silently merging duplicate classes (IMHO I prefere to do it myself for less magic and more visibility, but that's just me).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment