Button
A pressable button with seven visual variants, three sizes, and disabled and pending states.
Usage
import {Button} from "@peakui/react";The examples start with "use client". The directive is needed only in a framework with React Server Components, such as Next.js, and only in a file that passes a function (like onPress) to a component or uses state: a server component cannot hand a function to Button. In a Vite or other client-only app it has no effect, and Vite's production build prints a warning that it was ignored, so leave it out there.
Handling Presses
onPress fires when the press is released over the button. It covers mouse, touch, pen and keyboard (Enter and Space) interactions alike: the event's pointerType says which one. Use onPress, not onClick. React Aria keeps onClick only as a compatibility alias and marks it not recommended, because its event is a mouse event and carries no details for other kinds of interaction.
<Button onPress={(event) => console.log(event.pointerType)}>Save</Button>A button that is pending or disabled does not fire onPress.
Examples
Variants
Sizes
With Icons
Place an icon before or after the label. The button sizes any svg child and sets the gap.
Icon Only
An icon-only button has no visible text, so give it an aria-label.
Loading
isPending keeps the button focusable but stops it responding to presses. It does not add a spinner: put a Spinner in the children, and see that page for its size, color and label props.
Keep the button's visible label in place and add the spinner beside it. The words "Saving" and "Loading" below are sample text: use the label the button already has, or a pending variant of it, never a spinner alone.
Loading State
Children can be a function that receives the button's state.
Pressed State
The same function receives isPressed, which is true from the moment the press starts until it ends, so a label can depend on it.
Full Width
Disabled State
Link Styled as a Button
To give a link the look of a button, apply buttonVariants to an <a>. Do not try to make Button render an <a>: its render function must still return a <button>.
import {buttonVariants} from "@peakui/react";
<a className={buttonVariants({size: "md", variant: "primary"})} href="/dashboard">
Open dashboard
</a>The Styling guide covers variant functions and how to extend them, and Composition shows the same recipe with a framework link component and with plain HTML classes.
Adding Custom Variants
Describe variants of your own with tv in a wrapper component. Button applies size and variant itself, so the wrapper adds only its own classes. To give an element other than Button the same look, extend buttonVariants instead, as described in Styling.
If the wrapper adds a rounded-* utility, the utility wins over the component's --button-radius variable. .button reads the variable in the components layer (border-radius: var(--button-radius)), while Tailwind utilities live in the later utilities layer, and a later layer beats an earlier one whatever the selector. Setting the variable through a utility ([--button-radius:0.25rem]) works too, for the same reason. Prefer rounded-* in a wrapper: it is the shorter form and nothing else on the button depends on the radius. Set --button-radius in global CSS when every button should follow, as in the section below.
Customization
Tailwind CSS
Pass utilities through className. They are merged with the button's own classes.
<Button className="rounded-full px-6 font-semibold">Rounded</Button>Global CSS
Override the button's classes in the components layer. Prefer the component variables where one exists: hover and pressed styles derive from them.
@layer components {
.button--primary {
--button-bg: var(--color-success);
--button-bg-hover: var(--color-success-hover);
--button-fg: var(--color-success-foreground);
}
.button--icon-only {
@apply rounded-lg;
}
}Styling Reference
PeakUI follows the BEM methodology, so variants and states are plain class names you can target.
CSS Classes
The Button component uses these CSS classes (view source styles):
Base & Size Classes
.button- Base button styles.button--sm- Small size.button--md- Medium size.button--lg- Large size
Variant Classes
.button--primary.button--secondary.button--tertiary.button--outline.button--ghost.button--danger.button--danger-soft
danger is a solid fill: the danger color as the background with the danger-foreground text on it. danger-soft is a tint: the same red at low opacity (15% in the default theme) with the darker danger-soft-foreground text, so it sits quietly beside other controls. Use danger for the confirming action of a destructive step and danger-soft for a destructive option that should not dominate the page.
Modifier Classes
.button--icon-only- Square button for a single icon.button--full-width- Stretches to the container's width
Slots
[data-slot="button"]
CSS Variables
| Variable | Purpose |
|---|---|
--button-bg | Background color |
--button-bg-hover | Background color when hovered |
--button-bg-pressed | Background color when pressed |
--button-fg | Text and icon color |
--button-border | Border color |
--button-radius | Corner radius |
Interactive States
The button supports both CSS pseudo-classes and data attributes:
- Hover:
:hoveror[data-hovered] - Pressed:
:activeor[data-pressed] - Focus:
:focus-visibleor[data-focus-visible](shows the focus ring) - Disabled:
:disabled,[data-disabled]or[aria-disabled="true"] - Pending:
[data-pending="true"]
API Reference
Button
| Prop | Type | Default | Description |
|---|---|---|---|
variant | 'primary' | 'secondary' | 'tertiary' | 'outline' | 'ghost' | 'danger' | 'danger-soft' | 'primary' | Visual style |
size | 'sm' | 'md' | 'lg' | 'md' | Size of the button |
fullWidth | boolean | false | Whether the button takes the full width of its container |
isIconOnly | boolean | false | Whether the button contains only an icon |
isDisabled | boolean | false | Whether the button is disabled |
isPending | boolean | false | Whether the button is waiting on an action. Presses are ignored; the button stays focusable |
type | 'button' | 'submit' | 'reset' | 'button' | Behavior of the button inside a form |
onPress | (e: PressEvent) => void | - | Handler called when the button is pressed |
children | React.ReactNode | (values: ButtonRenderProps) => React.ReactNode | - | Button content, or a function of the button's state |
className | string | (values: ButtonRenderProps) => string | - | Additional classes, or a function of the button's state |
render | DOMRenderFunction<'button', ButtonRenderProps> | - | Replaces the rendered element. It must still return a <button> |
There is no color prop: the look is chosen with variant. Every other React Aria Button prop is accepted.
Render Props
When children or className is a function, it receives:
| Prop | Type | Description |
|---|---|---|
isPending | boolean | Whether the button is waiting on an action |
isPressed | boolean | Whether the button is currently pressed |
isHovered | boolean | Whether the button is hovered |
isFocused | boolean | Whether the button is focused |
isFocusVisible | boolean | Whether the button is focused by keyboard |
isDisabled | boolean | Whether the button is disabled |