# 弹出层

## 使用方法

`Popover` 在点击触发按钮时，在它旁边打开一个浮层。和只放纯文字的 Tooltip 不同，浮层可以承载简短的交互内容，例如一个小表单、一段补充说明或一组操作。触发元素放进默认插槽，浮层内容放进 `content` 插槽：

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

<template>
  <Popover label="项目详情">
    <Button label="详情" variant="secondary" />
    <template #content>
      <p>这里放置简短内容。</p>
    </template>
  </Popover>
</template>
```

### 结构

- **触发元素**: 使用方提供或从外部引用的控件，用来锚定并开关浮层。
- **浮层表面**: 由 Popover 拥有并套上主题的着色表面，使用组件约定的 popover 主题目标。
- **浮层内容**: 画在表面里、由使用方提供的内容。
- **后备关闭控件**: usePopover 启用时追加的关闭操作，键盘可以到达。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 弹出层内容只聚焦一项任务或一条信息。 |
| 推荐 | 提供明确的关闭方式：点击外部区域，或使用显式的关闭按钮。 |
| 避免 | 在弹出层中嵌套弹出层。这会造成混乱的焦点和导航。 |
| 避免 | 需要大量用户输入的内容，避免放在弹出层中。应使用 Dialog。 |
| 避免 | 在弹出层中放入过多内容。若需要滚动，应改用 Dialog。 |

## 示例

### 底部弹层

紧凑触屏上，项目操作改由底部弹层承接，而不是锚在按钮旁的弹出层。

```vue
<script setup lang="ts">
import type { Component } from 'vue'
import { BottomSheet } from '@astryx-vue/core/BottomSheet'
import { Button } from '@astryx-vue/core/Button'
import { Heading } from '@astryx-vue/core/Heading'
import { Icon } from '@astryx-vue/core/Icon'
import { List, ListItem } from '@astryx-vue/core/List'
import { Section } from '@astryx-vue/core/Section'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { h, ref } from 'vue'

function svgIcon(paths: string[]): Component {
  return () => h('svg', {
    'viewBox': '0 0 24 24',
    'fill': 'none',
    'stroke': 'currentColor',
    'stroke-width': 1.5,
    'stroke-linecap': 'round',
    'stroke-linejoin': 'round',
  }, paths.map(d => h('path', { d })))
}

const UserIcon = svgIcon(['M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2', 'M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8'])
const TagIcon = svgIcon(['M20.6 13.4 12 22l-8.6-8.6a2 2 0 0 1-.6-1.4V4a2 2 0 0 1 2-2h8a2 2 0 0 1 1.4.6L20.6 8a2 2 0 0 1 0 2.8Z', 'M7.5 7.5h.01'])
const CalendarIcon = svgIcon(['M8 2v4', 'M16 2v4', 'M3 10h18', 'M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2Z'])

const actions = [
  { icon: UserIcon, label: 'Assign owner', description: 'Route follow-up to a teammate.' },
  { icon: TagIcon, label: 'Add label', description: 'Group this item with related work.' },
  { icon: CalendarIcon, label: 'Set due date', description: 'Pick a reminder for review.' },
]

const isOpen = ref(false)
</script>

<template>
  <Button label="Open project actions" @click="isOpen = true" />
  <BottomSheet v-model:is-open="isOpen" label="Project actions" height="hug">
    <Section :padding-block="4" :padding-inline="1">
      <VStack :gap="3">
        <VStack :gap="1" style="margin-inline-start: var(--astryx-spacing-3, 12px)">
          <Heading :level="3">
            Project actions
          </Heading>
          <Text type="supporting" color="secondary">
            Use this modal touch surface when the task should move away from its trigger and stay close to the bottom edge.
          </Text>
        </VStack>
        <List density="spacious">
          <ListItem
            v-for="action in actions"
            :key="action.label"
            :label="action.label"
            :description="action.description"
            :on-click="() => { isOpen = false }"
          >
            <template #startContent>
              <Icon :icon="action.icon" size="md" color="secondary" />
            </template>
          </ListItem>
        </List>
      </VStack>
    </Section>
  </BottomSheet>
