# Button

## Usage

`Button` triggers an action. Set `label` to name the action.

Without a default slot, the button displays `label`:

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
</script>

<template>
  <Button label="Save changes" variant="primary" />
</template>
```

Use the dedicated [ButtonGroup](/en/components/button-group) and [IconButton](/en/components/icon-button) components when you need connected actions or an explicit icon-only button.

The `icon` slot sits in a sized box before the label, and the `end` slot mirrors it. Put an [Icon](/en/components/icon) there and leave its `size` unset; the slot already decides the box:

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Icon } from '@astryx-vue/core/Icon'
</script>

<template>
  <Button label="Upload file">
    <template #icon>
      <Icon name="carbon:upload" />
    </template>
  </Button>
  <Button label="Continue" variant="secondary">
    <template #end>
      <Icon name="carbon:chevron-right" />
    </template>
  </Button>
</template>
```

A CSS icon class still works in the slot if you already generate one. UnoCSS is not required.

On a touch-first device such as a phone or tablet, a button's tap target grows toward 44 px while the button keeps its size. The growth stops halfway to the nearest interactive neighbor, so a tap between two controls always goes to the nearer one. A disabled or busy button does not grow, and a button in a group grows through its group. A mouse sees no change.

### Anatomy

- **Icon**: A leading icon that reinforces the label, like a trash icon on a Delete button.
- **Label**: The visible text describing the action. Also used as the accessible name.
- **End content**: A trailing badge or icon after the label, like a notification count or dropdown arrow.
- **Spinner**: Replaces the icon during loading to show the action is in progress.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Reserve primary for the single most important action in the view. Use secondary or ghost for everything else based on emphasis. |
| Do | Write labels that describe the action ("Save changes", "Delete account", "Send invite"), not vague labels like "OK" or "Click here". |
| Do | Show a loading state for actions that take time, like saving or submitting, so the user knows it is working. |
| Do | Always provide a label for icon-only buttons so screen readers can announce what the button does. Add a tooltip for sighted users. |
| Do | For a dedicated icon-only button, use IconButton from '@astryx-vue/core/IconButton'. It is a separate component, not exported from '@astryx-vue/core/Button'. |
| Don't | Place more than one primary button in the same view; this dilutes the visual hierarchy. |
| Don't | Use the destructive variant without a confirmation step for irreversible actions like deleting data. |
| Don't | Use a button for navigation. If it only takes the user to another page, use a link instead. Buttons are for actions like saving, deleting, or submitting. |
| Don't | Restyle Button's `::before` and expect it to hold through a touch press: under a coarse primary pointer, while a press is on or fading, Button paints the press on that pseudo-element (`z-index: -1`, its opacity animated), so your styles merge into the press layer for that moment. At rest Button adds nothing there. Style a child element instead. |

## Examples

### Floating

A floating action button raised with elevation="med". Use elevation for buttons that hover above content, like a FAB.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Icon } from '@astryx-vue/core/Icon'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const variants = [
  { variant: 'primary' as const, label: 'Primary' },
  { variant: 'secondary' as const, label: 'Secondary' },
  { variant: 'ghost' as const, label: 'Ghost' },
  { variant: 'destructive' as const, label: 'Destructive' },
]
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Text type="supporting" color="secondary">
      Floating action buttons — raised above content with `elevation="med"`
    </Text>
    <Stack align="center" direction="horizontal" :gap="3">
      <Button v-for="item in variants" :key="item.variant" :label="item.label" :variant="item.variant" elevation="med">
        <template #icon>
          <Icon icon="i-heroicons-plus" />
        </template>
      </Button>
    </Stack>
  </Stack>
</template>
```

### Sizes

Small, medium, and large buttons side by side. Use small in dense UIs like toolbars, medium for most cases, and large for prominent CTAs.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const sizes = [
  { size: 'sm' as const, label: 'Small' },
  { size: 'md' as const, label: 'Medium' },
  { size: 'lg' as const, label: 'Large' },
]
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Primary
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <Button v-for="item in sizes" :key="item.size" :label="item.label" variant="primary" :size="item.size" />
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Secondary
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <Button v-for="item in sizes" :key="item.size" :label="item.label" variant="secondary" :size="item.size" />
      </Stack>
    </Stack>
  </Stack>
</template>
```

