# Button

> A button that submits a form, opens a dialog or runs an action, or a link that looks like one; with an icon, or icon-only for toolbars and table rows.

Source: https://shadleaf.dev/1.2.0/components/button/

A button the user presses to do something: submit a form, open a dialog, delete a row, start an htmx request.
With `as="a"` it is a link that looks like a button, for navigation that should stand out.

**Example: Button.**

```html
<sl:button>Save</sl:button>
```

## When to use

- The main action of a form or a page (`default`), and the actions next to it (`outline`, `secondary`, `ghost`).
- An action that destroys something, such as Delete (`destructive`).
- An icon-only action in a toolbar, a table row or a card header (the `icon` sizes).
- A link that should look like an action, such as "Create project" leading to a form (`as="a"`).

Use something else for:

- A button that stays on or off, such as Bold in a toolbar: a [toggle](https://shadleaf.dev/1.2.0/components/toggle.md), or a
  [toggle group](https://shadleaf.dev/1.2.0/components/toggle-group.md) for several.
- A list of actions behind one button: a [dropdown menu](https://shadleaf.dev/1.2.0/components/dropdown-menu.md).
- A link inside running text: a plain `<a>`.

## A button never submits by accident

`<sl:button>` renders `type="button"`, not HTML's default `submit`. A button inside a form therefore does nothing
unless you say so: write `type="submit"` on the one that should submit.

**Example: In a form.** Only the button that says type="submit" submits the form.

```html
<form method="post" th:action="@{/orders}">
  <sl:button type="submit">Save</sl:button>
  <sl:button variant="outline">Preview</sl:button>
</form>
```

## Variants

**Example: Variants.**

```html
<sl:button>Default</sl:button>
<sl:button variant="outline">Outline</sl:button>
<sl:button variant="ghost">Ghost</sl:button>
<sl:button variant="destructive">Destructive</sl:button>
<sl:button variant="secondary">Secondary</sl:button>
<sl:button variant="link">Link</sl:button>
```

## Sizes

**Example: Sizes.**

```html
<sl:button size="xs">Extra small</sl:button>
<sl:button size="sm">Small</sl:button>
<sl:button>Default</sl:button>
<sl:button size="lg">Large</sl:button>
```

The `icon` sizes are square, for a button holding only an icon. Such a button has no text for a screen reader, so it
must have an `aria-label` (or `aria-labelledby`); rendering fails without one.

**Example: Icon-only sizes.** An icon-only button needs an aria-label; rendering fails without one.

```html
<sl:button size="icon-xs" variant="outline" aria-label="Add"><sl:icon name="plus"/></sl:button>
<sl:button size="icon-sm" variant="outline" aria-label="Add"><sl:icon name="plus"/></sl:button>
<sl:button size="icon" variant="outline" aria-label="Add"><sl:icon name="plus"/></sl:button>
<sl:button size="icon-lg" variant="outline" aria-label="Add"><sl:icon name="plus"/></sl:button>
```

### Hit area on a touch screen

The `icon`, `icon-xs` and `icon-sm` buttons are smaller than the 44 by 44 pixels that touch guidelines ask for. On a
touch screen (`@media (pointer: coarse)`) such a button gets an invisible area around it, so that it is at least 44 by
44 pixels to tap, without looking any bigger: a tap just outside the button still presses it. With a mouse, the hit area
is the button itself. Buttons that touch each other (a row without a gap) only grow in height, so one never takes a tap
from its neighbour, and a button in an `sl:input-group` is not enlarged. `icon-lg` is large enough as it is.

## With an icon

Put an [`<sl:icon>`](https://shadleaf.dev/1.2.0/components/icon.md) in the `icon-start` or `icon-end` slot. It takes the button's colour and a 16 pixel size.

**Example: With an icon.**

```html
<sl:button variant="destructive">
  <sl:slot name="icon-start"><sl:icon name="trash"/></sl:slot>
  Delete order
</sl:button>
<sl:button variant="outline">
  Next
  <sl:slot name="icon-end"><sl:icon name="arrow-right"/></sl:slot>
</sl:button>
```

## As a link

`as="a"` renders an anchor that looks the same, for navigation.

**Example: As a link.**

```html
<sl:button as="a" href="/orders" variant="outline">View orders</sl:button>
<sl:button as="a" href="/orders" variant="link">View orders</sl:button>
```

## The cursor

A button keeps the arrow cursor on hover, as in shadcn/ui and Tailwind CSS 4; only `as="a"` shows the hand, because
it is a link. To show the hand on every button, add this rule to your own stylesheet:

```css
@layer base {
  button:not(:disabled),
  [role="button"]:not(:disabled) {
    cursor: pointer;
  }
}
```

It lives in the base layer, below Shadleaf's components, so a component that sets its own cursor (a tab, a menu item)
keeps it.

## As a label

`as="label"` renders a `<label>` that looks the same, for a control that is visually hidden: `for` points at it, and
a click on the label activates the control. [`<sl:file-upload>`](https://shadleaf.dev/1.2.0/components/file-upload.md)'s Browse button is one, for its file
input. The label takes no focus and no `disabled`; the control does, so draw the focus ring and the disabled look from
the control's state (`:has()`), as the file upload does.

```html
<input type="file" id="photo" name="photo" class="sl-sr-only">
<sl:button as="label" for="photo" variant="outline">Choose a photo</sl:button>
```

## Disabled and loading

A disabled button gets the native `disabled` attribute. A disabled link loses its `href`, leaves the tab order and is
marked `aria-disabled`, so it cannot be followed. `loading` shows a spinner in place of the start icon, announces the
busy state with `aria-busy` and a visually hidden "Loading" label, and disables the button.

**Example: Disabled.**

```html
<sl:button disabled>Save</sl:button>
<sl:button as="a" href="/orders" variant="outline" disabled>View orders</sl:button>
```
**Example: Loading.**

```html
<sl:button loading>Saving</sl:button>
<sl:button variant="outline" loading>
  <sl:slot name="icon-start"><sl:icon name="save"/></sl:slot>
  Save
</sl:button>
<sl:button size="icon" variant="outline" aria-label="Refresh" loading><sl:icon name="refresh-cw"/></sl:button>
```

The "Loading" label comes from the message `sl.button.loading`; define it in your own `messages.properties` to
translate or reword it, as [Translating](https://shadleaf.dev/1.2.0/guides/translating.md) explains.

## Props from expressions

Every prop also has a `th:` form that takes a Thymeleaf expression.

**Example: Props from an expression.**

```html
<sl:button th:variant="${order.cancellable ? 'destructive' : 'secondary'}"
           th:text="#{orders.cancel}">Cancel order</sl:button>
```

## Everything else passes through

Attributes that are not props, such as `hx-*`, `x-*`, `aria-*` and `data-*`, land on the rendered element unchanged,
so the button works with htmx and Alpine. `class` is appended to the component's own class.

**Example: Pass-through attributes.** Anything that is not a prop lands on the rendered element; class is appended.

```html
<sl:button variant="outline" class="app-wide"
           hx-post="/orders/42/archive" hx-target="#orders"
           data-testid="archive">Archive</sl:button>
```

## Attributes

Attributes of `<sl:button>`: A button, or with `as="a"` an anchor that looks the same. Every attribute that is not a prop (`hx-*`, `x-*`, `aria-*`, `data-*`, `href`) passes through; `class` is appended to `button`.

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `variant` | `default`, `outline`, `ghost`, `destructive`, `secondary`, `link` | `default` | Visual style of the button. |
| `size` | `xs`, `sm`, `default`, `lg`, `icon`, `icon-xs`, `icon-sm`, `icon-lg` | `default` | Size of the button. The `icon` sizes are square, for a button that holds only an icon, and need an `aria-label`. |
| `as` | `button`, `a`, `label` | `button` | Element to render: a `button`, an anchor (`a`) for navigation, or a `label` for a visually hidden control (`for`), as `sl:file-upload`'s Browse button for its file input. A label takes no focus and no `disabled`: the control does. |
| `type` | `button`, `submit`, `reset` | `button` | Submit behaviour. Defaults to `button`, unlike HTML, so a button in a form never submits by accident. |
| `disabled` | *boolean* | — | Disable the button. A disabled anchor loses its `href` and leaves the tab order. |
| `loading` | *boolean* | — | Show a spinner, announce the busy state and disable the button. |

Every prop can also be set from an expression: `th:variant="${...}"`. Any other attribute passes through to the rendered element, and `class` is appended. An `aria-label` or `aria-labelledby` is required when `size` is `icon` or `icon-xs` or `icon-sm` or `icon-lg`; rendering fails without one.

## Styling hooks

The classes and attributes your own CSS can select to restyle the component, as explained in
[your own CSS rules](https://shadleaf.dev/1.2.0/guides/adjustability.md#5-your-own-css-rules).

Styling hooks of `<sl:button>`.

**Parts**

| Class | Element |
| --- | --- |
| `.button` | `<a>`, `<button>`, `<label>`, `<summary>` |
| `.button-icon` | `<span>` |
| `.button-spinner` | `<svg>` |

**Props**

| Prop | Selector | Values |
| --- | --- | --- |
| `size` | `.button[data-size="…"]`, `.button:not([data-size])` | `xs`, `sm`, `lg`, `icon`, `icon-xs`, `icon-sm`, `icon-lg`; `default` (default) |
| `variant` | `.button[data-variant="…"]`, `.button:not([data-variant])` | `outline`, `ghost`, `destructive`, `secondary`, `link`; `default` (default) |

**States**

| Selector | Values |
| --- | --- |
| `.button[aria-current="…"]` | `page` |
| `.button[aria-disabled="…"]` | `true` |
| `.button[aria-expanded="…"]` | `true` |
| `.button[aria-haspopup="…"]` | `dialog`, `menu` |
| `.button[aria-invalid="…"]` | `true` |
| `.button[data-empty="…"]` | `true` |
| `.button-icon[data-icon="…"]` | `inline-end`, `inline-start` |
