Last active
February 28, 2026 08:11
-
-
Save Kcko/99a847878e60ce9e2427ed225ea5584f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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