Swipeable List
通过指针或键盘输入,展示列表行左右两侧的操作。
适用场景
- 当界面需要以下行为时使用 Swipeable List:通过指针或键盘输入,展示列表行左右两侧的操作。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/swipeable-list.json
首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
预览
Priority queue
Ready
Launch brief
Finalize the announcement copy
9:41
Client feedback
Three comments need a response
11:08
Design review
Check spacing before handoff
13:20
Flagged run
Retry queue has one failed job
Now
4 openToday
packages/vue/src/blocks/swipeable-list/demo.vue
<script setup lang="ts">
import { h, shallowRef } from "vue";
import {
Check,
Clock3,
FileText,
Flag,
Mail,
Pin,
RotateCcw,
Trash2,
UserRound,
} from "lucide-vue-next";
import {
SwipeableList,
type SwipeAction,
type SwipeableListItem,
} from "@/components/orvui/blocks/swipeable-list";
const leftActions: SwipeAction[] = [
{ id: "done", label: "Done", icon: Check, tone: "success" },
{ id: "pin", label: "Pin", icon: Pin, tone: "primary" },
];
const rightActions: SwipeAction[] = [
{ id: "later", label: "Later", icon: Clock3, tone: "warning" },
{ id: "trash", label: "Trash", icon: Trash2, tone: "danger" },
];
const descriptions = [
{
id: "brief",
title: "Launch brief",
description: "Finalize the announcement copy",
meta: "9:41",
icon: FileText,
},
{
id: "feedback",
title: "Client feedback",
description: "Three comments need a response",
meta: "11:08",
icon: Mail,
},
{
id: "review",
title: "Design review",
description: "Check spacing before handoff",
meta: "13:20",
icon: UserRound,
},
{
id: "incident",
title: "Flagged run",
description: "Retry queue has one failed job",
meta: "Now",
icon: Flag,
},
];
const initial: SwipeableListItem[] = descriptions.map(({ icon, ...item }) => ({
...item,
leftActions,
rightActions,
leading: () =>
h(
"div",
{
class:
"grid h-10 w-10 place-items-center rounded-xl border border-border bg-background text-muted-foreground",
},
[h(icon, { class: "h-4 w-4" })],
),
}));
const items = shallowRef(initial),
lastAction = shallowRef("Ready");
function action({
item,
action,
}: {
item: SwipeableListItem;
action: SwipeAction;
}) {
lastAction.value = `${action.label} · ${item.title}`;
if (action.id === "trash")
items.value = items.value.filter((row) => row.id !== item.id);
}
function reset() {
items.value = initial;
lastAction.value = "Queue restored";
}
</script>
<template>
<div class="flex min-h-96 w-full items-center justify-center">
<div
class="w-full max-w-sm rounded-[2rem] border border-border bg-background p-3 shadow-2xl"
>
<div class="mb-3 flex items-center justify-between px-1">
<div>
<p class="text-sm font-semibold text-foreground">Priority queue</p>
<p class="text-xs text-muted-foreground">{{ lastAction }}</p>
</div>
<button
type="button"
@click="reset"
class="inline-flex h-8 items-center gap-1.5 rounded-full border border-border px-3 text-xs font-medium text-muted-foreground transition-colors hover:text-foreground"
>
<RotateCcw class="h-3.5 w-3.5" />Reset
</button>
</div>
<SwipeableList :items="items" @action="action" />
<div
class="mt-3 flex items-center justify-between px-1 text-[11px] font-medium text-muted-foreground"
>
<span>{{ items.length }} open</span><span>Today</span>
</div>
</div>
</div>
</template>
packages/vue/src/blocks/swipeable-list/src/SwipeableList.vue
状态与事件
需要由表单、路由或父组件管理时,绑定下列状态并接收对应更新。v-model 是属性与 update 事件的简写;值的类型应与 API 一致。
| 组件 | Vue 绑定 | 更新事件 |
|---|---|---|
SwipeableList | v-modelnull | SwipeableListValue | update:modelValue |
可选受控属性未传入时,按组件文档的默认行为工作;不要用固定的 false 或空值代替未传入。异步请求和持久化在应用中处理,再把结果传回组件。
API 参考
itemsSwipeableListItem[]—modelValue?null | SwipeableListValue—defaultValue?null | SwipeableListValuenullactionWidth?number56revealThreshold?number34closeOnAction?booleantrueclassNames?SwipeableListClassNames—事件
update:modelValue[value: SwipeableListValue | null]@update:modelValuevalueChange[value: SwipeableListValue | null]@valueChangeaction[payload: { item: SwipeableListItem; action: SwipeAction; side: SwipeSide; }]@action插槽
item—作用域参数: v-bind
#item无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Swipeable List?
- 通过 items、modelValue、defaultValue、actionWidth、revealThreshold、closeOnAction、classNames 配置 Swipeable List;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 监听 update:modelValue、valueChange、action,将组件连接到应用状态。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
请留意
部分组件参考了开源作品,相关署名与独立许可请参阅 NOTICE。
更新于
来源与许可 ↗MIT · 0.1.1