命令面板
Command Palette
快捷键拉出动作列表,支持即时筛选、键盘选择和关闭后归位焦点。
适用场景
把常用导航和编辑器命令集中在一个可搜索入口。 每条命令用清楚的动作和对象命名,用户靠键盘搜索时能快速认出要执行的那一条。
应用示例
编辑器用户按快捷键打开命令面板,输入「复制页面」,按回车执行匹配命令。面板关闭,页面完成复制,键盘焦点回到编辑器。
AI 提示词
给【应用】加入命令面板。使用对话框、带标签的过滤框和选项列表,方向键更新活动项,回车或点按执行,关闭时归还焦点。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>命令面板</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.ui { width: min(100%, 238px); display: grid; gap: 8px; position: relative; font-size: 12px; line-height: 1.35; }
.ui .surface { min-width: 0; border: 1px solid #676591; border-radius: 10px; background: #282744; padding: 8px; }
.ui .bar { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.ui .actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px; }
.ui .muted { color: #cecae9; font-size: 11px; }
.ui .center { text-align: center; }
.ui .mono { font: 11px ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.ui p { margin: 0; }
.ui .button { min-height: 30px; padding: 4px 7px; border-radius: 7px; border: 1px solid #79779f; background: #3d3a60; color: #f9f5ff; font-size: 11px; font-weight: 650; cursor: pointer; }
.ui .button:hover { background: #4e4976; }
.ui .primary { background: #c5eeec; border-color: #dafaee; color: #17213b; }
.ui .primary:hover { background: #e3fff9; }
.ui .button:disabled { opacity: .6; cursor: default; }
.ui .field { min-width: 0; width: 100%; border: 1px solid #79779f; border-radius: 6px; background: #1b1a31; color: #faf5ff; padding: 5px 7px; font-size: 12px; }
.ui .field::placeholder { color: #c2bcdd; }
.ui input[type="range"] { accent-color: #b8ebe0; width: 100%; min-width: 0; margin: 0; }
.ui button { font: inherit; }
.ui [hidden] { display: none !important; }
.ui progress { accent-color: #b8ebe0; }
@media (prefers-reduced-motion: reduce) {
.ui *, .ui *::after, .ui *::before { transition-duration: 0s !important; animation-duration: 0s !important; scroll-behavior: auto !important; }
}
dialog{width:min(92%,224px); inset:0; margin:auto; background:#211e37; color:inherit; border:1px solid #9a8ebf; border-radius:11px; padding:8px; box-shadow:0 10px 28px #090813b0} dialog::backdrop{background:#0d0b1a9c} [role=option]{padding:5px;font-size:11px;border-radius:5px;cursor:pointer} [aria-selected=true]{background:#59527b;color:#fff} kbd{margin-inline-start:6px}
</style>
</head>
<body>
<main class="demo">
<div class="ui"><button class="button primary open" type="button">命令 <kbd>⌘K</kbd></button><p class="muted center" role="status"></p><dialog aria-label="命令"><input class="field" role="combobox" aria-expanded="true" aria-controls="commands" aria-label="筛选命令" placeholder="搜索动作…"><div id="commands" role="listbox"></div></dialog></div>
</main>
<script>
const commands = ["新建笔记","打开归档","切换工作区"];
const dialog = document.querySelector('dialog');
const input = dialog.querySelector('input');
const list = dialog.querySelector('[role=listbox]');
const opener = document.querySelector('.open');
const status = document.querySelector('[role=status]');
let active = 0;
let visible = [];
function render() {
visible = commands.filter((name) => name.toLowerCase().includes(input.value.trim().toLowerCase()));
active = Math.min(active, Math.max(0, visible.length - 1));
list.replaceChildren();
for (const [index, name] of visible.entries()) {
const option = document.createElement('div');
option.id = 'command-' + index;
option.setAttribute('role', 'option');
option.setAttribute('aria-selected', String(index === active));
option.textContent = name;
option.addEventListener('click', () => select(index));
list.append(option);
}
if (visible.length) input.setAttribute('aria-activedescendant', 'command-' + active);
else input.removeAttribute('aria-activedescendant');
}
function select(index) {
if (!visible[index]) return;
status.textContent = visible[index];
dialog.close();
}
function open() { if (dialog.open) return; input.value = ''; active = 0; render(); dialog.showModal(); input.focus(); }
opener.addEventListener('click', open);
dialog.addEventListener('close', () => opener.focus());
input.addEventListener('input', () => { active = 0; render(); });
input.addEventListener('keydown', (event) => {
if (event.key === 'Enter') { event.preventDefault(); select(active); }
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
if (!visible.length) return;
active = (active + visible.length + (event.key === 'ArrowDown' ? 1 : -1)) % visible.length;
render();
}
});
document.addEventListener('keydown', (event) => {
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'k') { event.preventDefault(); open(); }
});
</script>
</body>
</html>UI Skills 原创示例