### Variants

All 4 button variants in default, disabled, and loading states. Use primary for the main action, secondary for most others, ghost for low-emphasis, and destructive for dangerous actions.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const variants = [
  { variant: 'primary' as const, label: 'Primary' },
  { variant: 'secondary' as const, label: 'Secondary' },
  { variant: 'ghost' as const, label: 'Ghost' },
  { variant: 'destructive' as const, label: 'Destructive' },
]
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Default
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <Button v-for="item in variants" :key="item.variant" :label="item.label" :variant="item.variant" />
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Disabled
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <Button v-for="item in variants" :key="item.variant" :label="item.label" :variant="item.variant" is-disabled />
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Loading
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <Button v-for="item in variants" :key="item.variant" :label="item.label" :variant="item.variant" is-loading />
      </Stack>
    </Stack>
  </Stack>
</template>
```

### End slot

Buttons with a trailing badge showing a count or status. Use for notification counts, unread messages, or any button that needs a visual indicator.

```vue
<script setup lang="ts">
import { Badge } from '@astryx-vue/core/Badge'
import { Button } from '@astryx-vue/core/Button'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Text type="supporting" color="secondary">
      Trailing badges for counts or status
    </Text>
    <Stack align="center" direction="horizontal" :gap="3">
      <Button label="Messages" variant="primary">
        <template #end>
          <Badge variant="info">
            3
          </Badge>
        </template>
      </Button>
      <Button label="Notifications" variant="secondary">
        <template #end>
          <Badge variant="warning">
            12
          </Badge>
        </template>
      </Button>
      <Button label="Updates" variant="ghost">
        <template #end>
          <Badge variant="neutral">
            New
          </Badge>
        </template>
      </Button>
    </Stack>
  </Stack>
</template>
```

### Icon

Buttons with a leading icon that reinforces the label. Use when the icon helps the user identify the action faster, like a plus for New or a trash can for Delete.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Icon } from '@astryx-vue/core/Icon'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Text type="supporting" color="secondary">
      Icons reinforce the action
    </Text>
    <Stack align="center" direction="horizontal" :gap="3">
      <Button label="New item" variant="primary">
        <template #icon>
          <Icon icon="i-heroicons-plus" />
        </template>
      </Button>
      <Button label="Edit" variant="secondary">
        <template #icon>
          <Icon icon="i-heroicons-pencil-square" />
        </template>
      </Button>
      <Button label="Download" variant="ghost">
        <template #icon>
          <Icon icon="i-heroicons-arrow-down-tray" />
        </template>
      </Button>
      <Button label="Delete" variant="destructive">
        <template #icon>
          <Icon icon="i-heroicons-trash" />
        </template>
      </Button>
    </Stack>
  </Stack>
</template>
```

