# Split Button

> 在工具栏中分别执行当前值和展开其他选择，适用于颜色、样式等操作。

网页来源：/components/split-button

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

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

Split Button 把“执行当前值”和“选择其他值”放在两个相邻按钮中，适合文字颜色、边框样式等工具栏操作。`QuireToolbarSplitButton` 需要放在 `QuireToolbar` 内使用。

## 基础用法

左侧触发 `action`，执行当前选项；右侧切换 `v-model:expanded`，打开其他选项。设置 `overlay="menu"`，在 `overlay` 插槽中提供菜单条目。

### 示例：split-button/menu

文件：`menu.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import {
  QuireIcon,
  QuireIconProvider,
  QuireMenuItem,
  QuireToolbar,
  QuireToolbarSplitButton
} from "@quirekit/ui/vue";
const expanded = ref(false);
const format = ref("PDF");
const result = ref("尚未导出");
/** 保存格式并执行这一次导出。
 * @param next - 选中的格式。
 */
function choose(next: string): void {
  format.value = next;
  result.value = `已导出 ${next}`;
}
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="grid min-w-0 justify-items-start gap-4 p-qk-surface">
      <QuireToolbar label="文档导出">
        <QuireToolbarSplitButton
          v-model:expanded="expanded"
          label="导出文档"
          :action-label="`导出 ${format}`"
          disclosure-label="选择导出格式"
          overlay="menu"
          @action="choose(format)"
        >
          <QuireIcon name="fileText" />
          <template #overlay>
            <QuireMenuItem value="pdf" @select="choose('PDF')">
              PDF 文档
            </QuireMenuItem>
            <QuireMenuItem value="docx" @select="choose('DOCX')">
              Word 文档
            </QuireMenuItem>
          </template>
        </QuireToolbarSplitButton>
      </QuireToolbar>
      <output>{{ result }}</output>
    </div>
  </QuireIconProvider>
</template>
```

## Menu 与 Popover

菜单适合执行离散选项；`overlay="popover"` 适合调整参数，插槽放入完整的 `QuirePopoverContent`。下面先在浮层中调整打印份数，再点击主按钮执行。

### 示例：split-button/popover

文件：`popover.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import {
  QuireIcon,
  QuireIconProvider,
  QuireInputNumber,
  QuirePopoverContent,
  QuirePopoverTitle,
  QuireToolbar,
  QuireToolbarSplitButton
} from "@quirekit/ui/vue";
const expanded = ref(false);
const copies = ref("1");
const result = ref("尚未打印");
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="grid min-w-0 justify-items-start gap-4 p-qk-surface">
      <QuireToolbar label="文档打印">
        <QuireToolbarSplitButton
          v-model:expanded="expanded"
          label="打印"
          action-label="按当前份数打印"
          disclosure-label="调整打印份数"
          overlay="popover"
          @action="result = `已打印 ${copies} 份`"
        >
          <QuireIcon name="fileText" />
          <template #overlay>
            <QuirePopoverContent class="grid gap-3" style="width: 240px">
              <QuirePopoverTitle>打印份数</QuirePopoverTitle>
              <QuireInputNumber
                v-model="copies"
                label="打印份数"
                width="100%"
                :min="1"
                :max="10"
              />
            </QuirePopoverContent>
          </template>
        </QuireToolbarSplitButton>
      </QuireToolbar>
      <output>{{ result }}</output>
    </div>
  </QuireIconProvider>
</template>
```

## Color Picker

使用 `overlay="custom"` 和 `disclosure` 插槽组合 `QuireColorPicker`。`QuireToolbarColorPickerDisclosure` 让打开按钮参与工具栏键盘导航。用同一变量同步 Split Button 的 `expanded` 和 Color Picker 的 `open`。

`reset-active` 同步无颜色状态：主按钮继续执行清除，图标隐藏色条或填充，最近使用颜色独立保留。下面的源码包含完整状态和回调。

