Button set

A Button with six variants (primary, secondary, outline, ghost, destructive, link) and four sizes including a square icon size. The loading state shows a spinner without changing the button's width, keeps focus on the button and sets aria-busy. Exports buttonVariants so links can share the same styles.

ButtonsComponentTailwind v4

Source

"use client";

import { forwardRef, type ButtonHTMLAttributes, type MouseEvent } from "react";
import { LoaderCircle } from "lucide-react";
import { cn } from "@/lib/utils";

export type ButtonVariant = "primary" | "secondary" | "outline" | "ghost" | "destructive" | "link";
export type ButtonSize = "sm" | "md" | "lg" | "icon";

const base =
  "relative inline-flex shrink-0 select-none items-center justify-center whitespace-nowrap font-medium " +
  "transition-[color,background-color,border-color,box-shadow,text-decoration-color,scale] duration-150 ease-out " +
  "outline-hidden focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 " +
  "aria-disabled:cursor-default [&_svg]:pointer-events-none [&_svg]:shrink-0";

const variants: Record<ButtonVariant, string> = {
  primary:
    "bg-primary text-primary-foreground shadow-xs shadow-black/10 hover:bg-primary/88 " +
    "inset-shadow-[0_1px_0_rgb(255_255_255/0.14)] dark:inset-shadow-none",
  secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/70 dark:hover:bg-secondary/80",
  outline:
    "border border-border bg-background text-foreground shadow-xs shadow-black/5 hover:bg-accent " +
    "hover:text-accent-foreground dark:border-input dark:bg-input/20 dark:hover:bg-input/40",
  ghost: "text-foreground hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/60",
  destructive:
    "bg-destructive text-white shadow-xs shadow-black/10 hover:bg-destructive/90 focus-visible:ring-destructive/30 " +
    "dark:bg-destructive/70 dark:hover:bg-destructive/80",
  link: "rounded-sm text-foreground underline decoration-foreground/30 underline-offset-4 hover:decoration-foreground",
};

const sizes: Record<ButtonSize, string> = {
  sm: "h-8 gap-1.5 rounded-md px-3 text-[13px] [&_svg]:size-3.5",
  md: "h-9 gap-2 rounded-md px-4 text-sm [&_svg]:size-4",
  lg: "h-10 gap-2 rounded-lg px-5 text-[15px] [&_svg]:size-[18px]",
  icon: "size-9 rounded-md [&_svg]:size-4",
};

// Links keep the type scale but drop the box: no height or padding.
const linkSizes: Record<ButtonSize, string> = {
  sm: "gap-1 text-[13px] [&_svg]:size-3.5",
  md: "gap-1.5 text-sm [&_svg]:size-4",
  lg: "gap-1.5 text-[15px] [&_svg]:size-[18px]",
  icon: "size-9 [&_svg]:size-4",
};

/** Class names for a button look, for links or other elements that should match a Button. */
export function buttonVariants({ variant = "primary", size = "md", className }: { variant?: ButtonVariant; size?: ButtonSize; className?: string } = {}) {
  return cn(base, variant === "link" ? linkSizes[size] : sizes[size], variants[variant], className);
}

export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
  variant?: ButtonVariant;
  size?: ButtonSize;
  /** Shows a spinner, keeps the button's width and ignores clicks. Focus stays on the button. */
  loading?: boolean;
  /** Read by screen readers while loading. */
  loadingLabel?: string;
}

export const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
  { variant = "primary", size = "md", loading = false, loadingLabel = "Loading", type = "button", className, children, onClick, ...props },
  ref,
) {
  function handleClick(event: MouseEvent<HTMLButtonElement>) {
    // aria-disabled instead of disabled while loading, so keyboard focus is not dropped mid-action.
    if (loading) {
      event.preventDefault();
      return;
    }
    onClick?.(event);
  }

  return (
    <button
      ref={ref}
      type={type}
      data-loading={loading || undefined}
      aria-busy={loading || undefined}
      aria-disabled={loading || undefined}
      onClick={handleClick}
      className={cn(buttonVariants({ variant, size }), !loading && variant !== "link" && "motion-safe:active:scale-[0.97]", className)}
      {...props}
    >
      {loading ? (
        <>
          <span className="inline-flex items-center gap-[inherit] opacity-0">{children}</span>
          <span className="absolute inset-0 flex items-center justify-center">
            <LoaderCircle aria-hidden="true" className="motion-safe:animate-spin" />
            <span className="sr-only">{loadingLabel}</span>
          </span>
        </>
      ) : (
        children
      )}
    </button>
  );
});