Command Palette
在支持键盘操作的搜索面板中查找并执行分组命令。
适用场景
- 当界面需要以下行为时使用 Command Palette:在支持键盘操作的搜索面板中查找并执行分组命令。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/command-palette.json
首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
预览
Press ⌘ J (or Ctrl J) to open.
packages/vue/src/blocks/command-palette/demo.vue
<script setup lang="ts">
import { ref } from "vue";
import { FileText, Home, Plus, Settings, User } from "lucide-vue-next";
import { CommandPalette } from "@/components/orvui/blocks/command-palette";
const open = ref(false);
const items = [
{
id: "home",
label: "Go to Home",
group: "Navigation",
icon: Home,
hint: "G H",
},
{
id: "profile",
label: "Open profile",
group: "Navigation",
icon: User,
hint: "G P",
},
{ id: "settings", label: "Settings", group: "Navigation", icon: Settings },
{
id: "new-doc",
label: "Create document",
group: "Actions",
icon: FileText,
hint: "⌘ N",
},
{
id: "new-project",
label: "New project",
group: "Actions",
icon: Plus,
hint: "⌘ ⇧ N",
},
];
</script>
<template>
<div class="flex flex-col items-start gap-3">
<button
type="button"
@click="open = true"
class="inline-flex h-10 items-center rounded-full border border-border bg-card px-5 text-sm font-medium text-foreground press hover:border-(--color-border-strong)"
>
Open command palette
</button>
<p class="text-sm text-muted-foreground">
Press
<kbd
class="rounded border border-border bg-card px-1.5 py-0.5 text-xs text-foreground"
>⌘ J</kbd
>
(or
<kbd
class="rounded border border-border bg-card px-1.5 py-0.5 text-xs text-foreground"
>Ctrl J</kbd
>) to open.
</p>
<CommandPalette v-model:open="open" shortcut="j" :items="items" />
</div>
</template>
packages/vue/src/blocks/command-palette/src/CommandPalette.vue
状态与事件
需要由表单、路由或父组件管理时,绑定下列状态并接收对应更新。v-model 是属性与 update 事件的简写;值的类型应与 API 一致。
| 组件 | Vue 绑定 | 更新事件 |
|---|---|---|
CommandPalette | v-model:openboolean | update:open |
可选受控属性未传入时,按组件文档的默认行为工作;不要用固定的 false 或空值代替未传入。异步请求和持久化在应用中处理,再把结果传回组件。
API 参考
itemsCommandItem[]—shortcut?string"k"placeholder?string"Type a command or search…"emptyMessage?string"No results found."open?booleanundefineddefaultOpen?booleanfalse事件
update:open[open: boolean]@update:openopenChange[open: boolean]@openChangeselect[item: CommandItem]@select插槽
trigger—作用域参数: open
#trigger无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Command Palette?
- 通过 items、shortcut、placeholder、emptyMessage、open、defaultOpen 配置 Command Palette;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 监听 update:open、openChange、select,将组件连接到应用状态。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
请留意
部分组件参考了开源作品,相关署名与独立许可请参阅 NOTICE。
更新于
来源与许可 ↗MIT · 0.1.1