# Icon Button

## Usage

`IconButton` displays one icon. The icon must clearly identify the action. Set `label` to provide an accessible name:

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

<template>
  <IconButton label="Search">
    <Icon name="carbon:search" />
  </IconButton>
</template>
```

[Icon](/en/components/icon) fills the square the button already sizes. Leave `size` unset. A CSS icon class still works in the slot when your build generates it; UnoCSS is not required.

Use a text-bearing [Button](/en/components/button) when the action is not immediately clear.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Make the aria-label specific: a trash icon labeled "Delete conversation" is clearer than just "Delete" for screen readers. |
| Do | Add a tooltip: even a gear icon can mean Settings, Preferences, or Configure. |
| Do | Use ghost in toolbars and dense areas to reduce visual clutter. |
| Don't | Use IconButton if the action isn't obvious from the icon alone; use Button with text. |
| Don't | Skip the tooltip; label only reaches screen readers, sighted users need the hover hint. |

## Examples

### Action bar

Row of ghost icon buttons for a compact action toolbar.

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

<template>
  <HStack :gap="1">
    <IconButton label="Search" variant="ghost">
      <Icon icon="i-heroicons-magnifying-glass" color="inherit" />
    </IconButton>
    <IconButton label="Copy" variant="ghost">
      <Icon icon="i-heroicons-document-duplicate" color="inherit" />
    </IconButton>
    <IconButton label="Info" variant="ghost">
      <Icon icon="i-heroicons-information-circle" color="inherit" />
    </IconButton>
    <IconButton label="Menu" variant="ghost">
      <Icon icon="i-heroicons-bars-3" color="inherit" />
    </IconButton>
    <IconButton label="Close" variant="ghost">
      <Icon icon="i-heroicons-x-mark" color="inherit" />
    </IconButton>
  </HStack>
</template>
```

### Floating

A floating action button. The most common floating-button shape is icon-only. Raised here with elevation="high".

```vue
<script setup lang="ts">
import { Icon } from '@astryx-vue/core/Icon'
import { IconButton } from '@astryx-vue/core/IconButton'
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">
      FABs are usually icon-only — raise one with `elevation="high"`
    </Text>
    <Stack align="center" direction="horizontal" :gap="3">
      <IconButton v-for="item in variants" :key="item.variant" :label="item.label" :variant="item.variant" elevation="high">
        <Icon icon="i-heroicons-plus" />
      </IconButton>
    </Stack>
  </Stack>
</template>
```

### Loading state

Icon buttons that show a loading spinner on click for async feedback.

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

const loadingId = ref<string | null>(null)

function handleClick(id: string) {
  loadingId.value = id
  setTimeout(() => {
    loadingId.value = null
  }, 1500)
}
</script>

<template>
  <HStack :gap="2">
    <IconButton label="Copy" variant="primary" :is-loading="loadingId === 'copy'" @click="handleClick('copy')">
      <Icon icon="i-heroicons-document-duplicate" color="inherit" />
    </IconButton>
    <IconButton label="Search" :is-loading="loadingId === 'search'" @click="handleClick('search')">
      <Icon icon="i-heroicons-magnifying-glass" color="inherit" />
    </IconButton>
    <IconButton label="Close" variant="ghost" :is-loading="loadingId === 'close'" @click="handleClick('close')">
      <Icon icon="i-heroicons-x-mark" color="inherit" />
    </IconButton>
  </HStack>
</template>
```

### With tooltips

Icon buttons with tooltips that explain each action on hover.

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

<template>
  <HStack :gap="2">
    <IconButton label="Search" variant="ghost" tooltip="Search items">
      <Icon icon="i-heroicons-magnifying-glass" color="inherit" />
    </IconButton>
    <IconButton label="Copy link" variant="ghost" tooltip="Copy to clipboard">
      <Icon icon="i-heroicons-document-duplicate" color="inherit" />
    </IconButton>
    <IconButton label="More options" variant="ghost" tooltip="More options">
      <Icon icon="i-heroicons-ellipsis-horizontal" color="inherit" />
    </IconButton>
  </HStack>
</template>
```

## IconButton Props

IconButton inherits most Button props but does not expose <code>isIconOnly</code>, because it is always icon-only.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | required | Accessible name for the button. |
| `variant` | `'primary' \| 'secondary' \| 'ghost' \| 'destructive'` | `'secondary'` | Visual hierarchy and semantic variant. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Square control size. |
| `elevation` | `'none' \| 'low' \| 'med' \| 'high'` | `'none'` | Resting shadow depth for a standalone floating button. |
| `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. |
| `tooltip` | `string` | — | Supplementary hint shown on hover and keyboard focus. |
| `href` | `string` | — | Renders the enabled root as a link when present. |
| `width` | `number \| string` | — | Button width. Numbers become pixels; CSS strings are used as supplied. |

These props are also inherited. See [Button props](/en/components/button/props) for their definitions.

- `type`.
- `name`.
- `value`.
- `form`.
- `loadingLabel`.
- `as`.
- `target`.
- `rel`.

## Slot

- The default slot is required.
- The default slot must contain one icon that represents the action.
- `IconButton` does not display text.
- `IconButton` does not provide an `end` slot.

:::warning Accessible name
Provide an accurate `label`. Without it, assistive technology cannot identify the button's purpose.

`tooltip` provides additional information. Do not use it instead of `label`.
:::

## Events

The `click` event passes a `MouseEvent`.

## Template ref

`IconButton` 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

### Essential icon or spinner arc

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Rest, Hover, Pointer down, Loading

IconButton has no visible label. Its icon must have at least 3:1 contrast with the button background in Rest, Hover, and Pointer down. The moving spinner arc must also meet 3:1 while loading.

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

### Keyboard focus indicator

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

The focus outline must have at least 3:1 contrast with the area around the button. Check the red outline on destructive buttons too.

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