## Button Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | required | Names the action and becomes visible text when no default slot is present. |
| `variant` | `'primary' \| 'secondary' \| 'ghost' \| 'destructive'` | `'secondary'` | Visual hierarchy and semantic variant. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Button size. |
| `elevation` | `'none' \| 'low' \| 'med' \| 'high'` | `'none'` | Resting shadow depth for a standalone floating button; ButtonGroup owns it inside a group. |
| `type` | `'button' \| 'submit' \| 'reset'` | `'button'` | Native button type. |
| `name` | `string` | — | Form submission name. |
| `value` | `string \| number \| readonly string[]` | — | Form submission value. |
| `form` | `string` | — | ID of the associated form. |
| `isDisabled` | `boolean` | `false` | Disables interaction. The button never uses the native disabled attribute: it is marked aria-disabled and stays in the Tab order, so keyboard and screen-reader users can still reach it. |
| `isLoading` | `boolean` | `false` | Shows loading state, sets aria-busy, and disables non-interruptible activation. |
| `isInterruptible` | `boolean` | `false` | Allows clickAction to run again while earlier work is pending. |
| `clickAction` | `(event: MouseEvent) => void \| Promise<void>` | — | Action function that automatically drives pending state. |
| `isIconOnly` | `boolean` | `false` | Shows only the icon slot and uses label to name the button. |
| `width` | `number \| string` | — | Button width. Numbers become pixels; CSS strings are used as supplied. When a row is too narrow the label truncates with an ellipsis and shows in full in a tooltip on keyboard focus. |
| `loadingLabel` | `string` | `'Loading'` | Localized text used by the spinner and live region. |
| `tooltip` | `string` | — | Supplementary hint shown on hover and keyboard focus. |
| `href` | `string` | — | Renders the enabled root as a link when present. |
| `as` | `string \| Component` | `'a'` | Custom Vue link component for href mode. It receives both href and to. |
| `target` | `string` | — | Link browsing context. |
| `rel` | `string` | — | Link relationship attribute. |

## Async actions

- `@click` runs before `clickAction`.
- If `@click` calls `event.preventDefault()`, the component skips `clickAction`.
- If `clickAction` returns a Promise, the component enters the loading state.
- When the Promise fulfills or rejects, the component clears the loading state.
- The application must catch errors inside `clickAction`.

### Repeated activation

- If `isInterruptible` is `false`, loading blocks another activation.
- If `isInterruptible` is `true`, loading permits another activation.
- Repeated activation runs multiple actions concurrently.
- The component does not cancel earlier Promises.

To cancel an earlier network request, call `AbortController.abort()` in the application.

## Button Slots

| Name | Description |
| --- | --- |
| `default` | Optional visible-content override. label remains the stable accessible name. |
| `icon` | Decorative icon before the label. |
| `end` | Short trailing decoration such as a badge or icon. Ignored in icon-only mode. |

## Button Events

| Name | Payload | Description |
| --- | --- | --- |
| `click` | `MouseEvent` | Cancellable Vue component event. It is not emitted while disabled, non-interruptible loading, or deduplicated. |

## Template ref

`Button` exposes `element`.
Its type is `HTMLButtonElement | HTMLAnchorElement | HTMLElement | null`.
In link mode (with `href` and not disabled) it is the link element: the anchor, or the root element of the `as` link component. Otherwise it is the button.

## Native attributes

The root element receives:

- HTML attributes.
- `aria-*` attributes.
- `data-*` attributes.
- `class`.
- `style`.
- Native event listeners.

## Accessibility

### Text label

- Criterion: 1.4.3 Contrast (Minimum)
- Check: 4.5:1
- States: Rest, Hover, Pointer down

Button text must have at least 4.5:1 contrast with the button background in every state. For Hover and Pointer down, measure the final background after the overlay is applied.

### Essential icon or spinner arc

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Icon only, Loading

An icon used instead of text must have at least 3:1 contrast with the button background. The moving spinner arc must also meet 3:1. An icon beside a visible label does not need its own check.

### Badge text

- Criterion: 1.4.3 Contrast (Minimum)
- Check: 4.5:1
- States: Rest, Hover, Pointer down

Badge text inside a button must have at least 4.5:1 contrast with the Badge background. Check all 14 built-in Badge colors in Rest, Hover, and Pointer down on page and surface backgrounds. This covers 336 pairs per mode. Check custom end content separately.

### Visible control boundary

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1 if needed
- States: Rest

The button edge needs 3:1 contrast only when users need it to see the control. A text-only button can rely on its label.

### Keyboard focus indicator

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Focus visible

The focus outline needs at least 3:1 contrast with the area around the button. Check every style. Destructive buttons use a red outline.

### Disabled appearance

- Criterion: 1.4.3 and 1.4.11 exceptions
- Check: Not required
- States: Disabled

Disabled controls do not need to meet these contrast ratios.
