Button

Actions. One primary per view.

Motion: Scales to 0.97 on press. `loading` keeps colour and width, spinner overlays the label. Light haptic on press (rigid for destructive); `haptic={false}` silences it.

Installation

npx shadcn@latest add @craft/button

Source

components/ui/button.tsx
"use client"

import * as React from "react"
import { type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Slot } from "radix-ui"

import { buttonVariants } from "@/components/ui/button-variants"
import { Spinner } from "@/components/ui/spinner"
import { haptic as hapticFor, useHaptics, type HapticPreset } from "@/lib/haptics"

/**
 * `loading` keeps the button's colour, width and focus (it is not disabled):
 * the label stays in place invisibly and a spinner sits on top, so nothing
 * jumps. Clicks and form submits are ignored while loading.
 *
 * Presses fire a light haptic on phones (`rigid` for destructive); pass
 * `haptic={false}` to silence it or a preset name to change it.
 */
function Button({
  className,
  variant = "default",
  size = "default",
  asChild = false,
  loading = false,
  haptic,
  onClick,
  children,
  ...props
}: React.ComponentProps<"button"> &
  VariantProps<typeof buttonVariants> & {
    asChild?: boolean
    loading?: boolean
    /** Haptic on press (phones only). Defaults to `light`, `rigid` for destructive; `false` turns it off. */
    haptic?: HapticPreset | false
  }) {
  const Comp = asChild ? Slot.Root : "button"
  const busy = loading && !asChild
  const { trigger } = useHaptics()
  const preset = haptic ?? (variant === "destructive" ? hapticFor.destructive : hapticFor.press)

  return (
    <Comp
      data-slot="button"
      data-variant={variant}
      data-size={size}
      data-loading={busy || undefined}
      aria-busy={busy || undefined}
      className={cn(buttonVariants({ variant, size, className }))}
      {...props}
      onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
        if (busy) return e.preventDefault()
        if (preset && !props.disabled) trigger(preset)
        onClick?.(e)
      }}
    >
      {busy ? (
        <>
          <span className="invisible contents">{children}</span>
          <span className="absolute inset-0 grid place-items-center">
            <Spinner role="presentation" aria-hidden />
          </span>
        </>
      ) : (
        children
      )}
    </Comp>
  )
}

export { Button, buttonVariants }
components/ui/button-variants.ts
import { cva, type VariantProps } from "class-variance-authority"

/**
 * Button styles, kept out of button.tsx (a client component) so Server
 * Components can style links as buttons: `<Link className={buttonVariants({ variant: "outline" })} />`.
 */
export const buttonVariants = cva(
  "group/button relative inline-flex shrink-0 items-center justify-center rounded-full border border-transparent bg-clip-padding text-sm font-medium in-data-[slot=button-group]:rounded-lg whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:not-data-loading:scale-[0.97] duration-(--motion-base) ease-(--ease-out) disabled:cursor-not-allowed disabled:opacity-50 data-loading:cursor-progress aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  {
    variants: {
      variant: {
        default: "bg-primary text-primary-foreground hover:not-disabled:bg-primary/85",
        outline:
          "border-border bg-background hover:not-disabled:bg-muted hover:not-disabled:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:not-disabled:bg-input/50",
        secondary:
          "bg-secondary text-secondary-foreground hover:not-disabled:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
        ghost:
          "hover:not-disabled:bg-muted hover:not-disabled:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:not-disabled:bg-muted/50",
        destructive:
          "bg-destructive/10 text-destructive hover:not-disabled:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:not-disabled:bg-destructive/30 dark:focus-visible:ring-destructive/40",
        link: "text-primary underline-offset-4 hover:not-disabled:underline",
      },
      size: {
        default:
          "h-(--control-h) gap-1.5 px-4 has-data-[icon=inline-end]:pr-3 has-data-[icon=inline-start]:pl-3",
        xs: "h-6 gap-1 px-2.5 text-xs has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2 [&_svg:not([class*='size-'])]:size-3",
        sm: "h-8 gap-1 px-3 text-[0.8rem] has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2 [&_svg:not([class*='size-'])]:size-3.5",
        lg: "h-10 gap-2 px-5 has-data-[icon=inline-end]:pr-4 has-data-[icon=inline-start]:pl-4",
        icon: "size-(--control-h)",
        "icon-xs":
          "size-6 [&_svg:not([class*='size-'])]:size-3",
        "icon-sm":
          "size-8",
        "icon-lg": "size-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
)

export type ButtonVariantProps = VariantProps<typeof buttonVariants>