Collapsible
显示和隐藏一个面板,保留它的内容,并为它的高度添加动画。
用 Collapsible 显示和隐藏一个内容面板。要处理一组面板时,用 Accordion。
<script setup lang="ts">
import { ChevronDownIcon } from "@lucide/vue"
import {
Collapsible,
CollapsiblePanel,
CollapsibleTrigger,
} from "@ui-sdk/vue/collapsible"
const items = [
{ name: "Linen shirt", price: "$48.00" },
{ name: "Canvas tote", price: "$18.00" },
{ name: "Wool socks", price: "$18.00" },
]
</script>
<template>
<Collapsible data-variant="card">
<CollapsibleTrigger>
<span data-slot="stack">
<span>Order #4189</span>
<span data-slot="hint">3 items, $84.00</span>
</span>
<ChevronDownIcon />
</CollapsibleTrigger>
<CollapsiblePanel>
<div v-for="item in items" :key="item.name" data-slot="row">
<span>{{ item.name }}</span>
<span>{{ item.price }}</span>
</div>
</CollapsiblePanel>
</Collapsible>
</template>安装
npm install @ui-sdk/vue @ui-sdk/core组合
用下面的组合来构建 Collapsible:
Collapsible
├── CollapsibleTrigger
└── CollapsiblePanel根部件和面板渲染成 div,触发器渲染成原生 button。在它们周围放其他内容,比如一个标题,或一段始终显示的摘要。
用法
导入各个部件并把它们组合起来:
<script setup lang="ts">
import {
Collapsible,
CollapsiblePanel,
CollapsibleTrigger,
} from "@ui-sdk/vue/collapsible"
</script>
<template>
<Collapsible>
<CollapsibleTrigger>Order details</CollapsibleTrigger>
<CollapsiblePanel>2 × Linen shirt</CollapsiblePanel>
</Collapsible>
</template>自己构建
@ui-sdk/vue/collapsible 自带一个现成的 Collapsible。如果你想用自己的名字构建一个,可以从 @ui-sdk/core 里的 collapsible 原语(primitive)开始:
import { collapsible } from "@ui-sdk/core/collapsible"
import { createParts } from "@ui-sdk/vue/primitive"
const {
Root: Spoiler,
Trigger: SpoilerToggle,
Panel: SpoilerContent,
} = createParts(collapsible, {
root: "div",
trigger: "button",
panel: "div",
})
export { Spoiler, SpoilerContent, SpoilerToggle }<template>
<Spoiler>
<SpoilerToggle>Show the ending</SpoilerToggle>
<SpoilerContent>The butler did it.</SpoilerContent>
</Spoiler>
</template>每个部件都保留其现成对应部件的行为、状态和数据属性。更多内容见渲染原语和自定义部件。
禁用
在根部件上设置 disabled,阻止它的触发器打开或收起面板。触发器仍留在 Tab 顺序里,并报告 aria-disabled="true"。用禁用的 <fieldset> 包住 collapsible,也会把它禁用。
<Collapsible disabled>
<CollapsibleTrigger>Gift options</CollapsibleTrigger>
<CollapsiblePanel>Add a gift message and wrapping.</CollapsiblePanel>
</Collapsible>保持挂载
面板收起时会卸载,所以 Tab 会跳过它们的内容。在面板上设置 keep-mounted,让它保持挂载并隐藏,这样输入框里写的内容会保留下来。
<script setup lang="ts">
import { ChevronDownIcon } from "@lucide/vue"
import {
Collapsible,
CollapsiblePanel,
CollapsibleTrigger,
} from "@ui-sdk/vue/collapsible"
import { Field, FieldLabel } from "@ui-sdk/vue/field"
import { Textarea } from "@ui-sdk/vue/textarea"
</script>
<template>
<Collapsible data-variant="card">
<CollapsibleTrigger>
<span data-slot="stack">
<span>Add a delivery note</span>
<span data-slot="hint">Optional, for the courier</span>
</span>
<ChevronDownIcon />
</CollapsibleTrigger>
<CollapsiblePanel keep-mounted>
<Field>
<FieldLabel>Delivery note</FieldLabel>
<Textarea placeholder="Leave it with the front desk…" />
</Field>
</CollapsiblePanel>
</Collapsible>
</template>在页面中查找
在面板上设置 hidden-until-found,让它以 hidden="until-found" 留在 DOM 里。浏览器的查找命令,以及指向面板内内容的链接,都能把它露出来。面板打开时没有动画,所以匹配的内容会立即出现。
Vue 在服务器上渲染出 hidden="until-found",所以收起的面板在水合之前就能被找到。
<script setup lang="ts">
import { ChevronDownIcon } from "@lucide/vue"
import {
Collapsible,
CollapsiblePanel,
CollapsibleTrigger,
} from "@ui-sdk/vue/collapsible"
</script>
<template>
<Collapsible data-variant="card">
<CollapsibleTrigger>
<span data-slot="stack">
<span>Ingredients</span>
<span data-slot="hint">Morning buns, makes 12</span>
</span>
<ChevronDownIcon />
</CollapsibleTrigger>
<CollapsiblePanel hidden-until-found>
<p>Flour, butter, brown sugar, eggs, a pinch of cardamom and sea salt.</p>
</CollapsiblePanel>
</Collapsible>
</template>受控
用 v-model:open 自己管理展开状态。@open-change 还会收到变更详情:在它里面调用 details.cancel() 取消这次变更。
页面其他位置要放一个按钮时,用 createCollapsibleController() 创建一个控制器,并作为 controller 传入。每个挂载的 collapsible 各创建一个控制器。
<script setup lang="ts">
import { ChevronDownIcon } from "@lucide/vue"
import type { ChangeDetails } from "@ui-sdk/core"
import {
Collapsible,
CollapsiblePanel,
CollapsibleTrigger,
createCollapsibleController,
} from "@ui-sdk/vue/collapsible"
import { ref } from "vue"
import { useLog } from "./log"
const recoveryCodes = ["7KQ2-M9XD", "P4TR-8HZC", "W3NB-6JUE", "C5YF-2LSA"]
const log = useLog()
const collapsible = createCollapsibleController()
const open = ref(false)
</script>
<template>
<div data-slot="stack">
<div data-slot="controls">
<button type="button" @click="collapsible.toggle()">Toggle</button>
</div>
<Collapsible
v-model:open="open"
data-variant="card"
:controller="collapsible"
@open-change="
(next: boolean, details: ChangeDetails) =>
log(`${next ? 'Opened' : 'Closed'} (${details.reason})`)
"
>
<CollapsibleTrigger>
<span data-slot="stack">
<span>Recovery codes</span>
<span data-slot="hint">Each code signs you in once</span>
</span>
<ChevronDownIcon />
</CollapsibleTrigger>
<CollapsiblePanel>
<div data-slot="stack">
<ul>
<li v-for="code in recoveryCodes" :key="code">{{ code }}</li>
</ul>
<button type="button" @click="collapsible.close()">Done</button>
</div>
</CollapsiblePanel>
</Collapsible>
</div>
</template>动画
把测量出的 --collapsible-panel-height 在零和内容高度之间做动画。data-starting-style 标记打开的第一帧,data-ending-style 在收起期间一直保留。面板会保持挂载,直到退场动画结束;第一次绘制时就展开的面板没有动画。
[data-slot="collapsible-panel"] {
height: var(--collapsible-panel-height);
overflow: hidden;
transition: height 0.2s ease-out;
&:is([data-starting-style], [data-ending-style]) {
height: 0;
}
}@open-change-complete 在打开或收起结束时报告。
<script setup lang="ts">
import { ChevronDownIcon } from "@lucide/vue"
import {
Collapsible,
CollapsiblePanel,
CollapsibleTrigger,
} from "@ui-sdk/vue/collapsible"
import { useLog } from "./log"
const notes = [
"Search now matches file contents, not only names.",
"Shared links remember the folder they were opened from.",
"Uploads resume after the connection drops.",
]
const log = useLog()
</script>
<template>
<Collapsible
data-variant="card"
@open-change-complete="
(open: boolean) => log(open ? 'Finished opening' : 'Finished closing')
"
>
<CollapsibleTrigger>
<span data-slot="stack">
<span>Release notes</span>
<span data-slot="hint">Version 2.4, released October 28</span>
</span>
<ChevronDownIcon />
</CollapsibleTrigger>
<CollapsiblePanel>
<ul>
<li v-for="note in notes" :key="note">{{ note }}</li>
</ul>
</CollapsiblePanel>
</Collapsible>
</template>自定义渲染
用部件的 #render 插槽选择它的元素,从该插槽或默认插槽读取它的状态。下面的触发器渲染在标题里,面板渲染成有序列表。
<script setup lang="ts">
import { ChevronDownIcon } from "@lucide/vue"
import {
Collapsible,
CollapsiblePanel,
CollapsibleTrigger,
} from "@ui-sdk/vue/collapsible"
const updates = [
{ label: "Out for delivery", time: "07:40" },
{ label: "Arrived at the local depot", time: "05:12" },
{ label: "Left the sorting center", time: "Wed 21:03" },
]
</script>
<template>
<Collapsible data-variant="card">
<CollapsibleTrigger>
<template #render="{ props, children }">
<h3>
<button v-bind="props"><component :is="children" /></button>
</h3>
</template>
<template #default="{ state }">
<span data-slot="stack">
<span>Track parcel</span>
<span data-slot="hint">{{
state.open ? "3 updates" : "Arrives Thursday"
}}</span>
</span>
<ChevronDownIcon />
</template>
</CollapsibleTrigger>
<CollapsiblePanel>
<template #render="{ props }">
<ol v-bind="props">
<li v-for="update in updates" :key="update.label" data-slot="row">
<span>{{ update.label }}</span>
<span>{{ update.time }}</span>
</li>
</ol>
</template>
</CollapsiblePanel>
</Collapsible>
</template>样式
用 data-open、data-closed 和 data-disabled 为每个状态设置样式。示例根据触发器的 data-open 转动箭头。
每个部件都有一个 data-slot,比如 collapsible-panel,它的状态属性列在数据属性下。
CollapsiblePanel
| 变量 | 内容 |
|---|---|
--collapsible-panel-height | The panel's content height in pixels while it animates, auto once open and 0px while closed. |
--collapsible-panel-width | The panel's content width, the same way. For panels that open sideways. |
数据属性
Collapsible
[data-slot="collapsible"]
| 属性 | 含义 |
|---|---|
data-open | Present while the panel is open. |
data-closed | Present while the panel is closed, including while it animates out. |
data-disabled | Present when the collapsible can't open or close. |
CollapsibleTrigger
[data-slot="collapsible-trigger"]
| 属性 | 含义 |
|---|---|
data-open | Present while the panel is open. |
data-closed | Present while the panel is closed, including while it animates out. |
data-disabled | Present when the collapsible can't open or close. |
CollapsiblePanel
[data-slot="collapsible-panel"]
| 属性 | 含义 |
|---|---|
data-open | Present while the panel is open. |
data-closed | Present while the panel is closed, including while it animates out. |
data-disabled | Present when the collapsible can't open or close. |
data-starting-style | Present for the panel's first frame. Style the state you animate from. |
data-ending-style | Present while the panel animates closed. Style the state you animate to. |
API 参考
Props 与下面相同。onOpenChange 对应 v-model:open,需要拿到详情时用 @open-change;onOpenChangeComplete 对应 @open-change-complete。
Collapsible
open?boolean- Whether the panel is open. Pass it to control the collapsible.
defaultOpen?boolean- Whether the panel is open at first, when uncontrolled.
@open-change((open: boolean, details: ChangeDetails<CollapsibleOpenChangeReason>) => void)- Runs when the panel wants to open or close.
details.reasonsays why, anddetails.cancel()stops it. @open-change-complete((open: boolean) => void)- Runs once the panel has finished opening or closing, after any animation.
disabled?boolean- Stops the trigger from opening or closing the panel. The trigger stays focusable.
controller?object- Opens and closes the panel from anywhere. Create one with
createCollapsibleController(). debug?boolean- Logs every state change with its reason to the console, in development builds.
CollapsibleTrigger
触发器没有自己的选项,只有它渲染出的元素的 props。
CollapsiblePanel
keepMounted?boolean- Whether the panel stays in the page, hidden, while closed.
hiddenUntilFound?boolean- Keeps the closed panel in the page as
hidden="until-found", so the browser's find in page can match its text and open it.
状态
渲染函数、基于状态的类名和槽位按部件分别收到什么。触发器和面板收到与 collapsible 相同的 状态。
openboolean- Whether the panel is open.
disabledboolean- Whether the trigger ignores presses, by its own
disabledor a disabled fieldset around it. Theopenoption and the controller can still open and close it.
原因
details.reason 说明面板为什么打开或收起。
trigger-pressMouseEvent | KeyboardEvent | TouchEvent- Click, tap, Enter or Space on the part that opens, closes or toggles. Arrow keys that open a menu or submenu count too. A tap on a combobox's input that opens a sheet reports its
touchend. find-in-pageEvent- The browser's find in page matched hidden content.
imperativenone- A controller or
apicall.
控制器
用 createCollapsibleController() 创建一个,并作为 controller 传入。
每个写入方法都接受可选的 { reason, event },并在变更被取消或没有任何效果时返回 false。
isOpen()() => boolean- Whether the panel is open.
setOpen(open, options?)(open: boolean, options?: CollapsibleChangeOptions | undefined) => boolean- Opens or closes the panel. Returns false when the change was canceled or not possible.
open(options?)(options?: CollapsibleChangeOptions | undefined) => boolean- Opens the panel.
close(options?)(options?: CollapsibleChangeOptions | undefined) => boolean- Closes the panel.
toggle(options?)(options?: CollapsibleChangeOptions | undefined) => boolean- Opens the panel when it's closed, and closes it when it's open.
focus()() => void- Moves focus to the trigger.
事件
事件先收到新状态,再收到详情。只有 @open-change 没有被取消时,v-model 才会更新。
| 绑定 | 所属部件 | 收到 |
|---|---|---|
v-model:open | Collapsible | 面板是否展开 |
@open-change | Collapsible | (open, details):新状态,以及原因 |
@open-change-complete | Collapsible | (open),在面板动画之后 |
无障碍
Collapsible 遵循 WAI-ARIA 披露模式。触发器用 aria-expanded 表示面板是否展开,并在面板挂载期间用 aria-controls 指向它。下面每一行都是一个自动化测试。
键盘
| 按键 | 作用 |
|---|---|
| Enter, Space | 打开或收起面板。焦点留在触发器上。 |
| Tab | 把焦点从触发器移进展开的面板 |
焦点在面板里时收起面板,焦点会移回触发器。
经过测试的行为
- Starts closed, with the panel out of the page.
- Clicking the trigger opens the panel, and clicking again closes it.
- The trigger controls the panel.
- Generated ids start with ui-sdk-.
- It can start open.
- An opening can be canceled.
- A disabled collapsible doesn't open.
- A disabled trigger stays focusable and says it's disabled.
- A controller opens and closes it from anywhere.
- Enter toggles the panel and keeps focus on the trigger.
- Space toggles the panel and keeps focus on the trigger.
- Tab moves from the open trigger into the panel.
- Tab skips a closed panel.
- With keepMounted, the closed panel stays in the page, hidden.
- With keepMounted, a class that sets display doesn't show the closed panel.
- With hiddenUntilFound, the closed panel stays findable.
- With hiddenUntilFound, a class that sets display keeps the closed panel findable.
- Finding text in the closed panel opens it.
- Following a link into the closed panel opens it.
- The browser's own find opens the closed panel.
运行于 React、Vue 和 web components。
在模式基线之外:
- Closing the panel while focus is inside it moves focus to the trigger.
- OnOpenChangeComplete runs once the animation has finished.
- An inert its owner writes during the exit stays once the exit ends.
- Find in page reports its own reason and opens without an animation.
- A canceled find in page keeps the panel hidden until found.
- An infinite exit animation still unmounts the panel.
- Parts report open and closed through data attributes.
运行于 React、Vue 和 web components。
在每个经过测试的框架里,屏幕阅读器听到的内容相同,由虚拟屏幕阅读器录制:
- Announcements while using the collapsible with a keyboard.
运行于 React、Vue 和 web components。