Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Button for UI SDK

A button for React on UI SDK with six variants, square icon sizes, a loading state that holds its width, and a way to render it as a link.

On this page9 sections

A button runs one action when it's pressed: save a form, open a dialog, delete a row. It renders a real <button> with its type set to button, so it never submits a form you didn't mean it to, and it comes in six variants and five sizes, each size with a square twin for an icon.

For a control that stays on or off, such as bold in an editor, use a Toggle. To join related actions into one bar, put them in a Button Group.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Button anatomy: root, icon, text

Usage

import { ArrowUpIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
<div className="flex items-center gap-2">
  <Button variant="outline">Button</Button>
  <Button aria-label="Submit" size="icon-md" variant="outline">
    <ArrowUpIcon aria-hidden="true" />
  </Button>
</div>

Accessibility

Button renders a native <button>, so the browser gives it the button role and a place in the tab order, and its text becomes its accessible name. An icon-only button has no text, so give it an aria-label, as in the Icon demo. While isLoading is on, the button sets aria-busy and keeps a visually hidden copy of its label, so a screen reader still hears what it does. A disabled button leaves the tab order unless you add focusableWhenDisabled, which marks it aria-disabled and keeps it focusable. Passed an a as render, it stays a link, with no button role, and is announced as one.

Enter
Presses the focused button.
Space
Presses the focused button when the key is released.

Demos

Size

Four pairs, from xs to lg, each an outline button with text beside its square icon-* twin holding an arrow. Pick the size from where the button sits: xs in a dense toolbar, sm in a table row or a card header, the default md in a form. A fifth pair, xl and icon-xl, is there for a landing page and isn't shown.

Preview width

Variant default

The filled button in the theme's primary color, with no variant prop at all, since default is what Button falls back to. Give it to the one action the screen exists for, such as Save or Pay. Drop the actions around it to outline or ghost, so the eye lands in one place.

Preview width

Outline

A bordered button on the page background, with a muted fill only on hover. Next to a filled Save it is the natural Cancel, and in a filter bar it reads as pressable without competing with the primary action. It keeps its border at rest, which is why it holds up better than ghost on a busy page.

Preview width

Secondary

A filled button in the theme's quieter secondary color. Use it for an action that is real but not the preferred one, like Save as draft next to Publish, when both deserve a filled shape. If the second action should recede further, outline drops the fill altogether.

Preview width

Ghost

No border and no background until the pointer is over it, so at rest the button is only its label. It suits repeated, low-stakes actions in a toolbar or a table row, where a dozen bordered buttons would turn into noise. Avoid it for the only action on a screen, since nothing marks it as pressable before someone hovers.

Preview width

Destructive

The button in the theme's destructive color, for delete, remove and revoke. Put it on the button that confirms the deletion, inside the dialog, and keep the button that opens that dialog neutral. One red control in a group is enough for the danger to stand out.

Preview width

Text in the primary color that underlines on hover and is still a <button>, so it runs an action and doesn't navigate. Use it for a minor action under a form or inside a sentence, such as Forgot password or Resend code. When the press should change the URL, render a real link instead, as the As child demo does.

Preview width

Icon

One outline button at icon-md with a circular arrow icon and no text. The icon is hidden from screen readers with aria-hidden, so aria-label, set to Submit, gives the button its name. Use an icon-only button for actions people know by their glyph, such as close, more or send, and keep a word on anything less obvious.

Preview width

With icon

New Branch has a branch icon before its label, marked data-icon="inline-start", and Fork has its icon after, marked data-icon="inline-end". The attribute trims the padding on the icon's side so the button looks balanced, and because that padding is logical it flips in a right-to-left layout. Leave it off and the icon side keeps the full text padding.

Preview width

Rounded

Get Started and an icon button, both with rounded-full added to their classes. The pill prop gives the same capsule without a class and also sets data-pill for your own styles. A capsule suits a call to action on a landing page or a round icon floating over content, while in app chrome a row of pills starts to look like tags.

Preview width

Spinner

Generating and Downloading are disabled while a Spinner turns beside the label, before it on one and after it on the other. Keep the words on screen like this when people need to know which of several jobs is running. The isLoading prop is the other choice: it hides the label behind a centered spinner and keeps the button's width, as the next demo shows.

Preview width

Loading

Save changes turns on isLoading for two seconds when pressed. The label stays in place but invisible, so the button keeps its width while a spinner turns over it, and the button is disabled with aria-busy set. Use it for a save or a submit that people wait on. The demo adds focusableWhenDisabled, so keyboard focus stays on the button while it loads.

Preview width

As child

Login is an a element passed as render, so it wears the small secondary button's classes and navigates like any link. UI SDK sees the href and leaves it a link, with no type or button role. Use this when pressing the control should change the URL, as with a sign-in or pricing page.

Preview width

RTL

Five buttons in a row whose dir is rtl, four of them with Arabic text: outline, destructive, one with an arrow marked data-icon="inline-end", an icon button labelled Add, and a disabled one with a spinner. The arrow also carries rtl:rotate-180, so it points the way the text reads. Set dir on a container like this for a small island of right-to-left UI.

Preview width

FAQ

Why doesn't my Button submit the form?

Button sets type to button unless you pass another value. A native button inside a form is a submit button by default, so without this every Cancel or Show password control would send the form. Set type to submit on the one button that should send it, and pressing Enter in a text field will submit through it as well.

While isLoading is on, the button is disabled, so a second press can't send the same form twice.

How do I show a loading spinner on a Button?

Set isLoading on Button, as the Loading demo does. The label stays in place but invisible, so the width doesn't jump, a Spinner is centered over it, and the button is disabled with aria-busy set. A hidden copy of the label keeps the name for screen readers.

To keep the words on screen, put a Spinner inside the button next to the text and disable the button yourself, as the Spinner demo does. A disabled native button can't hold focus, so add focusableWhenDisabled when focus should stay on the button while it loads. Returning a Promise from onClick also keeps the button pending until it settles, ignoring presses but drawing no spinner.

Rendered as a link, it shows the same spinner and can't be followed while it loads.

What Button variants and sizes are there, and can I add my own?

variant takes default, secondary, outline, ghost, destructive and link. size takes xs, sm, md, lg and xl for a button with text, and icon-xs through icon-xl for a square icon button. shadcn/ui's default and icon sizes still work as other names for md and icon-md, and pill rounds any of them fully.

Both lists live in buttonVariants, a tailwind-variants tv call in the Button file you installed. Add a key under variant or size with its classes, and Button accepts the new value with its type checked.

How do I add an icon to a Button?

Put the icon inside Button, before or after the text, and mark it with data-icon set to inline-start or inline-end. The button's gap spaces the icon from the words, and the attribute trims the padding on that side, as the With icon demo shows. That padding is logical, so it flips in a right-to-left layout.

With no text, use an icon-* size, which keeps the button square, and give it an aria-label, as in the Icon demo. An icon takes the button's icon size unless it carries a size-* class of its own.

Why doesn't a Tooltip open on a disabled Button?

A disabled Button ignores the pointer completely. Its classes set pointer-events: none whenever it is disabled, so hovering it never reaches a Tooltip trigger. Put the tooltip on a wrapper instead, such as a span around the button, and give that wrapper a tab index of 0 so keyboard users can reach the explanation too.

Disabling is usually better than hiding: a missing button looks like a bug, while a disabled one with a reason shows the action exists. Adding focusableWhenDisabled keeps the button itself in the tab order, which lets a tooltip open on focus, though hover is still blocked.

How do I handle a Button click in React?

Pass onClick, the same handler a plain button takes. It fires for a mouse click, a tap, and Enter or Space on the focused button, and a second argument says which of those it was. A disabled or loading button doesn't fire it.

For work that takes a while, turn on isLoading until the promise settles, so a second press can't start the same job again.

API Reference

Button

<button> data-slot="button"

PropType
clickEffect?boolean

Apply a click effect to the button.

Default
true
debug?boolean

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

disabled?boolean

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.

Default
false
focusableWhenDisabled?boolean

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.

Default
false
isLoading?boolean

Show a loading indicator.

Default
false
onClick?( event: React.MouseEvent<HTMLButtonElement>, details?: ButtonActionDetails ) → unknown

Runs when the button is pressed. Return a Promise to keep it pending (data-pending) until it settles. details.source says how it was pressed; a wrapper passing the click on may leave it out.

Type
( event: React.MouseEvent<HTMLButtonElement>, details?: ButtonActionDetails ) → unknown
Parameters

event – React.MouseEvent<HTMLButtonElement>

details? – ButtonActionDetails

Returns
unknown
pendingAnnouncement?string

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.

Default
"Loading"
pill?boolean

Fully rounded corners.

Default
false
size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
type?"button" | "reset" | "submit"

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" | "reset" | "submit"
Default
"button"
variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

The visual style.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
"default"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-pending?string

Present while the Promise its press handler returned is running.

data-pill?string

Present when pill is set.

data-size?string

Mirrors size.

data-state?string

"loading" when isLoading is true, otherwise "idle".

data-variant?string

Mirrors variant.