# 视觉隐藏

## 使用方法

从独立入口导入 `VisuallyHidden`。默认插槽为必需项：

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

<template>
  <a href="/articles/accessibility">
    了解更多
    <VisuallyHidden>关于无障碍基础组件</VisuallyHidden>
  </a>
</template>
```

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 用于为纯图标按钮和控件提供屏幕阅读器可播报的无障碍名称。 |
| 推荐 | 以块级元素（as="div"）渲染，并设置 aria-live，用于播报拖放或结果数量等动态更新。 |
| 避免 | 用它对所有人隐藏内容。内容仍保留在无障碍树中。如需完全移除，应使用条件渲染或 hidden。 |
| 避免 | 在其中放置交互控件。内容不可见，也无法接收指针输入。 |

## 示例

### 实时播报

任务被移到下一列时，aria-live 把新位置读出来。看见箭头的人不用听这段话。

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { VisuallyHidden } from '@astryx-vue/core/VisuallyHidden'
import { computed, shallowRef } from 'vue'

const columns = ['Backlog', 'In progress', 'Done'] as const
const column = shallowRef(0)
const current = computed(() => columns[column.value])

function move(): void {
  column.value = (column.value + 1) % columns.length
}
</script>

<template>
  <VStack :gap="4" h-align="start">
    <Text type="supporting" color="secondary">
      Drag-and-drop and other visual-only changes are silent to screen readers. A live region narrates them.
    </Text>
    <HStack :gap="3" align="center">
      <Button label="Move task" variant="secondary" @click="move" />
      <Text type="body">
        Task is in <Text as="span" weight="bold">
          {{ current }}
        </Text>
      </Text>
    </HStack>
    <VisuallyHidden as="div" aria-live="polite">
      Task moved to {{ current }}
    </VisuallyHidden>
  </VStack>
</template>
```

### 结构标题

这一组在视觉上已经成块，但没有可见标题。隐藏的 h2 让屏幕阅读器能跳到这里。

```vue
<script setup lang="ts">
import { Badge } from '@astryx-vue/core/Badge'
import { Card } from '@astryx-vue/core/Card'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { VisuallyHidden } from '@astryx-vue/core/VisuallyHidden'

const items = [
  { name: 'astryx-core', status: 'Passing', variant: 'success' },
  { name: 'astryx-charts', status: 'Failing', variant: 'error' },
  { name: 'astryx-cli', status: 'Passing', variant: 'success' },
] as const
</script>

<template>
  <VStack :gap="3" h-align="start">
    <Text type="supporting" color="secondary">
      The layout makes this group obvious to sighted users. A hidden heading gives screen-reader users the same landmark to jump to.
    </Text>
    <VisuallyHidden as="h2">
      Build status
    </VisuallyHidden>
    <VStack :gap="2">
      <Card v-for="item in items" :key="item.name" variant="muted" :padding="3">
        <HStack :gap="3" align="center">
          <Text type="body">
            {{ item.name }}
          </Text>
          <Badge :variant="item.variant">
            {{ item.status }}
          </Badge>
        </HStack>
      </Card>
    </VStack>
  </VStack>
</template>
```

### 补充上下文

箭头只是装饰。涨了还是跌了，写在 VisuallyHidden 里给屏幕阅读器。

```vue
<script setup lang="ts">
import { Card } from '@astryx-vue/core/Card'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { VisuallyHidden } from '@astryx-vue/core/VisuallyHidden'

const stats = [
  { label: 'Revenue', value: '$48.2k', delta: '+12%', direction: 'up' },
  { label: 'Churn', value: '2.1%', delta: '-4%', direction: 'down' },
] as const
</script>

<template>
  <HStack :gap="4" wrap="wrap">
    <Card v-for="item in stats" :key="item.label" variant="muted">
      <VStack :gap="1">
        <Text type="supporting" color="secondary">
          {{ item.label }}
        </Text>
        <Text type="display-3">
          {{ item.value }}
        </Text>
        <HStack :gap="1" align="center">
          <span
            class="css-icon css-icon--sm"
            :class="item.direction === 'up' ? 'i-carbon-arrow-up' : 'i-carbon-arrow-down'"
            aria-hidden="true"
          />
          <Text type="body">
            {{ item.delta }}
            <VisuallyHidden>
              {{ item.direction === 'up' ? ' increase' : ' decrease' }} from last month
            </VisuallyHidden>
          </Text>
        </HStack>
      </VStack>
    </Card>
  </HStack>
</template>
```

## Props

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `as` | `string \| Component` | `'span'` | 根 HTML 元素或 Vue 组件。实时区域或块级内容可使用 `div`。 |

## 插槽

| 名称 | 说明 |
| --- | --- |
| `default` | 对辅助技术公开、但从视觉界面隐藏的内容。此插槽为必需项。 |

## 模板引用

组件公开 `element`。类型为 `HTMLElement | null`。
`element` 指向最终渲染的根元素。

## 样式约束

- 组件忽略传入的 `class`。
- 组件忽略传入的 `style`。
- 固定的隐藏样式不接受外部覆盖。

## 原生属性

以下内容传入根元素：

- `aria-*` 属性。
- `role`。
- `id`。
- `data-*` 属性。
- 原生事件监听器。

:::warning 隐藏内容
不得在 `VisuallyHidden` 内放置交互控件。视觉界面无法显示这些控件。

必须对所有用户隐藏内容时，使用条件渲染或原生 `hidden` 属性。
:::
