Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active February 28, 2026 08:11
Show Gist options
  • Select an option

  • Save Kcko/99a847878e60ce9e2427ed225ea5584f to your computer and use it in GitHub Desktop.

Select an option

Save Kcko/99a847878e60ce9e2427ed225ea5584f to your computer and use it in GitHub Desktop.
// 1 install
npm install class-variance-authority clsx tailwind-merge
// 2 /lib/utils.ts
/*
clsx handles conditional logic and class composition
tailwind-merge removes conflicting Tailwind classes
*/
import { clsx } from "clsx"
import { twMerge } from "tailwind-merge"
cn("px-2 px-4") // => "px-4"
// 3 variant with cva
import { cva } from "class-variance-authority"
import { cn } from "@/lib/utils"
const buttonVariants = cva(
"rounded px-4 py-2 font-medium transition",
{
variants: {
variant: {
primary: "bg-blue-500 text-white",
secondary: "bg-gray-200 text-black",
},
size: {
sm: "text-sm",
lg: "text-lg",
},
},
defaultVariants: {
variant: "primary",
size: "sm",
},
}
)
export function Button({ variant, size, className }) {
return (
<button
className={cn(buttonVariants({ variant, size }), className)}
/>
)
}
export function cn(...inputs: any[]) {
return twMerge(clsx(inputs))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment