# Button

Starts an action on this page or submits a form.

- Family: action
- Import: `import { Button } from "@senvenli/react/action";`
- Page: https://design.senvenli.com/docs/components/button
- JSON: https://design.senvenli.com/docs/components/button.json

## Example: Actions in a view

One primary action, with the quieter ones beside it; danger only confirms a destructive step.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      <Button variant="primary">Save changes</Button>
      <Button>Cancel</Button>
      <Button variant="subtle">Reset filters</Button>
      <Button variant="link">Show details</Button>
      <Button variant="primary" tone="danger">Delete order</Button>
    </Stack>
  );
}
```

## Examples

### With an icon

An icon before or after the label, or alone with an accessible name.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      <Button variant="primary" iconBefore="plus">
        New order
      </Button>
      <Button iconAfter="chevron-down">Export</Button>
      {/* Without a visible label, the name comes from `aria-label`. */}
      <Button iconOnly iconBefore="settings" aria-label="Settings" />
    </Stack>
  );
}
```

### Sizes

Compact for dense tables and toolbars, large for a page's main call to action.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      <Button size="small">Add filter</Button>
      <Button>Add filter</Button>
      <Button size="large">Add filter</Button>
    </Stack>
  );
}
```

### Disabled and pending

A pending button keeps its width and its name while the work runs.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      <Button isDisabled>Send invoice</Button>
      <Button variant="primary" isPending>
        Save changes
      </Button>
    </Stack>
  );
}
```

### As a link: A call to action that goes somewhere

It looks like a Button and is a real link: it works before JavaScript loads.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      <Button href="#" variant="primary" iconBefore="plus">
        Add product
      </Button>
      <Button href="#">View orders</Button>
    </Stack>
  );
}
```

### As a link: External

Opens in a new tab with the arrow after the label.

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

export default function Example() {
  return (
    <Button href="https://example.com/docs" isExternal>
      Read the documentation
    </Button>
  );
}
```

### As a link: Icon only, and disabled

An icon-only link needs a name; a disabled one has no `href` and cannot be focused.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      <Button href="#" variant="subtle" iconOnly iconBefore="settings" aria-label="Project settings" />
      <Button href="#" isDisabled>
        View invoice
      </Button>
    </Stack>
  );
}
```

### As a link: Handled in place

`onPress` cancels a plain click so the app can respond; a modified click still follows the `href`.

```tsx
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

export default function Example() {
  const [isOpen, setOpen] = useState(false);
  return (
    <Stack direction="horizontal" space="space-150">
      {/* A plain click opens the preview here; Ctrl or Cmd click still opens the link in a new tab. */}
      <Button href="https://example.com/invoices/1042" onPress={() => setOpen(true)}>
        Preview invoice
      </Button>
      {isOpen && <Text tone="subtle">Preview opened in place.</Text>}
    </Stack>
  );
}
```

### Radius

`radius`: none, small, medium (the design), large or full; the provider can set it for every component.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";

// `radius` on one component; `<DesignSystemProvider defaults={{ radius: "full" }}>` sets it for the whole app.
export default function Example() {
  return (
    <Stack direction="horizontal" space="space-150">
      <Button radius="none">None</Button>
      <Button radius="small">Small</Button>
      <Button>Medium</Button>
      <Button radius="large">Large</Button>
      <Button radius="full">Full</Button>
    </Stack>
  );
}
```

## Guidelines

### When to use it

- The press changes something on this page: save, send, open a dialog.
- A call to action that navigates: "Add product" to /products/new.

### Use something else when