### 示例：split-button/playground

文件：`playground.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import {
  QuireColorPicker,
  QuireIcon,
  QuireIconProvider,
  QuireToolbar,
  QuireToolbarColorPickerDisclosure,
  QuireToolbarSplitButton
} from "@quirekit/ui/vue";

const textColor = ref("#3b6df6");
const fillColor = ref("#facc15");
const textReset = ref(false);
const fillReset = ref(false);
const textOpen = ref(false);
const fillOpen = ref(false);
const recentTextColors = ref<readonly string[]>(["#3B6DF6", "#E5484D", "#12A594"]);
const recentFillColors = ref<readonly string[]>(["#FACC15", "#F59E0B", "#65A30D"]);
const lastAction = ref("尚未执行命令");

/** Applies the visible text color or its explicit no-color state. */
function applyTextColor(): void {
  lastAction.value = textReset.value ? "应用无文字颜色" : `应用文字颜色 ${textColor.value}`;
}

/** @param color - Committed text color promoted to the recent-color list. */
function commitTextColor(color: string): void {
  textColor.value = color;
  recentTextColors.value = Object.freeze([
    color,
    ...recentTextColors.value.filter((recent) => recent !== color)
  ].slice(0, 10));
  lastAction.value = `选择文字颜色 ${color}`;
}

/** Applies the currently visible fill-color swatch. */
function applyFillColor(): void {
  lastAction.value = fillReset.value ? "应用无填充颜色" : `应用填充颜色 ${fillColor.value}`;
}

/** @param color - Committed fill color promoted to the recent-color list. */
function commitFillColor(color: string): void {
  fillColor.value = color;
  recentFillColors.value = Object.freeze([
    color,
    ...recentFillColors.value.filter((recent) => recent !== color)
  ].slice(0, 10));
  lastAction.value = `选择填充颜色 ${color}`;
}
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="grid min-w-0 justify-items-start p-qk-surface" data-split-button-playground>
      <div class="grid min-w-0 max-w-full gap-4">
        <QuireToolbar label="Split Button 示例" class="max-w-full p-qk-inline-tight">
          <QuireToolbarSplitButton
            v-model:expanded="textOpen"
            label="文字颜色"
            :action-label="textReset ? '应用无文字颜色' : '应用当前文字颜色'"
            disclosure-label="选择文字颜色"
            overlay="custom"
            @action="applyTextColor"
          >
            <QuireIcon name="textColor" :accent-color="textColor" :empty="textReset" />
            <template #disclosure>
              <QuireColorPicker
                v-model="textColor"
                v-model:reset-active="textReset"
                v-model:open="textOpen"
                label="选择文字颜色"
                :recent-colors="recentTextColors"
                @commit="commitTextColor"
                @reset="lastAction = '已清除文字颜色'"
              >
                <template #trigger>
                  <QuireToolbarColorPickerDisclosure
                    class="qk-toolbar__split-disclosure"
                    label="选择文字颜色"
                  >
                    <QuireIcon name="chevronDown" :size="16" />
                  </QuireToolbarColorPickerDisclosure>
                </template>
              </QuireColorPicker>
            </template>
          </QuireToolbarSplitButton>

          <QuireToolbarSplitButton
            v-model:expanded="fillOpen"
            label="单元格填充"
            :action-label="fillReset ? '应用无填充颜色' : '应用当前填充色'"
            disclosure-label="选择填充色"
            overlay="custom"
            @action="applyFillColor"
          >
            <QuireIcon name="tableCellFill" :accent-color="fillColor" :empty="fillReset" />
            <template #disclosure>
              <QuireColorPicker
                v-model="fillColor"
                v-model:reset-active="fillReset"
                v-model:open="fillOpen"
                label="选择填充色"
                :recent-colors="recentFillColors"
                @commit="commitFillColor"
                @reset="lastAction = '已清除填充颜色'"
              >
                <template #trigger>
                  <QuireToolbarColorPickerDisclosure
                    class="qk-toolbar__split-disclosure"
                    label="选择填充色"
                  >
                    <QuireIcon name="chevronDown" :size="16" />
                  </QuireToolbarColorPickerDisclosure>
                </template>
              </QuireColorPicker>
            </template>
          </QuireToolbarSplitButton>
        </QuireToolbar>

        <p class="m-0 text-qk-helper text-qk-text-tertiary" aria-live="polite">
          {{ lastAction }}
        </p>
      </div>
    </div>
  </QuireIconProvider>
</template>
```

