# Button

A native `<button>` in one of four variants and three sizes. Use it for actions; use [Link](https://design-system.blocks-ui.com/components/link.md) to go somewhere.

```tsx
import { Button } from '@blocks/design-system'
```

## Forms

Button forwards `type` and every other attribute, so it behaves like the HTML element: inside a form it is a submit button unless you pass `type="button"`. A Server Component can render a whole form with a plain submit button and no JavaScript; event handlers belong in a client-authored caller.

**Example: Save a station** (`examples/button/save-station.tsx`)

```tsx
import { Box, Button, Stack } from '@blocks/design-system'

// A Server Component: a native form submits with no JavaScript at all.
export default function SaveStation() {
  return (
    <Stack as="form" action="/components/button" method="get" gap="sm" aria-label="Station name">
      <Box as="label" htmlFor="station-name">
        Station name
      </Box>
      <Box as="input" id="station-name" name="station" defaultValue="Mount Hood" padding="xs" border="default" />
      <Stack direction="row" gap="sm">
        <Button type="submit" variant="primary">
          Save station
        </Button>
        <Button as="a" href="/components/button#variants" variant="ghost">
          See all variants
        </Button>
      </Stack>
    </Stack>
  )
}
```

Give every button that shouldn’t submit an explicit `type="button"`, like this Cancel.

**Example: Edit with cancel** (`examples/button/edit-with-cancel.tsx`)

```tsx
'use client'

import { Box, Button, Stack } from '@blocks/design-system'
import { type FormEvent, useState } from 'react'

export default function EditWithCancel() {
  const [saved, setSaved] = useState({ name: 'Launch checklist', count: 0 })
  const [draft, setDraft] = useState(saved.name)

  function save(event: FormEvent<HTMLFormElement>) {
    event.preventDefault()
    setSaved((previous) => ({ name: draft, count: previous.count + 1 }))
  }

  return (
    <Stack as="form" gap="sm" onSubmit={save} aria-label="Rename item">
      <Box as="label" htmlFor="item-name">
        Item name
      </Box>
      <Box as="input" id="item-name" value={draft} onChange={(event) => setDraft(event.target.value)} padding="xs" border="default" />
      <Stack direction="row" gap="sm">
        <Button type="submit" variant="primary">
          Save
        </Button>
        {/* An explicit type: a button in a form submits unless told otherwise. */}
        <Button type="button" onClick={() => setDraft(saved.name)}>
          Cancel
        </Button>
      </Stack>
      <p role="status">
        Saved “{saved.name}” {saved.count} {saved.count === 1 ? 'time' : 'times'}
      </p>
    </Stack>
  )
}
```

## Variants

`primary` for the main action, `base` (the default) for the rest, `ghost` for low emphasis and `link` for an action that reads like text.

**Example: Variants** (`examples/button/variants.tsx`)

```tsx
import { Button, Stack } from '@blocks/design-system'

export default function Variants() {
  return (
    <Stack direction="row" align="center" gap="md" wrap="wrap">
      <Button type="button" variant="primary">
        Publish
      </Button>
      <Button type="button">Save draft</Button>
      <Button type="button" variant="ghost">
        Discard
      </Button>
      <Button type="button" variant="link">
        View history
      </Button>
    </Stack>
  )
}
```

## Sizes

`sm`, `md` (the default) and `lg` follow the control-height tokens.

**Example: Sizes** (`examples/button/sizes.tsx`)

```tsx
import { Button, Stack } from '@blocks/design-system'

export default function Sizes() {
  return (
    <Stack direction="row" align="center" gap="md" wrap="wrap">
      <Button type="button" size="sm">
        Retry
      </Button>
      <Button type="button">Retry</Button>
      <Button type="button" size="lg">
        Retry
      </Button>
    </Stack>
  )
}
```

## Links that look like buttons

`as="a"` renders an anchor with the button’s styles, as with “See all variants” above. It is still a link: Enter follows it, Space doesn’t.

## Disabled

`disabled` on a button removes it from the tab order and blocks clicks. An anchor can’t be disabled; `aria-disabled="true"` styles it as unavailable, and the caller should leave out its `href`.

**Example: Disabled** (`examples/button/disabled.tsx`)

```tsx
import { Button, Stack } from '@blocks/design-system'

export default function Disabled() {
  return (
    <Stack direction="row" gap="md" wrap="wrap">
      <Button type="button" disabled>
        Publish
      </Button>
      <Button as="a" aria-disabled="true" variant="ghost">
        Export (unavailable)
      </Button>
    </Stack>
  )
}
```

## Props

**`Button` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `as` | `ElementType` | — | The element or component to render, e.g. `as={NextLink}`. |
| `borderRadius` | `"button" \| "circle" \| "sm" \| "md" \| "xl" \| "xxl" \| "xxxl" \| "none" \| "pill"` | `button` | A border radius token, e.g. `"md"`, `"pill"` or `"circle"`. |
| `fontWeight` | `"body" \| "heading" \| "medium" \| "bold" \| "semibold"` | `semibold` | A font weight token: `body`, `medium`, `semibold`, `heading` or `bold`. |
| `size` | `"sm" \| "md" \| "lg"` | `md` | `sm`, `md` or `lg`. Defaults to `"md"`. |
| `variant` | `"base" \| "link" \| "primary" \| "ghost"` | `base` | `base`, `primary`, `ghost` or `link`. Defaults to `"base"`. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [margin props](https://design-system.blocks-ui.com/components/box.md#spacing), and the native attributes of the element it renders.

## Accessibility

- A button’s text is its name. An icon-only button needs an `aria-label`.
- The focus ring is drawn by Button itself, so it stays visible without the global reset.
- Don’t disable a submit button to show that a form is incomplete; let it submit and explain the error next to the field.
