# ActionSheet

> 从底部展示相关操作，提供分组、危险动作、取消与异步反馈。

网页来源：/components/action-sheet

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

ActionSheet 从底部展示与当前对象相关的操作，适合移动端的分享、复制、移动等任务。选择字体、样式或多个资源时，使用 Select 或 Listbox。

## 基础动作

通过 `actions` 提供动作，使用 `@select` 接收所选动作的 `value`。动作默认执行后关闭面板，焦点返回打开面板的按钮。每个动作的 `value` 必须唯一。

### 示例：action-sheet/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import type { QuireKitActionSheetAction } from "@quirekit/ui";
import { QuireActionSheet, QuireButton, QuireIconProvider } from "@quirekit/ui/vue";

const actions: readonly QuireKitActionSheetAction[] = [
  { value: "copy-link", label: "复制链接", description: "获取当前文档的访问地址", icon: "link" },
  { value: "copy", label: "另存副本", description: "保留原文档，创建可独立编辑的副本", icon: "copy" },
  { value: "details", label: "查看详细信息", icon: "fileText" }
];
const result = ref("尚未选择动作");
const selections = ref(0);

/**
 * Records one accepted local action without touching the clipboard or a document service.
 *
 * @param value - Stable action selected by the shared component.
 */
function selectAction(value: string): void {
  const action = actions.find(action => action.value === value);
  if (!action) return;
  result.value = action.label;
  selections.value += 1;
}
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="grid min-h-48 justify-items-start content-center gap-3 p-qk-surface">
      <QuireActionSheet title="文档操作" description="为《季度设计方案》选择一项操作。" :actions="actions" @select="selectAction">
        <template #trigger><QuireButton color="primary" variant="solid">打开文档操作</QuireButton></template>
      </QuireActionSheet>
      <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">最近动作：{{ result }} · 选择 {{ selections }} 次</p>
    </div>
  </QuireIconProvider>
</template>
```

## 分组、禁用与危险动作

`group` 把相邻的同名动作归为一组，并保留传入顺序。使用 `disabled` 禁用动作，用 `description` 解释原因。`danger` 突出危险动作；需要确认时，在动作回调中打开确认对话框。

使用 `icon` 指定图标，或通过 `icon` 插槽自定义图标内容。

### 示例：action-sheet/groups

文件：`groups.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import type { QuireKitActionSheetAction } from "@quirekit/ui";
import { QuireActionSheet, QuireButton, QuireIcon, QuireIconProvider } from "@quirekit/ui/vue";

const actions: readonly QuireKitActionSheetAction[] = [
  { value: "review", label: "标记待审", icon: "check", group: "协作" },
  { value: "versions", label: "查看版本记录", icon: "fileText", group: "协作" },
  { value: "move", label: "移动到团队目录", description: "需要团队编辑权限", icon: "copy", disabled: true, group: "文档管理" },
  { value: "trash", label: "移到回收站", description: "可在回收站恢复", icon: "trash", danger: true, group: "文档管理" }
];
const result = ref("尚未选择");

/**
 * Keeps disabled permission handling inside ActionSheet and records accepted actions only.
 *
 * @param value - Accepted action identity.
 */
function selectAction(value: string): void {
  result.value = actions.find(action => action.value === value)?.label ?? result.value;
}
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="grid min-h-48 justify-items-start content-center gap-3 p-qk-surface">
      <QuireActionSheet title="项目文件操作" description="查看协作记录或管理当前文档。" :actions="actions" @select="selectAction">
        <template #trigger><QuireButton variant="outline">打开分组操作</QuireButton></template>
        <template #icon="{ action }"><QuireIcon v-if="action.icon" :name="action.icon" :size="20" /></template>
      </QuireActionSheet>
      <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">已选择：{{ result }}</p>
    </div>
  </QuireIconProvider>
</template>
```

## 打开状态与关闭保护

`v-model:open` 控制打开状态，`defaultOpen` 设置初始状态。取消、Escape 和外部点击只关闭面板，不触发 `select`。

需要保护未完成任务时，绑定 `@close-request`，根据 `reason` 判断关闭来源，并同步调用 `preventDefault()` 拒绝关闭。直接修改受控 `open` 可以关闭面板，不受此回调限制。

### 示例：action-sheet/controlled

文件：`controlled.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import type { QuireKitActionSheetAction, QuireKitDrawerCloseRequest } from "@quirekit/ui";
import { QuireActionSheet, QuireButton, QuireIconProvider, QuireSwitch } from "@quirekit/ui/vue";

