Sortable List
使用指针或键盘重排列表,并提供撤销操作。
适用场景
- 当界面需要以下行为时使用 Sortable List:使用指针或键盘重排列表,并提供撤销操作。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/sortable-stack.json
首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
预览
In the studio
Make room for what matters.
Drag a handle to reorder. Use Up and Down to move, Home and End to move to the edges.
- 01
Collect references
Find the right direction
- 02
Explore the visual system
Type, color, and composition
- 03
Build the first prototype
Make the interaction tangible
- 04
Ship something useful
Polish the details and release
packages/vue/src/motion/sortable-stack/demo.vue
<script setup lang="ts">
import { Circle, FileText, Layers, Palette, Rocket } from "lucide-vue-next";
import { SortableList, SortableListGroup, SortableListItem, SortableListHandle, SortableListItemContent, SortableListUndo } from "@/components/orvui/motion/sortable-stack";
const priorities = [
{ id: "research", title: "Collect references", detail: "Find the right direction", icon: FileText, color: "bg-amber-500/10 text-amber-600 dark:text-amber-400" },
{ id: "design", title: "Explore the visual system", detail: "Type, color, and composition", icon: Palette, color: "bg-rose-500/10 text-rose-600 dark:text-rose-400" },
{ id: "build", title: "Build the first prototype", detail: "Make the interaction tangible", icon: Layers, color: "bg-blue-500/10 text-blue-600 dark:text-blue-400" },
{ id: "ship", title: "Ship something useful", detail: "Polish the details and release", icon: Rocket, color: "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400" },
];
</script>
<template>
<div class="w-full max-w-md"><div class="mb-5 flex items-end justify-between gap-4 px-1"><div><p class="mb-1 text-[10px] font-medium uppercase tracking-[0.18em] text-muted-foreground">In the studio</p><h3 class="text-lg font-medium tracking-tight">Make room for what matters.</h3></div><span class="shrink-0 text-xs tabular-nums text-muted-foreground">04 tasks</span></div>
<SortableList :default-items="priorities" label="Studio priorities" :get-item-label="item => item.title"><template #default="{ items }"><SortableListGroup><SortableListItem v-for="(item, index) in items" :key="item.id" :id="item.id"><SortableListHandle /><SortableListItemContent><div class="flex items-center gap-3"><span :class="['flex size-10 shrink-0 items-center justify-center rounded-xl', item.color]"><component :is="item.icon" :size="18" aria-hidden="true" /></span><div class="min-w-0 flex-1"><p class="text-sm font-medium">{{ item.title }}</p><p class="mt-0.5 text-xs text-muted-foreground">{{ item.detail }}</p></div><span class="text-[10px] tabular-nums text-muted-foreground">{{ String(index + 1).padStart(2, '0') }}</span><Circle :size="14" class="text-border" aria-hidden="true" /></div></SortableListItemContent></SortableListItem></SortableListGroup><SortableListUndo /></template></SortableList>
</div>
</template>
packages/vue/src/motion/sortable-stack/src/SortableList.vue
状态与事件
需要由表单、路由或父组件管理时,绑定下列状态并接收对应更新。v-model 是属性与 update 事件的简写;值的类型应与 API 一致。
| 组件 | Vue 绑定 | 更新事件 |
|---|---|---|
SortableList | v-modelany[] | update:modelValue |
SortableList | v-model:itemsany[] | update:items |
可选受控属性未传入时,按组件文档的默认行为工作;不要用固定的 false 或空值代替未传入。异步请求和持久化在应用中处理,再把结果传回组件。
API 参考
SortableList
modelValue?any[]—items?any[]model.valuedefaultItems?any[]() => []getItemLabel?(item: any) => string—label?string"Sortable items"disabled?booleanfalseshowUndo?booleantrueitemClassName?string—事件
update:modelValue—@update:modelValueupdate:items—@update:itemsitemsChange—@itemsChangechange—@change插槽
default—作用域参数: items, move-item, undo
#defaultitem—作用域参数: item, index
#itemSortableListGroup
插槽
default() => VNodeChild#defaultSortableListItem
idstring—label?string—插槽
default() => VNodeChild#defaultSortableListHandle
disabled?booleanfalse插槽
default() => VNodeChild#defaultSortableListItemContent
插槽
default() => VNodeChild#defaultSortableListUndo
disabled?booleanfalse插槽
default() => VNodeChild#default无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Sortable List?
- 通过 modelValue、items、defaultItems、getItemLabel、label、disabled、showUndo、itemClassName 配置 Sortable List;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 监听 update:modelValue、update:items、itemsChange、change,将组件连接到应用状态。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
更新于
来源与许可 ↗MIT · 0.1.1