A utility function that combines the power of clsx
and tailwind-merge
for seamless management of Tailwind CSS class names.
- Merges Tailwind CSS class names with priority management.
- Supports conditional class names with the flexibility of
clsx
. - Simplifies dynamic class name handling in TypeScript and JavaScript projects.
Install the package via npm:
npm install @sglara/cn
import { cn } from "@sglara/cn";
const className = cn("bg-red-500", "text-white", "bg-blue-500");
console.log(className); // "text-white bg-blue-500"
const isActive = true;
const className = cn("bg-red-500", {
"text-white": isActive,
"text-gray-500": !isActive,
});
console.log(className); // "bg-red-500 text-white"
const className = cn("p-4", "p-2");
console.log(className); // "p-2"
const className = cn("bg-red-500", ["text-white", "rounded-lg"]);
console.log(className); // "bg-red-500 text-white rounded-lg"
The function fully supports TypeScript with type definitions for safe and predictable usage:
const className = cn("bg-red-500", { "text-white": true, hidden: false });
// TypeScript will catch errors for invalid inputs.
- Simplify Tailwind CSS Class Management: Avoid repetitive and conflicting class name issues.
- Typed Support: Works seamlessly with both JavaScript and TypeScript.
-
Lightweight: Combines two efficient libraries (
clsx
andtailwind-merge
) into one handy utility.
Contributions are welcome! Feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.