const open = ref(false);
const empty = ref(false);
const result = ref("尚未选择");
const lastCloseReason = ref("无");
const actions = computed<readonly QuireKitActionSheetAction[]>(() => empty.value ? [] : [
  { value: "viewed", label: "标记已查看", description: "记录动作后保留面板" },
  { value: "finish", label: "完成并关闭", description: "完成当前操作并关闭面板" }
]);

/**
 * Applies the host's explicit selection and completion policy.
 *
 * @param value - Action requested by the user.
 */
function selectAction(value: string): void {
  result.value = value === "finish" ? "完成并关闭" : "标记已查看";
  if (value === "finish") open.value = false;
}

/**
 * Shows the shared close-request reason without creating a second dismissal path.
 *
 * @param request - The component's cancellable closing intent.
 */
function recordCloseRequest(request: QuireKitDrawerCloseRequest): void {
  lastCloseReason.value = request.reason;
}
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="grid min-h-60 justify-items-start content-center gap-3 p-qk-surface">
      <QuireSwitch v-model:checked="empty" label="显示空操作列表" />
      <QuireButton variant="outline" @click="open = true">打开操作面板</QuireButton>
      <QuireActionSheet
        v-model:open="open" title="受控文档操作" description="可以连续标记文件，完成后关闭面板。"
        :actions="actions" :close-on-select="false"
        @select="selectAction" @close-request="recordCloseRequest"
      >
        <template #trigger><QuireButton>打开受控操作</QuireButton></template>
      </QuireActionSheet>
      <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">受控状态：{{ open ? "打开" : "关闭" }} · 最近动作：{{ result }} · 关闭请求：{{ lastCloseReason }}</p>
    </div>
  </QuireIconProvider>
</template>
```

## 异步操作

异步任务设置 `:close-on-select="false"`，开始处理时设置 `busy`，成功后关闭面板。`busy` 期间保留内容和焦点，禁止重复执行与用户关闭。失败后清除 `busy`，通过 `error` 显示反馈，用户可以继续重试。

请求取消和过期结果处理由调用方负责。

### 示例：action-sheet/async

文件：`async.vue`

```vue
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import type { QuireKitActionSheetAction, QuireKitDrawerCloseRequest } from "@quirekit/ui";
import { QuireActionSheet, QuireButton, QuireField, QuireIconProvider, QuireInput } from "@quirekit/ui/vue";

const open = ref(false);
const draftName = ref("季度方案副本");
const savedName = ref("");
const busy = ref(false);
const error = ref("");
const attempts = ref(0);
let generation = 0;
let pending: ReturnType<typeof setTimeout> | undefined;
const description = computed(() => busy.value
  ? "正在保存副本，处理完成前不能关闭。"
  : "首次保存显示失败反馈，重试后保存成功。");
const actions = computed<readonly QuireKitActionSheetAction[]>(() => [{
  value: "save", label: error.value ? "重试保存副本" : "保存副本", icon: "save",
  description: draftName.value.trim() ? `副本名称：${draftName.value.trim()}` : "请关闭面板后填写副本名称",
  disabled: draftName.value.trim().length === 0
}]);

/**
 * Captures one request and rejects repeated activation while it is pending.
 *
 * @param value - Accepted action identity from ActionSheet.
 */
function startSave(value: string): void {
  if (value !== "save" || busy.value || !open.value || !draftName.value.trim()) return;
  const request = ++generation;
  const name = draftName.value.trim();
  const attempt = ++attempts.value;
  error.value = "";
  busy.value = true;
  // The deterministic local delay stands in for a host service; no document or network is changed.
  pending = setTimeout(() => finishSave(request, name, attempt), 900);
}

/**
 * Accepts only the still-current request before publishing a result or closing the task.
 *
 * @param request - Generation captured when this attempt began.
 * @param name - Draft name captured by this attempt.
 * @param attempt - Local attempt number; the first one deliberately fails.
 */
function finishSave(request: number, name: string, attempt: number): void {
  if (request !== generation || !open.value || !busy.value) return;
  generation += 1;
  pending = undefined;
  busy.value = false;
  if (attempt === 1) {
    error.value = "暂时无法保存，名称已保留，请重试。";
    return;
  }
  savedName.value = name;
  open.value = false;
}

