Button

A simple button component.

This one is exension of shadcn/ui button component. If you want to use the original one from shadcn/ui, you can visit this link here.

Buttons with different variants

Loading...

Usage guide

Install the clsx and tailwind-merge packages:

npm install @radix-ui/react-slot clsx tw-merge class-variance-authority

Next, add cn utility:

import { twMerge } from "tailwind-merge";
import { clsx, type ClassValue } from "clsx";

export function cn(...inputs: ClassValue[]) {
    return twMerge(clsx(inputs));
}

Copy and paste the following component source.

import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@/lib/utils";

const buttonVariants = cva(
    "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-500 disabled:pointer-events-none disabled:opacity-50",
    {
        variants: {
            variant: {
                default:
                    "bg-gray-800 text-white shadow hover:bg-gray-700 dark:bg-gray-200 dark:text-gray-900 dark:hover:bg-gray-300",
                danger: "bg-red-600 text-white shadow-sm hover:bg-red-500 dark:bg-red-400 dark:text-white dark:hover:bg-red-300",
                outline:
                    "border border-gray-300 bg-white shadow-sm hover:bg-gray-100 hover:text-gray-900 dark:border-gray-700 dark:bg-gray-900 dark:hover:bg-gray-800 dark:text-gray-100",
                secondary:
                    "bg-gray-200 text-gray-800 shadow-sm hover:bg-gray-300 dark:bg-gray-700 dark:text-gray-100 dark:hover:bg-gray-600",
                ghost: "hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-100",
                link: "text-gray-800 underline-offset-4 hover:underline dark:text-gray-100",
                glow: "bg-gray-800 text-white shadow-md shadow-gray-400 hover:shadow-lg hover:shadow-gray-500 dark:bg-gray-200 dark:text-gray-900 dark:shadow-gray-700 dark:hover:shadow-gray-800"
            },
            size: {
                default: "h-9 px-4 py-2",
                sm: "h-8 rounded-md px-3 text-xs",
                lg: "h-10 rounded-md px-8",
                icon: "h-9 w-9"
            }
        },
        defaultVariants: {
            variant: "default",
            size: "default"
        }
    }
);

export interface ButtonProps
    extends React.ButtonHTMLAttributes<HTMLButtonElement>,
        VariantProps<typeof buttonVariants> {
    asChild?: boolean;
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
    ({ className, variant, size, asChild = false, ...props }, ref) => {
        const Comp = asChild ? Slot : "button";
        return (
            <Comp
                className={cn(buttonVariants({ variant, size, className }))}
                ref={ref}
                {...props}
            />
        );
    }
);
Button.displayName = "Button";

export { Button, buttonVariants };

Props

PropTypeDescriptionDefault
childrenReactNodeThe content to be rendered inside it_
variantsstringdefault, danger, outline, secondary, link, gradient, glow, glassdefault
sizestringdefault, sm, lg, icondefault
asChild?stringIf false, returns button other wise returns childrenfalse
className?stringOptional class names to style the button component_