cn
Utilities for combining classNames with or without custom tailwind configurations to manage style conflicts.
Installation
Install the following dependencies:
npm install clsx tailwind-mergeCreate a utils/cn.ts file and paste the following code into it.
IntelliSense setup (optional)
If you are using VSCode and the TailwindCSS IntelliSense Extension, you have to add the following to your .vscode/settings.json file to enable Intellisense features for cn, cnExt and tv functions.
Prettier setup (optional)
If you're using prettier-plugin-tailwindcss, you can include cn, cnExt in the functions list to ensure it gets sorted as well.
Examples
cnExt
In this example, cnExt is used to merge Tailwind classes and conditionally apply styles based on the isActive prop while managing potential class conflicts.
cn
The cnExt function provides a more comprehensive approach compared to cn, handling class conflicts with an extensive configuration which may introduce performance overhead. 1 It is advisable to use the cn function when conflict resolution is not required for better performance.
Here is an example of using the cn function to achieve the same output as the usage above.