/**
 * Explains the host's pending-operation policy through the shared close request.
 *
 * @param request - Cancellable close request from any supported close entry.
 */
function guardClose(request: QuireKitDrawerCloseRequest): void {
  if (busy.value) request.preventDefault();
}

/** Invalidates pending work before a closed or unmounted task can receive a late result. */
function invalidateRequest(): void {
  generation += 1;
  if (pending !== undefined) clearTimeout(pending);
  pending = undefined;
  busy.value = false;
}

/**
 * Cleans up externally closed sessions as well as ordinary user dismissals.
 *
 * @param nextOpen - Current host-owned visibility.
 */
function observeOpen(nextOpen: boolean): void {
  if (!nextOpen) invalidateRequest();
}

watch(open, observeOpen, { flush: "sync" });
onBeforeUnmount(invalidateRequest);
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="mx-auto grid min-h-60 w-full max-w-md justify-items-start content-center gap-3 p-qk-surface">
      <QuireField label="副本名称" width="100%">
        <template #default="{ controlProps }"><QuireInput v-bind="controlProps" v-model="draftName" width="100%" /></template>
      </QuireField>
      <QuireActionSheet
        v-model:open="open" title="保存文档副本" :description="description" :actions="actions"
        :busy="busy" :error="error" :close-on-select="false" @select="startSave" @close-request="guardClose"
      >
        <template #trigger><QuireButton color="primary" variant="solid">打开保存操作</QuireButton></template>
      </QuireActionSheet>
      <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">已保存副本：{{ savedName || "尚未保存" }} · 请求 {{ attempts }} 次</p>
    </div>
  </QuireIconProvider>
</template>
```

## 长内容与空状态

动作标签和说明允许换行，内容超出可用高度时独立滚动，取消按钮保持可见。空集合显示空状态说明与取消按钮，界面文案跟随 [Locale Provider](/raw/components/locale-provider.md) 的语言。

### 示例：action-sheet/long-content

文件：`long-content.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import type { QuireKitActionSheetAction } from "@quirekit/ui";
import { QuireActionSheet, QuireButton, QuireIconProvider } from "@quirekit/ui/vue";

const actions: readonly QuireKitActionSheetAction[] = Array.from({ length: 18 }, (_, index) => ({
  value: `folder-${index + 1}`,
  label: `归档到跨部门协作资料与归档记录目录 ${index + 1}`,
  description: index === 0
    ? "https://example.invalid/shared/quarterly-design-review-and-cross-team-reference-archive-with-a-long-location-name"
    : "归档时保留文档名称和当前版本，便于团队成员稍后查找与核对。",
  group: index < 9 ? "团队工作目录" : "个人工作目录"
}));
const result = ref("尚未选择归档目标");

/**
 * Records a destination action after the independently scrolling list reaches it.
 *
 * @param value - Stable identity of the chosen destination.
 */
function selectAction(value: string): void {
  const action = actions.find(action => action.value === value);
  if (action) result.value = action.label;
}
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="grid min-h-48 justify-items-start content-center gap-3 p-qk-surface">
      <QuireActionSheet title="选择归档位置" description="选择归档目录，向下滚动查看更多位置。" :actions="actions" @select="selectAction">
        <template #trigger><QuireButton variant="outline">打开长列表操作</QuireButton></template>
      </QuireActionSheet>
      <p class="m-0 min-w-0 break-words text-qk-helper text-qk-text-secondary" role="status">归档目标：{{ result }}</p>
    </div>
  </QuireIconProvider>
