# 纯图标按钮

## 使用方法

`IconButton` 是一个只含图标的方形按钮，适合工具栏、列表操作等空间紧凑、含义明确的场景。由于按钮没有可见文字，必须通过 `label` 提供无障碍名称，辅助技术才能识别它的用途：

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

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

[Icon](/components/icon) 会铺满按钮已经定好的方框，不要再设 `size`。构建里已经会生成的 CSS 图标类仍然可以放进这个槽，不必为此安装 UnoCSS。

如果操作的含义无法凭图标一眼看出，请改用带文字的 [Button](/components/button)。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 让 `label` 尽量具体：标有 “Delete conversation” 的垃圾桶图标，比只写 “Delete” 更便于屏幕阅读器用户理解按钮的作用。 |
| 推荐 | 为按钮加上提示框。即便是常见的齿轮图标，在不同产品中也可能表示 Settings、Preferences 或 Configure，悬停提示能减少用户的猜测。 |
| 推荐 | 在工具栏和紧凑区域使用 ghost 变体，视觉重量更轻，不会干扰旁边的正文内容。 |
| 避免 | 避免在操作含义不直观时仍然只放图标：用户看不出按钮能做什么，这类操作应改用带文字的 Button。 |
| 避免 | 避免省略提示框：`label` 只供屏幕阅读器使用，普通用户还需要悬停或键盘聚焦时的文字提示。 |

## 示例

### 操作条

一排 ghost 图标按钮，组成紧凑的操作条。

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

### 悬浮

悬浮操作按钮。最常见的悬浮按钮是纯图标。这里用 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>
```

### 加载状态

点击后显示加载指示，用来反馈异步操作。

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

### 带提示

图标按钮在悬停时用提示说明每个操作。

```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 继承 Button 的大部分属性，但不提供 <code>isIconOnly</code>，因为它始终是纯图标模式。

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `label` | `string` | 必需 | 按钮的无障碍名称，供辅助技术识别按钮用途。 |
| `variant` | `'primary' \| 'secondary' \| 'ghost' \| 'destructive'` | `'secondary'` | 按钮的视觉样式，同时暗示操作的类型。 |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 按钮尺寸，IconButton 始终渲染为正方形。 |
| `elevation` | `'none' \| 'low' \| 'med' \| 'high'` | `'none'` | 静止状态的阴影深度，适合悬浮于内容上方的独立按钮。 |
| `isDisabled` | `boolean` | `false` | 是否禁用，禁用后不再响应交互。按钮不用原生 disabled，而是标记 aria-disabled 并留在 Tab 顺序里，键盘和读屏用户仍能聚焦、读到它。 |
| `isLoading` | `boolean` | `false` | 是否显示加载图标，同时设置 aria-busy。加载期间默认阻止重复操作。 |
| `isInterruptible` | `boolean` | `false` | 是否允许加载期间再次触发 clickAction。 |
| `clickAction` | `(event: MouseEvent) => void \| Promise<void>` | — | 点击时执行的回调；返回 Promise 时，组件会自动显示加载状态。 |
| `tooltip` | `string` | — | 悬停或键盘聚焦时显示的补充提示，不能替代 label。 |
| `href` | `string` | — | 链接地址。存在且未禁用时，根元素渲染为链接。 |
| `width` | `number \| string` | — | 按钮宽度。数字转换为像素，CSS 字符串按原样使用。 |

其他继承属性如下。完整说明见 [Button 属性](/components/button/props)。

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

## 插槽

- 默认插槽为必需项。
- 默认插槽必须提供一个表示操作的图标。
- `IconButton` 不提供可见文字。
- `IconButton` 不提供 `end` 插槽。

:::warning 无障碍名称
必须提供准确的 `label`。缺少 `label` 时，辅助技术无法确定按钮用途。

`tooltip` 提供补充说明。不得用 `tooltip` 代替 `label`。
:::

## 事件

`click` 事件的参数类型为 `MouseEvent`。

## 模板引用

`IconButton` 公开 `element`。
类型为 `HTMLButtonElement | HTMLAnchorElement | HTMLElement | null`。
链接模式下（有 `href` 且未禁用），它是链接元素：原生锚点，或 `as` 链接组件的根元素；其他情况下是按钮本身。

## 原生属性

以下内容传入根元素：

- HTML 属性。
- `aria-*` 属性。
- `data-*` 属性。
- `class`。
- `style`。
- 原生事件监听器。

## 无障碍

### 图标或转圈弧线

- 准则: 1.4.11 Non-text Contrast
- 要求: 3:1
- 适用状态: Rest, Hover, Pointer down, Loading

IconButton 没有可见文字。图标在静止、悬停、按下时都要和按钮底至少 3:1。加载时转动的那截弧线也要达到 3:1。

### 看得出的控件边界

- 准则: 1.4.11 Non-text Contrast
- 要求: 3:1 if needed
- 适用状态: Rest

只有使用者必须靠这条边才看得出这是控件时，边才需要 3:1。

### 键盘焦点指示

- 准则: 1.4.11 Non-text Contrast
- 要求: 3:1
- 适用状态: Focus visible

焦点轮廓和按钮周围至少 3:1。destructive 的红色轮廓也要量。

### 禁用外观

- 准则: 1.4.3 and 1.4.11 exceptions
- 要求: Not required
- 适用状态: Disabled

禁用的控件不用达到这些对比度。