- [link](https://design.senvenli.com/docs/components/link.md): The action sits inside running text.

### Do and avoid

- One primary button per view.
- Use danger only to confirm a destructive step.
- Label with a verb in sentence case: "Save changes".

## Accessibility

Keyboard:

- Enter, Space: Presses the button.
- Enter: Follows the link.

Built in:

- Pending and disabled states are exposed to assistive technology.
- A 44px hit area under coarse pointers without changing the layout.
- Keeps link semantics: role link and a real href that works without JavaScript.

You provide:

- An `aria-label` when the button shows only an icon.
- An `aria-label` when it shows only an icon.

## API reference

### Button

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| variant | `ButtonVariant` |  | no | `primary` is the one main action of a view; `subtle` for toolbars and rows; `link` for a quiet text action. |
| tone | `ButtonTone` |  | no | `danger` for a destructive action (delete, discard); it keeps the variant's shape. |
| fullWidth | `boolean` |  | no | As wide as its container (a form's submit on a phone, a stacked set of actions). |
| radius | `ComponentRadius` |  | no | Corner radius: `none`, `small`, `medium` (the design), `large` or `full` (a pill). The provider's default otherwise. |
| size | `ButtonSize` |  | no |  |
| iconBefore | `IconName` |  | no | Lucide icon name shown before the label ("plus"). |
| iconAfter | `IconName` |  | no | Lucide icon name shown after the label. |
| children | `React.ReactNode` |  | no |  |
| iconOnly | `false \| true` |  | no |  |
| href | `string` |  | no | Where it goes. Rendered as a real `href`, so the link works before (and without) JavaScript. |
| isExternal | `boolean` |  | no | Link's convention: opens in a new tab with `noopener noreferrer`, shows `arrow-up-right` after the label (unless `iconAfter` is set) and tells screen readers it opens a new tab. With `onPress` a plain click opens nothing (the app handles it in place), so the arrow and the new-tab words are left out; `target` and `rel` stay for the page before JavaScript. Disabled, it opens nothing either: no `target`, `rel` or new-tab words (the arrow stays, as `iconAfter` would). |

Behavior props from React Aria:

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| autoFocus | `boolean` |  | no | Whether the element should receive focus on render. |
| onFocus | `(e: FocusEvent<Element, Element>) => void` |  | no | Handler that is called when the element receives focus. |
| onBlur | `(e: FocusEvent<Element, Element>) => void` |  | no | Handler that is called when the element loses focus. |
| onClick | `(e: MouseEvent<FocusableElement, MouseEvent>) => void` |  | no | **Not recommended – use `onPress` instead.** `onClick` is an alias for `onPress` provided for compatibility with other libraries. `onPress` provides additional event details for non-mouse interactions. |
| isDisabled | `boolean` |  | no | Whether the button is disabled. |
| onPress | `(e: PressEvent) => void \| (event: MouseEvent<HTMLAnchorElement, MouseEvent>) => void` |  | no | Handler that is called when the press is released over the target. |
| onPressStart | `(e: PressEvent) => void` |  | no | Handler that is called when a press interaction starts. |
| onPressEnd | `(e: PressEvent) => void` |  | no | Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. |
| onPressChange | `(isPressed: boolean) => void` |  | no | Handler that is called when the press state changes. |
| onPressUp | `(e: PressEvent) => void` |  | no | Handler that is called when a press is released over the target, regardless of whether it started on the target or not. |
| onFocusChange | `(isFocused: boolean) => void` |  | no | Handler that is called when the element's focus status changes. |
| type | `"button" \| "submit" \| "reset"` |  | no | The behavior of the button when used in an HTML form. |
| preventFocusOnPress | `boolean` |  | no | Whether to prevent focus from moving to the button when pressing it. Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control. |
| form | `string` |  | no | The `<form>` element to associate the button with. The value of this attribute must be the id of a `<form>` in the same document. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/button#form). |
| formAction | `string \| ((formData: FormData) => void \| Promise<void>)` |  | no | The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner. |
| formEncType | `string` |  | no | Indicates how to encode the form data that is submitted. |
| formMethod | `string` |  | no | Indicates the HTTP method used to submit the form. |
| formNoValidate | `boolean` |  | no | Indicates that the form is not to be validated when it is submitted. |
| formTarget | `string` |  | no | Overrides the target attribute of the button's form owner. |
| name | `string` |  | no | Submitted as a pair with the button's value as part of the form data. |
| value | `string` |  | no | The value associated with the button's name when it's submitted with the form data. |
| excludeFromTabOrder | `boolean` |  | no | Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. |
| isPending | `boolean` |  | no | Whether the button is in a pending state. This disables press and hover events while retaining focusability, and announces the pending state to screen readers. |
| onHoverStart | `(e: HoverEvent) => void` |  | no | Handler that is called when a hover interaction starts. |
| onHoverEnd | `(e: HoverEvent) => void` |  | no | Handler that is called when a hover interaction ends. |
| onHoverChange | `(isHovering: boolean) => void` |  | no | Handler that is called when the hover state changes. |
| rel | `string` |  | no | The relationship between the linked resource and the current page. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel). |
| hrefLang | `string` |  | no | Hints at the human language of the linked URL. See[MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#hreflang). |
| download | `string \| boolean` |  | no | Causes the browser to download the linked URL. A string may be provided to suggest a file name. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#download). |
| ping | `string` |  | no | A space-separated list of URLs to ping when the link is followed. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#ping). |
| referrerPolicy | `HTMLAttributeReferrerPolicy` |  | no | How much of the referrer to send when following the link. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#referrerpolicy). |

Also takes 84 standard DOM and ARIA attributes (id, aria-label, event handlers…).

### Tokens it reads

`--ds-background-brand-bold`, `--ds-background-brand-bold-hovered`, `--ds-background-brand-bold-pressed`, `--ds-background-danger`, `--ds-background-danger-bold`, `--ds-background-danger-bold-hovered`, `--ds-background-disabled`, `--ds-background-neutral`, `--ds-background-neutral-hovered`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-background-selected`, `--ds-background-selected-hovered`, `--ds-background-selected-pressed`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-border-selected`, `--ds-easing-standard`, `--ds-link`, `--ds-link-pressed`, `--ds-radius-control-medium`, `--ds-radius-full`, `--ds-size-control`, `--ds-size-control-compact`, `--ds-size-control-large`, `--ds-size-icon`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-text`, `--ds-text-danger`, `--ds-text-disabled`, `--ds-text-on-brand`, `--ds-text-on-danger`, `--ds-text-subtle`

## Related

- [button-group](https://design.senvenli.com/docs/components/button-group.md)
- [toggle-button](https://design.senvenli.com/docs/components/toggle-button.md)
- [link](https://design.senvenli.com/docs/components/link.md)
