Button
Lets people take an action, like submitting a form or starting a task.
Installation
Install the dependencies:
npm install @base-ui/react class-variance-authority clsx tailwind-mergeAdd the cn helper if you haven't already:
import { clsx, type ClassValue } from "clsx";
import { extendTailwindMerge } from "tailwind-merge";
const twMerge = extendTailwindMerge({
extend: {
theme: {
text: [
"display",
"heading-xl",
"heading-lg",
"heading-md",
"heading-sm",
"heading-xs",
"body-lg",
"body-md",
"body-sm",
"label",
"helper",
"button",
],
"font-weight": ["regular"],
},
},
});
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}Add the component:
import * as React from "react";
import { Button as BaseButton } from "@base-ui/react/button";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
const buttonVariants = cva(
[
"inline-flex h-12 shrink-0 items-center justify-center gap-2 rounded-sm px-4 text-button whitespace-nowrap select-none",
"focus-visible:focus-ring",
"[&_svg]:size-5 [&_svg]:shrink-0",
"data-disabled:cursor-not-allowed",
],
{
variants: {
kind: {
primary:
"bg-button-primary text-text-on-color hover:not-data-disabled:bg-button-primary-hover active:not-data-disabled:bg-button-primary-active data-disabled:bg-button-disabled data-disabled:text-text-disabled",
secondary:
"bg-button-secondary text-text-on-color hover:not-data-disabled:bg-button-secondary-hover data-disabled:bg-button-disabled data-disabled:text-text-disabled",
tertiary:
"border border-button-tertiary text-button-tertiary hover:not-data-disabled:bg-button-tertiary-hover-bg data-disabled:border-border-disabled data-disabled:text-text-disabled",
subtle:
"bg-button-subtle text-text-primary hover:not-data-disabled:bg-button-subtle-hover data-disabled:bg-button-disabled data-disabled:text-text-disabled",
danger:
"bg-button-danger text-text-on-color hover:not-data-disabled:bg-button-danger-hover data-disabled:bg-button-disabled data-disabled:text-text-disabled",
},
iconOnly: {
true: "w-12 px-0",
false: "",
},
},
defaultVariants: {
kind: "primary",
iconOnly: false,
},
},
);
type ButtonProps = Omit<React.ComponentProps<typeof BaseButton>, "className"> &
VariantProps<typeof buttonVariants> & {
className?: string;
};
function Button({ className, kind, iconOnly, ...props }: ButtonProps) {
return (
<BaseButton
data-slot="button"
className={cn(buttonVariants({ kind, iconOnly }), className)}
{...props}
/>
);
}
export { Button, buttonVariants, type ButtonProps };A one-line install with the shadcn CLI is coming with the Habi registry.
Usage
import { Button } from "@/components/ui/button";
<Button>Book an appointment</Button>;Examples
Kinds
Use one primary button per screen, for the main action. Use secondary and tertiary for other actions, subtle for low-emphasis ones, and danger only for actions that delete something or can't be undone.
With an icon
Put the icon before the label for actions, like Download or Add. Put it after the label for moving forward, like Continue.
Icon only
Use these only where the icon is widely understood, like search or delete. Every icon-only button needs an aria-label that says what it does.
Disabled
Avoid disabled buttons where you can. People can't tell why a button doesn't work. It's usually better to let them press it and explain what's missing.
As a link
If the button takes people to another page, use a link that looks like a button. Pass buttonVariants to your link's className. This works with next/link, your router's link component, or a plain <a> for external sites.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
kind | "primary" | "secondary" | "tertiary" | "subtle" | "danger" | "primary" | How much emphasis the button has. |
iconOnly | boolean | false | Makes a 48 × 48px square button for a single icon. Needs an aria-label. |
disabled | boolean | false | Stops people using the button. |
focusableWhenDisabled | boolean | false | Keeps a disabled button focusable, for example while it shows a loading state. |
type | "button" | "submit" | "reset" | "button" | Use "submit" for the button that sends a form. |
Button also accepts the other props of Base UI's Button and of the HTML <button> element, like onClick.
Accessibility
- Keyboard: Tab moves focus to the button, and Enter or Space presses it. A 2px focus ring shows which button has focus.
- Size: buttons are 48px tall, well above the 24 x 24px minimum target size in WCAG 2.2.
- Labels: start with a verb and say what will happen: "Pay ₱145.00" instead of "Submit".
- Forms: buttons default to
type="button", so settype="submit"on the button that sends a form. - Icons are hidden from screen readers, because the label already says what the button does. Icon-only buttons need an
aria-label. - Links: if it goes to another page, use a link styled with
buttonVariants, not a Button. Screen readers then announce it as a link.