Skip to content

Button

Starts an action, such as saving a form, sending an order or opening a dialog.

Installation

pnpm dlx shadcn@latest add @silkware/button

To copy the component by hand, install its dependencies:

pnpm add react-aria-components class-variance-authority cn lucide-react

Then copy the source into components/ui/button.tsx:

components/ui/button.tsx
"use client"
import type * as React from "react"import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"import {  Button as ButtonPrimitive,  Link as LinkPrimitive,  type ButtonProps as ButtonPrimitiveProps,  type LinkProps as LinkPrimitiveProps,} from "react-aria-components"
const buttonVariants = cva(  "group/button inline-flex shrink-0 items-center justify-center rounded-md border border-transparent bg-clip-padding text-sm font-medium 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]:translate-y-px disabled:pointer-events-none disabled:opacity-50 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:bg-primary/80",        outline:          "border-border bg-background shadow-xs hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",        secondary:          "bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",        ghost:          "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground 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 dark:focus-visible:ring-destructive/40",        link: "text-primary underline-offset-4 hover:underline",      },      size: {        default:          "h-9 gap-1.5 px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",        xs: "h-6 gap-1 rounded-[min(var(--radius-md),8px)] px-2 text-xs in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",        sm: "h-8 gap-1 rounded-[min(var(--radius-md),10px)] px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5",        lg: "h-10 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",        icon: "size-9",        "icon-xs":          "size-6 rounded-[min(var(--radius-md),8px)] in-data-[slot=button-group]:rounded-md [&_svg:not([class*='size-'])]:size-3",        "icon-sm":          "size-8 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-md",        "icon-lg": "size-10",      },    },    defaultVariants: {      variant: "default",      size: "default",    },  })
function Button({  className,  variant = "default",  size = "default",  ...props}: Omit<ButtonPrimitiveProps, "className"> &  React.RefAttributes<HTMLButtonElement> &  VariantProps<typeof buttonVariants> & {    className?: string  }) {  return (    <ButtonPrimitive      data-slot="button"      data-variant={variant}      data-size={size}      className={cn(buttonVariants({ variant, size, className }))}      {...props}    />  )}
function LinkButton({  className,  variant = "default",  size = "default",  ...props}: Omit<LinkPrimitiveProps, "className"> &  VariantProps<typeof buttonVariants> & {    className?: string  }) {  return (    <LinkPrimitive      data-slot="button"      data-variant={variant}      data-size={size}      className={cn(buttonVariants({ variant, size, className }))}      {...props}    />  )}
export { Button, LinkButton, buttonVariants }

Usage

import { Button } from "@/components/ui/button"
<Button onPress={() => approveOrder(order.id)}>Approve order</Button>

Use onPress, not onClick. React Aria fires onPress for mouse, touch, pen and keyboard alike, and cancels it when the pointer leaves the button before it is released.

Examples

Default

The primary action of a view. Use one per view.

Secondary

An action that sits beside the primary one, such as saving a draft.

Outline

An action that needs an edge to stand apart from what is around it, such as a toolbar on a busy surface.

Ghost

A quiet action, such as cancel, or an action repeated on every row of a list.

Destructive

An action that removes data or cannot be undone. Ask for confirmation with an Alert Dialog before you carry it out.

An action that reads as text inside a sentence or a dense row. It still performs an action; to go to another page, use a link button.

Size

xs, sm, default and lg set the height and the padding. Keep one size per group of buttons.

Icon

icon-xs, icon-sm, icon and icon-lg make a square button for a single icon. An icon button has no visible text, so it needs an aria-label.

With icon

Put data-icon="inline-start" or data-icon="inline-end" on the icon so the button tightens its padding on that side. Use an icon only when it makes the action quicker to recognise.

Loading

Set isPending while the action runs and show a Spinner before the label. A pending button keeps its focus, ignores further presses and tells screen readers that it is busy. Change the label to say what is happening.

Disabled

isDisabled removes the button from the tab order and ignores presses. Prefer to keep the button enabled and explain what is missing when it is pressed, because a disabled button does not say why it is disabled.

A control that goes to another page is a link, even when it looks like a button. Use LinkButton: it renders an <a> with the button styles and navigates on the client through the router.

Accessibility

KeyAction
SpacePresses the button.
EnterPresses the button.
TabMoves focus to the next control. A disabled button is skipped.

React Aria provides:

  • The button role, type="button" by default, and the native keyboard behaviour above.
  • Press events that behave the same for mouse, touch, pen and keyboard, without the delays and ghost clicks of the native click event on touch screens.
  • A focus ring for keyboard focus only, through focus-visible.
  • The native disabled attribute, which removes the button from the tab order, with isDisabled. With isPending the button gets aria-disabled="true", keeps focus and ignores presses.

You provide:

  • A visible label that names the action, or an aria-label when the button holds only an icon.
  • aria-describedby pointing to the explanation when the action needs one, for example why a submission is blocked.
  • The right element for the job: Button for an action, LinkButton for navigation.

Guidelines

When to use

  • To start an action on the current page: submit a form, open a dialog, run an approval.
  • To confirm or cancel a step in a flow.

When not to use

  • To go to another page. Use LinkButton, or a text link inside running text.
  • To switch something on and off. Use Toggle or Switch.
  • To offer more than three related actions. Group the rest in a Dropdown Menu.

Content

  • Start with a verb and name the object: "Approve order", "Export price list".
  • Write in sentence case, in a few words, with no full stop and no exclamation mark.
  • Say the same thing on the button and in the confirmation that follows: "Delete supplier" opens a dialog titled "Delete supplier?".

Hierarchy

One default button per view carries the main action. Secondary, outline and ghost buttons carry the rest, in that order of weight. Put the main action last in a row of buttons, where the eye ends.

Do

Give the view one main action and make the others quieter.

Don’t

Don’t put two default buttons side by side. Neither reads as the main action.

Do

Name the action and what it acts on.

Don’t

Don’t use labels such as "OK" or "Submit" that leave the outcome to guess.

API reference

Button

Renders a <button> through the React Aria Button. It accepts every React Aria Button prop and the props below.

PropTypeDefaultDescription
variant?"default" | "secondary" | "outline" | "ghost" | "destructive" | "link""default"The visual weight of the button.
size?"xs" | "sm" | "default" | "lg" | "icon-xs" | "icon-sm" | "icon" | "icon-lg""default"The height and padding. The icon sizes make a square button.
onPress?(event: PressEvent) => voidNoneCalled when the button is pressed with a pointer or the keyboard.
isDisabled?booleanfalseRemoves the button from the tab order and ignores presses.
isPending?booleanfalseMarks the action as running. The button keeps focus, ignores presses and is announced as busy.
type?"button" | "submit" | "reset""button"The behaviour of the button inside a form.
className?stringNoneClasses merged after the variant classes.

LinkButton

Renders an <a> through the React Aria Link with the button styles. It accepts every React Aria Link prop and the props below.

PropTypeDefaultDescription
href?stringNoneThe destination. Internal paths navigate on the client through the router.
variant?"default" | "secondary" | "outline" | "ghost" | "destructive" | "link""default"The visual weight of the link.
size?"xs" | "sm" | "default" | "lg" | "icon-xs" | "icon-sm" | "icon" | "icon-lg""default"The height and padding.
target?stringNoneWhere to open the destination, for example _blank to open a new tab.

buttonVariants

The class function behind both components, from class-variance-authority. Call it to give another element the button styles: buttonVariants({ variant: "outline", size: "sm" }).