</template>
```

## 拖动与键盘

- 使用把手拖动面板；正文用于滚动、选择文本和输入。
- 设置 `busy` 或提供 `onCloseRequest` 时，拖动关闭被禁用，把手隐藏。关闭策略应在手势开始前设置，不能在拖动途中新增保护来回滚当前手势。
- 使用 Tab 切换动作，Enter 或 Space 执行；Escape 关闭面板，关闭保护仍然生效。
- 需要同源动作在桌面菜单和底部面板间自适应时，使用 [ActionMenu](/raw/components/action-menu.md)。

## API

### QuireActionSheet

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| title | 是 | — | 面板的可见标题与可访问名称。 |
| actions | 是 | — | 按显示顺序排列的动作，每项 value 必须唯一。 |
| description | 否 | — | 标题下方的补充说明。 |
| open | 否 | undefined | 受控打开状态；省略时由组件管理。 |
| defaultOpen | 否 | false | 非受控模式的初始打开状态。 |
| closeOnSelect | 否 | true | 选择动作后是否关闭；异步任务设为 false。 |
| busy | 否 | false | 是否正在处理；期间禁止重复执行与用户关闭。 |
| error | 否 | — | 操作失败的提示，显示后可以继续重试。 |
| finalFocusEl | 否 | — | 关闭后接收焦点的元素。 |
| onCloseRequest | 否 | — | 关闭请求回调；同步调用 preventDefault() 可拒绝关闭，提供此回调时禁用拖动关闭。 |

**title**

```ts
string
```

**actions**

```ts
readonly QuireKitActionSheetAction[]
```

```ts
readonly ({
  /** 动作执行时返回的唯一值。 */
  value: string;
  /** 动作的可见名称与可访问名称。 */
  label: string;
  /** 动作说明，可用于解释禁用原因。 */
  description?: undefined | string;
  /** 由 QuireIconProvider 提供的图标名。 */
  icon?: string | undefined;
  /** 是否禁用动作。 */
  disabled?: boolean | undefined;
  /** 是否以危险色突出动作。 */
  danger?: boolean | undefined;
  /** 相邻同组动作的分组标题。 */
  group?: string | undefined;
})[]
```

**description**

```ts
string | undefined
```

```ts
undefined | string
```

**open**

```ts
boolean | undefined
```

**defaultOpen**

```ts
boolean | undefined
```

**closeOnSelect**

```ts
boolean | undefined
```

**busy**

```ts
boolean | undefined
```

**error**

```ts
string | undefined
```

```ts
undefined | string
```

**finalFocusEl**

```ts
(() => HTMLElement | null) | undefined
```

```ts
undefined | { (): HTMLElement | null; }
```

**onCloseRequest**

```ts
((request: QuireKitTaskCloseRequest) => void) | undefined
```

```ts
undefined | { (request: QuireKitTaskCloseRequest): void; }
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| update\:open | 否 | — |  |
| openChange | 否 | — |  |
| select | 否 | — |  |

**update:open**

```ts
[open: boolean]
```

**openChange**

```ts
[open: boolean]
```

**select**

```ts
[value: string]
```

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| trigger | 否 | — | 打开面板的单个按钮；通过程序打开时可以省略。 |
| icon | 否 | — | 动作的装饰图标，不放置独立交互控件。 |

**trigger**

```ts
any
```

**icon**

```ts
QuireKitActionSheetIconSlotProps
```

```ts
{
  /** 当前动作。 */
  action: {
    /** 动作执行时返回的唯一值。 */
    value: string;
    /** 动作的可见名称与可访问名称。 */
    label: string;
    /** 动作说明，可用于解释禁用原因。 */
    description?: undefined | string;
    /** 由 QuireIconProvider 提供的图标名。 */
    icon?: string | undefined;
    /** 是否禁用动作。 */
    disabled?: boolean | undefined;
    /** 是否以危险色突出动作。 */
    danger?: boolean | undefined;
    /** 相邻同组动作的分组标题。 */
    group?: string | undefined;
  };
}
```

#### 公开实例成员

无。

#### 相关类型

```ts
type QuireKitActionSheetAction = {
  /** 动作执行时返回的唯一值。 */
  value: string;
  /** 动作的可见名称与可访问名称。 */
  label: string;
  /** 动作说明，可用于解释禁用原因。 */
  description?: undefined | string;
  /** 由 QuireIconProvider 提供的图标名。 */
  icon?: string | undefined;
  /** 是否禁用动作。 */
  disabled?: boolean | undefined;
  /** 是否以危险色突出动作。 */
  danger?: boolean | undefined;
  /** 相邻同组动作的分组标题。 */
  group?: string | undefined;
};
```

```ts
type QuireKitActionSheetIconSlotProps = {
  /** 当前动作。 */
  action: {
    /** 动作执行时返回的唯一值。 */
    value: string;
    /** 动作的可见名称与可访问名称。 */
    label: string;
    /** 动作说明，可用于解释禁用原因。 */
    description?: string | undefined;
    /** 由 QuireIconProvider 提供的图标名。 */
    icon?: string | undefined;
    /** 是否禁用动作。 */
    disabled?: boolean | undefined;
    /** 是否以危险色突出动作。 */
    danger?: boolean | undefined;
    /** 相邻同组动作的分组标题。 */
    group?: string | undefined;
  };
};
```
