⌘K 打开的模态搜索框 + 分组命令列表:按标签与关键词过滤、行尾显示快捷键、没有命中时说一句话;配一个长得像搜索框的入口按钮 CommandTrigger。命令从哪来、执行什么留在应用。
状态:experimental · 组件:CommandPalette、CommandTrigger
引入
定位到「引入」import { CommandPalette, CommandTrigger } from 'nacre'用法
定位到「用法」CommandTrigger 放在标题栏中间,按下打开;CommandPalette 受控(open + onClose),items 给 id、label、group、keywords、shortcut,onSelect 回传选中的那一项(应用可在项上带执行函数)。选完先收起、焦点归位,再回调。
import { Text, View, signal } from 'nsh'import type { ViewRef } from 'nsh'import { CommandPalette, CommandTrigger, TEXT_SIZE } from 'nacre'import type { CommandPaletteItem } from 'nacre'
const COMMANDS = (): CommandPaletteItem[] => [ { id: 'go.chat', label: '会话', group: '跳页', shortcut: 'mod+1' }, { id: 'go.dashboard', label: '工作看板', group: '跳页', shortcut: 'mod+2' }, { id: 'cmd.status', label: '切换在线状态', group: '命令', keywords: ['online', 'status'], shortcut: 'mod+shift+s' }, { id: 'cmd.refresh', label: '刷新会话列表', group: '命令', keywords: ['reload'] }, { id: 'cmd.export', label: '导出记录(无权限)', group: '命令', disabled: true },]
export function CommandPaletteUsage() { const open = signal(false) const last = signal('') let trigger: ViewRef | undefined return ( <View class="flex-row flex-wrap items-center gap-3"> <CommandTrigger testID="demo-cmdp-trigger" label="全局搜索" accessibilityLabel="命令面板" shortcut="mod+k" viewRef={(node) => (trigger = node)} onPress={() => open.set(true)} /> <Text testID="demo-cmdp-last" class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$上次执行:${last() || '—'}`}</Text> <CommandPalette testID="demo-cmdp" open={open()} onClose={() => open.set(false)} returnFocus={() => trigger} placeholder="搜索会话、命令…" accessibilityLabel="命令面板" items={COMMANDS()} onSelect={(item) => last.set(item.id)} /> </View> )}示例
定位到「示例」命令多到要滚动
定位到「命令多到要滚动」列表限高 360,超出就在面板里滚;会滚时行尾给覆盖式滚动条让出 10,快捷键不被压住。
import { Text, View, signal } from 'nsh'import { CommandPalette, CommandTrigger, TEXT_SIZE } from 'nacre'import type { CommandPaletteItem } from 'nacre'
export function CommandPaletteLong() { const open = signal(false) const last = signal('') const items: CommandPaletteItem[] = Array.from({ length: 18 }, (_, i) => ({ id: `cmd.${i + 1}`, label: `$命令 ${i + 1}`, group: i < 9 ? '常用' : '更多', shortcut: i < 9 ? `mod+shift+${i + 1}` : `mod+alt+${i - 8}`, })) return ( <View class="flex-row flex-wrap items-center gap-3"> <CommandTrigger testID="demo-cmdp-long-trigger" label="全部命令" onPress={() => open.set(true)} width={200} /> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$上次执行:${last() || '—'}`}</Text> <CommandPalette testID="demo-cmdp-long" open={open()} onClose={() => open.set(false)} items={items} placeholder="搜索命令…" accessibilityLabel="全部命令" onSelect={(item) => last.set(item.id)} /> </View> )}外部搜索与高亮预览
定位到「外部搜索与高亮预览」输入 C001 可以按编号找到林嘉怡,高亮后显示预览。shouldFilter={false} 与 cmdk 的手动过滤约定一致:来源决定命中与排序,面板不再过滤。不同于 cmdk 的组合式列表,这里用 NSH 原生键盘导航;renderItem 可换行内容,renderPreview 提供只读预览。异步来源用 loading 表示等待,resolveSelection 返回最终项才关闭,返回 null 或等待时换词不会执行旧项。参照:https://github.com/dip/cmdk#parts-and-styling
import { Text, View, computed, signal } from 'nsh'import { CommandPalette, CommandTrigger, TEXT_SIZE } from 'nacre'import type { CommandPaletteItem } from 'nacre'
const COMMANDS = (): CommandPaletteItem[] => [ { id: 'go.chat', label: '会话', group: '跳页', shortcut: 'mod+1' }, { id: 'go.dashboard', label: '工作看板', group: '跳页', shortcut: 'mod+2' }, { id: 'cmd.status', label: '切换在线状态', group: '命令', keywords: ['online', 'status'], shortcut: 'mod+shift+s' }, { id: 'cmd.refresh', label: '刷新会话列表', group: '命令', keywords: ['reload'] }, { id: 'cmd.export', label: '导出记录(无权限)', group: '命令', disabled: true },]
export function CommandPaletteQuery() { const open = signal(false) const query = signal('') const last = signal('') const chats = [ { id: 'C001', name: '林嘉怡' }, { id: 'C002', name: '陈智源' }, ] const items = computed<CommandPaletteItem[]>(() => { const q = query().trim().toLowerCase() const hits = q ? chats.filter((chat) => `${chat.name} ${chat.id}`.toLowerCase().includes(q)).map((chat) => ({ id: chat.id, label: chat.name, group: '会话' })) : [] return [...hits, ...COMMANDS().filter((item) => [item.label, ...(item.keywords ?? [])].some((text) => text.toLowerCase().includes(q)))] }) return ( <View class="flex-row flex-wrap items-center gap-3"> <CommandTrigger testID="demo-cmdp-query-trigger" label="搜索会话" onPress={() => open.set(true)} width={200} /> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$上次执行:${last() || '—'}`}</Text> <CommandPalette testID="demo-cmdp-query" open={open()} onClose={() => { open.set(false) query.set('') }} query={query()} onQueryChange={(q) => query.set(q)} items={items()} shouldFilter={false} renderPreview={(item) => chats.some((chat) => chat.id === item.id) ? <Text class={`p-3 ${TEXT_SIZE.body} text-popover-foreground`}>{`${item.label} · ${item.id}`}</Text> : null} placeholder="输入姓名或编号" accessibilityLabel="搜索会话" emptyText="没有这个客户的会话" onSelect={(item) => last.set(item.id)} /> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| ↑ ↓ | 在命令之间移动当前行,跳过禁用项、到头回绕 |
| Return | 执行当前行:先收起,再回调 onSelect |
| Esc | 有搜索词时先清空;搜索词为空时关闭,焦点回到打开前的位置(或 returnFocus) |
| Tab | 只在面板里转(模态) |
无障碍
定位到「无障碍」- 面板是模态对话框:开着时读屏与 Tab 只在面板里,关闭后焦点回到打开前有焦点的视图;给了 returnFocus 就回到它。
- 列表读作 listbox,当前行随方向键宣读;accessibilityLabel 同时是面板与搜索框的读屏名。
- CommandTrigger 是按钮,不是输入框:读屏名默认是 label,标题栏里建议写成「命令面板」。
该用 / 不该用
定位到「该用 / 不该用」该用
- 命令的快捷键在 items 里写出来(只显示,绑定由应用做)
- 给同义词、英文名写 keywords
- 入口用 CommandTrigger,快捷键在键帽上
- 需要焦点回到入口时传 returnFocus
不该用
- 在面板里放表单或多步流程(用 Dialog)
- 用它从固定选项里选一个表单值(用 Combobox)
- 把执行逻辑塞进组件(onSelect 拿回选中的项去执行)
API
定位到「API」CommandPalette
定位到「CommandPalette」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
open * |
boolean |
— | 开着吗(受控):关时整块卸载 |
query |
string |
— | 搜索词(受控);不给时面板自己管,每次打开都是空的 |
onQueryChange |
(query: string) => void |
— | 输入或 Esc 清空搜索词时回调;受控时在这里改 query |
items * |
readonly T[] |
— | 全部可选项,按显示顺序;面板按标签与 keywords 过滤(不分大小写),没有命中的组整组不显示 |
shouldFilter |
boolean |
— | false:按调用方给的顺序展示全部结果,不再按搜索词过滤 |
loading |
boolean |
— | 搜索进行中,搜索框末尾显示加载提示 |
renderItem |
(item: T) => JSX.Element | null |
— | 自定义行内容;返回 null 时仍用默认的标签、快捷键 |
renderPreview |
(item: T) => JSX.Element | null |
— | 高亮项的只读预览;返回 null 时不占用预览面 |
resolveSelection |
(item: T) => T | null | Promise<T | null> |
— | 执行前决定最终选项;返回 null 保持打开。换词、另选或关闭会作废先前的等待 |
onSelect * |
(item: T) => void |
— | 选了一项(回车、点按):回传 items 里的那一项本身(应用可以在项上带执行函数)。 面板在当拍取出这一项,再收起(onClose)、焦点归位,最后回调:命令自己挪焦点不会被抢回去,应用也不必在面板卸载后再查条目 |
onClose * |
() => void |
— | 要关:Esc(搜索词为空时;有词时 Esc 先清空)、点面板外、选完 |
returnFocus |
() => ViewRef | undefined |
— | 收起后把键盘焦点交给谁;不给时交还打开前有焦点的视图(平台的模态层自己还) |
placeholder |
string |
— | 搜索框的占位 |
accessibilityLabel |
string |
— | 读屏对面板(也是对搜索框)的称呼 |
emptyText |
string |
— | 没有命中时的一句话,默认「没有匹配的结果」 |
width |
number |
— | 宽度(pt),默认 520 |
testID |
string |
— | 面板的 testID(app.type 打字落到面板里的搜索框) |
* 必填
CommandTrigger
定位到「CommandTrigger」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
label * |
string |
— | 框里的提示字(「全局搜索」),也是默认读屏名 |
shortcut |
string |
— | 右侧键帽显示的快捷键,按 keymap 的写法("mod+k") |
onPress * |
() => void |
— | 按下(点按、空格、回车):打开命令面板 |
width |
number |
264 |
宽度(pt),默认 264 |
accessibilityLabel |
string |
— | 读屏名,默认 label;它其实是打开命令面板的按钮,可以写「命令面板」 |
testID |
string |
— | 按钮的 testID |
viewRef |
(node: ViewRef) => void |
— | 关闭命令面板后焦点要回到这里时,把 ref 交给 CommandPalette 的 returnFocus |
* 必填