A utility function that combines the power of clsx
and tailwind-merge
for seamless management of Tailwind CSS class names.
clsx
.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.
clsx
and tailwind-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.