</template>
```

### 确认操作

破坏性按钮打开一段简短确认，里面是删除和取消。

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

const isOpen = ref(false)
</script>

<template>
  <Popover
    :is-open="isOpen"
    placement="below"
    label="Confirm deletion"
    :width="300"
    @open-change="isOpen = $event"
  >
    <Button label="Delete project" variant="destructive" />
    <template #content>
      <VStack :gap="3">
        <Heading :level="4">
          Delete project?
        </Heading>
        <Text type="body">
          This will permanently delete the project and all its data. This action cannot be undone.
        </Text>
        <HStack h-align="end" :gap="2">
          <Button label="Delete" variant="destructive" @click="isOpen = false" />
          <Button label="Cancel" variant="ghost" @click="isOpen = false" />
        </HStack>
      </VStack>
    </template>
  </Popover>
</template>
```

### 筛选面板

按状态勾选，并提供应用和重置。

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { CheckboxInput } from '@astryx-vue/core/CheckboxInput'
import { Divider } from '@astryx-vue/core/Divider'
import { Heading } from '@astryx-vue/core/Heading'
import { Popover } from '@astryx-vue/core/Popover'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { reactive, ref } from 'vue'

const isOpen = ref(false)
const filters = reactive({
  active: true,
  archived: false,
  drafts: true,
  shared: false,
})

function reset() {
  filters.active = true
  filters.archived = false
  filters.drafts = true
  filters.shared = false
}
</script>

<template>
  <Popover
    :is-open="isOpen"
    placement="below"
    label="Filter"
    :width="240"
    @open-change="isOpen = $event"
  >
    <Button label="Filter" />
    <template #content>
      <VStack :gap="3">
        <Heading :level="4">
          Filter by status
        </Heading>
        <Divider />
        <CheckboxInput v-model="filters.active" label="Active" />
        <CheckboxInput v-model="filters.archived" label="Archived" />
        <CheckboxInput v-model="filters.drafts" label="Drafts" />
        <CheckboxInput v-model="filters.shared" label="Shared with me" />
        <Divider />
        <HStack h-align="end" :gap="2">
          <Button label="Apply" variant="primary" @click="isOpen = false" />
          <Button label="Reset" variant="ghost" @click="reset" />
        </HStack>
      </VStack>
    </template>
  </Popover>
</template>
```

### 键盘快捷键

列出快捷键以及各自的作用。

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

const shortcuts = [
  { key: '⌘K', action: 'Command palette' },
  { key: '⌘/', action: 'Toggle sidebar' },
  { key: '⌘.', action: 'Quick actions' },
]
</script>

<template>
  <Popover placement="below" label="Keyboard shortcuts" :width="260">
    <Button label="Shortcuts" />
    <template #content>
      <VStack :gap="2">
        <Heading :level="4">
          Keyboard shortcuts
        </Heading>
        <Divider />
        <HStack v-for="shortcut in shortcuts" :key="shortcut.key" :gap="3">
          <Text type="body" weight="bold">
            {{ shortcut.key }}
          </Text>
          <Text type="body">
            {{ shortcut.action }}
          </Text>
        </HStack>
      </VStack>
    </template>
  </Popover>
</template>
```

### 设置面板

通知、深色模式和声音三个开关。

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Divider } from '@astryx-vue/core/Divider'
import { Heading } from '@astryx-vue/core/Heading'
import { Popover } from '@astryx-vue/core/Popover'
import { VStack } from '@astryx-vue/core/Stack'
import { Switch } from '@astryx-vue/core/Switch'
import { ref } from 'vue'

const notifications = ref(true)
const darkMode = ref(false)
const sounds = ref(true)
</script>

<template>
  <Popover placement="below" label="Settings" :width="280">
    <Button label="Settings" />
    <template #content>
      <VStack :gap="3">
        <Heading :level="4">
          Settings
        </Heading>
        <Divider />
        <Switch v-model="notifications" label="Notifications" description="Receive push notifications" />
        <Switch v-model="darkMode" label="Dark mode" description="Use dark color theme" />
        <Switch v-model="sounds" label="Sounds" description="Play sounds for actions" />
      </VStack>
    </template>
  </Popover>
