Button

Runs an action, shows that it's working, and stays reachable when disabled.

Variants

Use primary for the one action a view is about, and quieter variants for the rest.

Preview
variants.tsx
import { Button } from "~/components/ui/button"

export default function Example() {
  return (
    <>
      <Button>Primary</Button>
      <Button variant="secondary">Secondary</Button>
      <Button variant="outline">Outline</Button>
      <Button variant="ghost">Ghost</Button>
      <Button variant="destructive">Delete</Button>
      <Button variant="link">Link</Button>
    </>
  )
}

Sizes

icon is square, for a button holding only an icon. Give it an aria-label.

Preview
sizes.tsx
import { Plus } from "lucide-react"

import { Button } from "~/components/ui/button"

export default function Example() {
  return (
    <>
      <Button size="sm">Small</Button>
      <Button size="md">Medium</Button>
      <Button size="lg">Large</Button>
      <Button size="icon" aria-label="Add">
        <Plus />
      </Button>
    </>
  )
}

Pending

Return a Promise from onClick and the button stays pending until it settles: it ignores further presses, keeps focus, and tells screen readers it’s busy after a second. Style the waiting state with the data-pending attribute, or Tailwind’s in-data-pending: variant inside the button.

Preview
pending.tsx
import { Button } from "~/components/ui/button"

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

export default function Example() {
  return (
    <Button onClick={() => wait(1500)} pendingAnnouncement="Saving">
      <span className="in-data-pending:hidden">Save changes</span>
      <span className="hidden in-data-pending:inline">Saving…</span>
    </Button>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byFile Drop Zoneform itemInputform itemNav SheetblockPaginationSidebarToastWorkspaceblock
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20tailwind-variants@4.1.0

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/button.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 tailwind-variants@4.1.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/button.tsx
import { Button as ButtonPrimitive } from "@ui-sdk/react/button"
import type * as React from "react"
import { tv, type VariantProps } from "tailwind-variants"

import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

const buttonVariants = tv({
  base: [
    textVariants({ weight: "medium" }),
    "inline-flex shrink-0 cursor-pointer items-center justify-center rounded-lg border border-transparent bg-clip-padding whitespace-nowrap select-none",
    "transition-[color,background-color,border-color,box-shadow,translate] duration-150",
    "outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/30",
    "active:not-aria-[haspopup]:translate-y-px",
    "aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20",
    "data-disabled:pointer-events-none data-disabled:opacity-50 data-pending:cursor-wait",
    "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  ],
  variants: {
    variant: {
      primary: "bg-primary text-primary-foreground hover:bg-primary/80",
      secondary:
        "bg-secondary text-secondary-foreground hover:brightness-95 aria-expanded:bg-secondary",
      outline:
        "border-border bg-background text-foreground hover:bg-muted aria-expanded:bg-muted dark:bg-transparent dark:hover:bg-input/30",
      ghost: "text-foreground hover:bg-muted aria-expanded:bg-muted dark:hover:bg-muted/50",
      destructive:
        "bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30",
      link: "text-primary underline-offset-4 hover:underline",
    },
    size: {
      xs: [textVariants({ size: "xs" }), "h-6 gap-2xs px-md [&_svg:not([class*='size-'])]:size-3"],
      sm: [textVariants({ size: "sm" }), "h-7 gap-2xs px-md"],
      md: [textVariants({ size: "sm" }), "h-8 gap-xs px-md"],
      lg: [textVariants({ size: "sm" }), "h-9 gap-xs px-lg"],
      "icon-xs": "size-6 [&_svg:not([class*='size-'])]:size-3",
      "icon-sm": "size-7",
      icon: "size-8",
      "icon-lg": "size-9",
    },
  },
  defaultVariants: {
    variant: "primary",
    size: "md",
  },
})

type ButtonVariants = VariantProps<typeof buttonVariants>

type ButtonProps = React.ComponentProps<typeof ButtonPrimitive> & {
  /** How much the button stands out. `destructive` is a tint, for actions that can't be undone. */
  variant?: NonNullable<ButtonVariants["variant"]>
  /** Height and padding. The `icon` sizes are square, for a button holding only an icon. */
  size?: NonNullable<ButtonVariants["size"]>
}

/** Runs an action. Return a Promise from `onClick` to keep it pending until the action finishes. */
function Button({ variant, size, className, ...props }: ButtonProps) {
  return (
    <ButtonPrimitive className={cnState(buttonVariants({ variant, size }), className)} {...props} />
  )
}

export { Button, buttonVariants }
export type { ButtonProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Button

Runs an action. Return a Promise from onClick to keep it pending until the action finishes.

sizeours

Height and padding. The icon sizes are square, for a button holding only an icon.

Type NonNullable<"xs" | "sm" | "md" | "lg" | "icon-xs" | "icon-sm" | "icon" | "icon-lg">

variantours

How much the button stands out. destructive is a tint, for actions that can't be undone.

Type NonNullable<"primary" | "destructive" | "secondary" | "outline" | "ghost" | "link">

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: ButtonState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: ButtonState) => string)

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

focusableWhenDisabled

Keeps a disabled button in the tab order with aria-disabled instead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing.

Type boolean · Default false

onClick

Runs when the button is pressed. Return a Promise to make the button pending until it settles: it gets aria-busy, aria-disabled and data-pending, keeps focus, ignores presses meanwhile, so a form can't submit twice, and says pendingAnnouncement after a second. The event's element is whatever render renders, like a link. details.source says how it was pressed, for changes made after an await.

Type (event: MouseEvent<HTMLElement, MouseEvent>, details: ActionDetails) => unknown

pendingAnnouncement

What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Type string · Default "Loading"

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<ButtonState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: ButtonState) => CSSProperties)

type

What a native button does in a form: "submit" submits it and "reset" resets it. Ignored when the button renders as something else.

Type "button" | "submit" | "reset" · Default "button"