<page url="https://peakui.dev/en/docs/react/components/button">
# Button

> A pressable button with seven visual variants, three sizes, and disabled and pending states.

**Category**: components
**URL**: https://peakui.dev/en/docs/react/components/button
**Source**: https://raw.githubusercontent.com/peakui-dev/peakui/refs/heads/main/apps/docs/content/docs/en/react/components/(buttons)/button.mdx

## Usage

```tsx
import {Button} from "@peakui/react";
```

```tsx
"use client";

import {Button} from "@peakui/react";

export function Basic() {
  return <Button onPress={() => console.log("Button pressed")}>Click me</Button>;
}
```

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.

```tsx
<Button onPress={(event) => console.log(event.pointerType)}>Save</Button>
```

A button that is pending or disabled does not fire `onPress`.

## Examples

### Variants

```tsx
import {Button} from "@peakui/react";

export function Variants() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-3">
      <Button variant="primary">Primary</Button>
      <Button variant="secondary">Secondary</Button>
      <Button variant="tertiary">Tertiary</Button>
      <Button variant="outline">Outline</Button>
      <Button variant="ghost">Ghost</Button>
      <Button variant="danger">Danger</Button>
      <Button variant="danger-soft">Danger Soft</Button>
    </div>
  );
}
```

### Sizes

```tsx
import {Button} from "@peakui/react";

export function Sizes() {
  return (
    <div className="flex items-center gap-3">
      <Button size="sm">Small</Button>
      <Button size="md">Medium</Button>
      <Button size="lg">Large</Button>
    </div>
  );
}
```

### With Icons

Place an icon before or after the label. The button sizes any `svg` child and sets the gap.

```tsx
import {Envelope, Plus, TrashBin} from "@gravity-ui/icons";
import {Button} from "@peakui/react";

export function WithIcons() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-3">
      <Button>
        <Plus />
        Add member
      </Button>
      <Button variant="secondary">
        <Envelope />
        Send invite
      </Button>
      <Button variant="danger-soft">
        Delete
        <TrashBin />
      </Button>
    </div>
  );
}
```

### Icon Only

An icon-only button has no visible text, so give it an `aria-label`.

```tsx
import {Ellipsis, Gear, TrashBin} from "@gravity-ui/icons";
import {Button} from "@peakui/react";

export function IconOnly() {
  return (
    <div className="flex items-center gap-3">
      <Button isIconOnly aria-label="More options" variant="tertiary">
        <Ellipsis />
      </Button>
      <Button isIconOnly aria-label="Settings" variant="secondary">
        <Gear />
      </Button>
      <Button isIconOnly aria-label="Delete" variant="danger">
        <TrashBin />
      </Button>
    </div>
  );
}
```

### Loading

`isPending` keeps the button focusable but stops it responding to presses. It does not add a spinner: put a [`Spinner`](https://peakui.dev/docs/react/components/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.

```tsx
import {Button, Spinner} from "@peakui/react";

export function Loading() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-3">
      <Button isPending>
        <Spinner aria-hidden color="current" size="sm" />
        Saving
      </Button>
      <Button isPending variant="tertiary">
        <Spinner aria-hidden color="current" size="sm" />
        Loading
      </Button>
    </div>
  );
}
```

### Loading State

Children can be a function that receives the button's state.

```tsx
"use client";

import {Button, Spinner} from "@peakui/react";
import {useState} from "react";

export function LoadingState() {
  const [isPending, setIsPending] = useState(false);

  const handlePress = () => {
    setIsPending(true);
    setTimeout(() => setIsPending(false), 2000);
  };

  return (
    <Button isPending={isPending} onPress={handlePress}>
      {({isPending}) => (
        <>
          {isPending ? <Spinner aria-hidden color="current" size="sm" /> : null}
          {isPending ? "Saving" : "Save changes"}
        </>
      )}
    </Button>
  );
}
```

### 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.

```tsx
"use client";

import {Button} from "@peakui/react";

export function PressedState() {
  return (
    <Button variant="secondary">
      {({isPressed}) => (isPressed ? "Release to confirm" : "Press and hold")}
    </Button>
  );
}
```

### Full Width

```tsx
import {Button} from "@peakui/react";

export function FullWidth() {
  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <Button fullWidth>Save</Button>
      <Button fullWidth variant="tertiary">
        Cancel
      </Button>
    </div>
  );
}
```

### Disabled State

```tsx
import {Button} from "@peakui/react";

export function Disabled() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-3">
      <Button isDisabled variant="primary">
        Primary
      </Button>
      <Button isDisabled variant="secondary">
        Secondary
      </Button>
      <Button isDisabled variant="tertiary">
        Tertiary
      </Button>
      <Button isDisabled variant="outline">
        Outline
      </Button>
      <Button isDisabled variant="ghost">
        Ghost
      </Button>
      <Button isDisabled variant="danger">
        Danger
      </Button>
    </div>
  );
}
```

### 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>`.

```tsx
import {buttonVariants} from "@peakui/react";

<a className={buttonVariants({size: "md", variant: "primary"})} href="/dashboard">
  Open dashboard
</a>
```

The [Styling](https://peakui.dev/docs/react/getting-started/styling#variant-functions) guide covers variant functions and how to extend them, and [Composition](https://peakui.dev/docs/react/getting-started/composition#styling-another-element) 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](https://peakui.dev/docs/react/getting-started/styling#adding-variants).

```tsx
import type {ButtonProps, VariantProps} from "@peakui/react";

import {Button, tv} from "@peakui/react";

// `tv` is imported from @peakui/react so that it is the instance the library's variants use.
// The wrapper adds only its own variants: `Button` still owns `size` and `variant`, so each
// modifier reaches the element once. No `shadow-*` utility here: the focus ring is a box-shadow,
// and a shadow utility replaces it.
const myButtonVariants = tv({
  base: "font-semibold tracking-wide",
  defaultVariants: {
    radius: "full",
  },
  variants: {
    radius: {
      full: "rounded-full",
      sm: "rounded-sm",
    },
  },
});

type MyButtonVariants = VariantProps<typeof myButtonVariants>;

interface MyButtonProps extends Omit<ButtonProps, "className">, MyButtonVariants {
  className?: string;
}

function MyButton({className, radius, ...props}: MyButtonProps) {
  return <Button className={myButtonVariants({className, radius})} {...props} />;
}

export function CustomVariants() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-3">
      <MyButton>Rounded</MyButton>
      <MyButton radius="sm" variant="secondary">
        Squared
      </MyButton>
    </div>
  );
}
```

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.

```tsx
<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.

```css
@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](https://getbem.com/) methodology, so variants and states are plain class names you can target.

### CSS Classes

The Button component uses these CSS classes ([view source styles](https://github.com/peakui-dev/peakui/blob/main/packages/styles/components/button.css)):

#### 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**: `:hover` or `[data-hovered]`
- **Pressed**: `:active` or `[data-pressed]`
- **Focus**: `:focus-visible` or `[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](https://react-aria.adobe.com/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             |
</page>