## 自定义浮层

`overlay="custom"` 不创建内置浮层。通过 `disclosure` 插槽组合已有的选择器或其他浮层组件，并保持展开按钮与浮层的状态同步。

## 主按钮状态与提示

主按钮执行开关命令时传 `pressed`，支持 `true`、`false` 和 `"mixed"`；普通动作省略该属性。
该状态由调用方控制，点击仍只发出一次 `action`，不会改变右侧展开状态。配置式 Toolbar 的
`kind: "split"` 同样支持 `pressed`。

可选 `actionTooltip` 插槽放入 `QuireTooltipContent`。提示锚定主按钮、展开选择器时关闭；
独立主题由 [QuireThemeProvider](/raw/components/theme-provider.md) 传递；
跨 document 场景在 Content 配置 `portal-target`，目标文档需安装组件 CSS。

## 禁用与名称

- `disabled` 同时禁用两侧按钮，`actionDisabled` 和 `disclosureDisabled` 分别禁用一侧。组合 `QuireToolbarColorPickerDisclosure` 时会自动继承所在 Split Button 和 Color Picker 的禁用状态。
- `label` 命名整组，`actionLabel` 与 `disclosureLabel` 分别命名两个按钮。
- `actionDescribedBy` 与 `disclosureDescribedBy` 可关联说明或禁用原因。
- `popup` 为自定义模式声明浮层类型，`expanded` 表示右侧浮层是否打开。

### 示例：split-button/states

文件：`states.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { quireIconRegistry } from "@quirekit/ui";
import {
  QuireIcon,
  QuireIconProvider,
  QuireMenuItem,
  QuireSwitch,
  QuireToolbar,
  QuireToolbarSplitButton
} from "@quirekit/ui/vue";
const actionDisabled = ref(true);
const disclosureDisabled = ref(false);
const result = ref("尚未应用");
</script>

<template>
  <QuireIconProvider :registry="quireIconRegistry">
    <div class="grid min-w-0 justify-items-start gap-4 p-qk-surface">
      <div class="flex flex-wrap gap-4">
        <QuireSwitch v-model:checked="actionDisabled" label="禁用主操作" />
        <QuireSwitch v-model:checked="disclosureDisabled" label="禁用展开" />
      </div>
      <QuireToolbar label="独立禁用示例">
        <QuireToolbarSplitButton
          label="样式操作"
          action-label="应用已选样式"
          disclosure-label="浏览可用样式"
          overlay="menu"
          :action-disabled="actionDisabled"
          :disclosure-disabled="disclosureDisabled"
          @action="result = '已应用正文样式'"
        >
          <QuireIcon name="fileText" />
          <template #overlay>
            <QuireMenuItem value="body" @select="result = '已选择正文样式'">
              正文样式
            </QuireMenuItem>
          </template>
        </QuireToolbarSplitButton>
      </QuireToolbar>
      <output>{{ result }}</output>
    </div>
  </QuireIconProvider>
</template>
```

## 键盘操作

两个按钮都参与所在工具栏的方向键导航。展开浮层后，在浮层内使用键盘；关闭后焦点返回右侧展开按钮。

## API

