import { Slot } from "@radix-ui/react-slot"; import { cva, type VariantProps } from "class-variance-authority"; import * as React from "react"; import { cn } from "../lib/utils"; const buttonVariants = cva( "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-lg font-medium text-sm transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0", { variants: { variant: { default: "bg-primary text-primary-foreground hover:bg-primary/90 active:scale-[0.97] duration-[var(--transition-fast)]", destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90 active:scale-[0.97] duration-[var(--transition-fast)]", outline: "border border-border bg-card hover:bg-muted active:scale-[0.97] duration-[var(--transition-fast)] text-foreground", secondary: "bg-muted border border-border text-foreground hover:bg-muted/80 active:scale-[0.97] duration-[var(--transition-fast)]", ghost: "hover:bg-muted hover:text-foreground duration-[var(--transition-fast)] text-muted-foreground border border-transparent", link: "text-primary underline-offset-4 hover:underline", sandbox: "bg-[var(--btn-primary-bg)] text-[var(--btn-primary-text)] border border-[var(--border-accent)] hover:bg-[var(--btn-primary-hover)] active:scale-[0.97] duration-[var(--transition-fast)]", }, size: { default: "h-[var(--control-height)] px-4 py-2", sm: "h-8 rounded-md px-3 text-xs", lg: "h-11 rounded-lg px-7 text-sm", xl: "h-13 rounded-xl px-9 text-base", icon: "h-[var(--control-height)] w-[var(--control-height)]", }, }, defaultVariants: { variant: "default", size: "default", }, }, ); export interface ButtonProps extends React.ButtonHTMLAttributes { variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link" | "sandbox" | null; size?: "default" | "sm" | "lg" | "xl" | "icon" | null; asChild?: boolean; loading?: boolean; children?: React.ReactNode; } const Button = React.forwardRef( ( { className, variant, size, asChild = false, loading = false, disabled, children, ...props }, ref, ) => { const Comp = asChild ? Slot : "button"; // When using asChild, we can't add the loading spinner as it would create multiple children // which breaks Slot's single-child requirement if (asChild) { return ( {children} ); } return ( {loading && ( Loading spinner )} {children} ); }, ); Button.displayName = "Button"; export { Button, buttonVariants };