</template>
```

## 无障碍

弹出层要把自己的存在、当前的开合状态和内部内容告诉使用屏幕阅读器或键盘的用户。组件会自动把触发元素和浮层关联起来，并在浮层打开、关闭时管理焦点，你只需要提供必要的无障碍名称。

- 触发元素上带有 `aria-haspopup`、`aria-expanded` 与 `aria-controls`，辅助技术由此知道这是一个会打开浮层的按钮，并能读出浮层当前是展开还是收起。
- `dialog` 类型的浮层带有 `role="dialog"`；设置 `isModal` 时还会带有 `aria-modal="true"`，并以 `label` 作为无障碍名称。
- 打开时焦点移入浮层中的第一个可聚焦元素；用户按下 Escape 或关闭浮层时，焦点回到触发元素。
- 设置 `isModal` 后，浮层打开期间 Tab 焦点保持在浮层内。
- 内置关闭按钮在获得焦点前视觉隐藏，键盘聚焦到它时才显示出来。

## 布局

浮层通过 JavaScript 定位。页面滚动或窗口尺寸变化时会重新计算位置；所选方向空间不足时翻转到另一侧。

## 属性

| 属性 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `placement` | `'above' \| 'below' \| 'start' \| 'end'` | `'below'` | 浮层相对触发元素的方向。所选方向空间不足时会自动翻转。 |
| `alignment` | `'start' \| 'center' \| 'end'` | `'start'` | 浮层沿位置方向的对齐方式：靠起始端、居中或靠末端。 |
| `isOpen` | `boolean` | — | 受控显示状态。未设置时，开合由触发元素自行管理。 |
| `isEnabled` | `boolean` | `true` | 是否启用。为 false 时触发元素无法打开浮层。 |
| `width` | `number \| string` | 触发元素宽度 | 浮层宽度，数字单位为像素。未设置时最小与触发元素等宽，最大为 320px，且不超出视口。 |
| `padding` | `0 \| 0.5 \| 1 \| 1.5 \| 2 \| 3 \| 4 \| 5 \| 6 \| 8 \| 10` | `3` | 浮层内边距，取值对应 `--astryx-spacing-*` 令牌。 |
| `label` | `string` | — | 浮层作为对话框时的无障碍名称，`role` 为 `dialog` 时应提供。 |
| `role` | `'dialog' \| 'none'` | `'dialog'` | 浮层的语义角色：包含交互内容时使用 `dialog`；内容自带角色（例如菜单）时使用 `none`。 |
| `isModal` | `boolean` | `true` | 浮层打开期间将 Tab 焦点限制在浮层内。仅对 `dialog` 角色生效。 |
| `hasCloseButton` | `boolean` | `true` | 是否显示关闭按钮。关闭按钮在获得焦点前保持视觉隐藏。 |
| `closeButtonLabel` | `string` | `'Close'` | 关闭按钮的文字，同时作为它的无障碍名称。 |
| `hasAutoFocus` | `boolean` | `true` | 打开时是否把焦点移入浮层。仅对 `dialog` 角色生效。 |
| `hasLightDismiss` | `boolean` | `true` | 是否允许点击浮层与触发元素以外的区域来关闭。 |
| `hasEscapeDismiss` | `boolean` | `true` | 是否允许按下 Escape 关闭浮层。 |
| `anchor` | `HTMLElement \| null` | — | 用外部元素当锚点。这时不渲染触发容器，默认插槽可以不写；点击、键盘和 ARIA 属性都挂到该元素里的按钮上。 |

## 事件

- `openChange(isOpen: boolean)`：浮层打开或关闭时触发，包括用户主动关闭。

## 插槽

- `default`：触发元素。请使用 `button` 或带有 `role="button"` 的元素。设置 `anchor` 后可以不写。
- `content`：浮层内容。

## 主题

```css
.my-popover {
  --astryx-popover-background: #fff;
  --astryx-popover-foreground: #000;
}
```

- `--astryx-popover-background`：浮层背景色。
- `--astryx-popover-foreground`：浮层文字颜色。
- `--astryx-popover-shadow`：浮层阴影。
- `--astryx-popover-z-index`：浮层的层叠顺序。