### QuireToolbarSplitButton

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| label | 是 | — | 整组按钮的可访问名称。 |
| actionLabel | 是 | — | 主按钮的可访问名称。 |
| disclosureLabel | 是 | — | 展开按钮的可访问名称。 |
| actionTooltip | 否 | undefined | Primary Tooltip explanation; defaults to actionLabel. |
| disclosureTooltip | 否 | undefined | Disclosure Tooltip explanation; defaults to disclosureLabel. |
| disabledReason | 否 | — | Explanation shown while either target is unavailable. |
| text | 否 | — | Visible primary label beside the icon; omitted for an icon-only square target. |
| pressed | 否 | undefined | Optional primary toggle state; omission preserves ordinary action semantics. |
| popup | 否 | "menu" | 右侧按钮展开的浮层类型。 |
| overlay | 否 | "custom" | 右侧按钮组合的浮层类型。 |
| controls | 否 | — | DOM id of the controlled popup when available. |
| type | 否 | "button" | 主按钮的原生表单提交类型。 |
| disabled | 否 | false | 是否禁用两侧按钮。 |
| actionDisabled | 否 | false | 是否只禁用主按钮。 |
| disclosureDisabled | 否 | false | 是否只禁用展开按钮。 |
| actionDescribedBy | 否 | — | 关联主按钮说明或禁用原因的元素 id。 |
| disclosureDescribedBy | 否 | — | 关联展开按钮说明或禁用原因的元素 id。 |
| expanded | 否 | false |  |

**label**

```ts
string
```

**actionLabel**

```ts
string
```

**disclosureLabel**

```ts
string
```

**actionTooltip**

```ts
string | false | undefined
```

```ts
undefined | string | false
```

**disclosureTooltip**

```ts
string | false | undefined
```

```ts
undefined | string | false
```

**disabledReason**

```ts
string | undefined
```

```ts
undefined | string
```

**text**

```ts
string | undefined
```

```ts
undefined | string
```

**pressed**

```ts
QuireKitToolbarToggleState | undefined
```

```ts
undefined | false | true | "mixed"
```

**popup**

```ts
QuireKitToolbarPopup | undefined
```

```ts
undefined | "dialog" | "grid" | "listbox" | "menu" | "tree"
```

**overlay**

```ts
QuireKitToolbarSplitOverlay | undefined
```

```ts
undefined | "custom" | "menu" | "popover"
```

**controls**

```ts
string | undefined
```

```ts
undefined | string
```

**type**

```ts
"submit" | "reset" | "button" | undefined
```

```ts
undefined | "submit" | "reset" | "button"
```

**disabled**

```ts
boolean | undefined
```

**actionDisabled**

```ts
boolean | undefined
```

**disclosureDisabled**

```ts
boolean | undefined
```

**actionDescribedBy**

```ts
string | undefined
```

```ts
undefined | string
```

**disclosureDescribedBy**

```ts
string | undefined
```

```ts
undefined | string
```

**expanded**

```ts
boolean | undefined
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| action | 否 | — |  |
| update\:expanded | 否 | — |  |
| disclosure | 否 | — |  |

**action**

```ts
[event: MouseEvent]
```

**update:expanded**

```ts
[value: boolean]
```

**disclosure**

```ts
[expanded: boolean]
```

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| default | 否 | — | 主按钮内容，通常为图标或带文字的图标。 |
| actionTooltip | 否 | — | Optional QuireTooltipContent for the primary target, including host portal/theme attributes. |
| disclosure | 否 | — | 自定义展开按钮，例如用于组合颜色选择器的 QuireToolbarColorPickerDisclosure。 |
| indicator | 否 | — | 自定义展开图标。 |
| overlay | 否 | — | 菜单条目或完整的 QuirePopoverContent。 |

**default**

```ts
any
```

**actionTooltip**

```ts
any
```

**disclosure**

```ts
any
```

**indicator**

```ts
any
```

**overlay**

```ts
any
```

#### 公开实例成员

无。
