Command Menu命令菜单
在同一个搜索框里查找并执行分组命令。操作可显示图标、来源和数字快捷键,选中背景随键盘或指针连续滑动。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add command-menu安装
在项目根目录运行,安装源码。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add command-menu使用
React
"use client";
import { useState } from "react";import { FilePlus2, Globe, PenTool } from "lucide-react";import { KalosCommandMenu, type KalosCommandGroup } from "@/components/ui/kalos-command-menu";
const groups: KalosCommandGroup[] = [ { label: "推荐操作", items: [ { value: "design", label: "打开设计文件", hint: "Figma", icon: <PenTool />, shortcut: 1 }, { value: "website", label: "编辑网页", hint: "Framer", icon: <Globe />, shortcut: 2 }, ] }, { label: "常用操作", items: [ { value: "document", label: "新建文档", icon: <FilePlus2 /> }, ] },];
export default function Example() { const [selection, setSelection] = useState("");
return <> <KalosCommandMenu groups={groups} placeholder="输入命令或搜索…" onRun={value => { const item = groups.flatMap(group => group.items).find(item => item.value === value); if (item) setSelection(item.label); }} /> {selection && <p role="status">已选择「{selection}」</p>} </>;}API
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
groups | KalosCommandGroup[] | — | 分组与操作项;每项包含唯一值、名称以及可选的图标、上下文和禁用状态 |
items[].hint | string | — | 与名称同行的辅助信息,可用于显示服务名、频道或操作目标,也参与搜索 |
items[].shortcut | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | — | 搜索框聚焦时用 ⌘ / Ctrl 加数字执行对应的可见操作。数字在菜单内应唯一 |
query | string | — | 受控搜索词 |
defaultQuery | string | "" | 非受控时的初始搜索词 |
onQueryChange | (query: string) => void | — | 搜索词变化时调用 |
onRun | (value: string) => void | — | 执行可用操作后回调 |
onClose | () => void | — | 搜索词为空时按 Esc 触发,可用于关闭外层对话框 |
filter | boolean | true | 按名称、同行提示、字符串上下文和关键词过滤。外部全文搜索时设为 false |
autoFocus | boolean | false | 挂载时聚焦搜索框 |
placeholder | string | 搜索命令… | 空输入提示 |
label | string | 命令菜单 | 搜索框及结果列表的可访问名称 |
emptyMessage | string | 没有找到匹配的内容 | 没有匹配项时的文案 |
键盘操作
- ↑ / ↓
- 在可用项之间移动,跳过禁用项
- Tab / Shift+Tab
- 跳到下一组或上一组
- Ctrl / ⌘ + Home / End
- 移动到首项或末项
- Enter
- 执行当前操作
- Ctrl / ⌘ + 数字
- 执行设置了对应数字且当前可见的操作
- Esc
- 先清空搜索词。搜索词为空时,请求关闭或让内嵌搜索框失焦