本文へ移動
日本語

仕組み

Matching tries a contiguous run first, then the letters in order (at most three pieces), and falls back to keywords. Focus stays in the field; a single highlight element glides to the active option with transform and height, and keeps it in view by adjusting the list's own scroll, never the page's.

向いている場面

  • Apps with many pages or actions where power users want to jump by keyboard.
  • A single entry point for search, navigation and commands.

向かない場面

  • Searching large remote data with filters: use a full search page.
  • Touch-first apps without a keyboard: a visible menu is easier.

使い方

import { useState } from 'react'
import { CommandPalette } from './CommandPalette.jsx'

export function AppSearch() {
  const [open, setOpen] = useState(false)
  return (
    <CommandPalette
      open={open}
      onOpenChange={setOpen}
      recent={[{ id: 'roadmap', label: 'Q4 roadmap', hint: 'Projects' }]}
      groups={[
        { id: 'pages', label: 'Pages', items: [{ id: 'guide', label: 'Onboarding guide', keywords: ['welcome'] }] },
        { id: 'actions', label: 'Actions', items: [{ id: 'new', label: 'New page', shortcut: ['⌘', 'N'] }] },
      ]}
      onSelect={(item) => console.log(item.id)}
    />
  )
}

プロパティ

プロパティ型既定値説明
openboolean—Whether the palette is shown.
onOpenChange(open: boolean) => void—Called on the hotkey, Escape, a click outside or after a choice.
groups{ id, label, items: { id, label, hint?, icon?, shortcut?: string[], keywords?: string[], disabled? }[] }[]—Grouped commands; empty groups are hidden while searching.
recentItem[]—Shown as the first group while the field is empty.
onSelect(item) => void—Called with the chosen item.
labelsobject—Replaces built-in text: placeholder, dialog, recent, empty ({query} is replaced), emptyHint, navigate, open, close.
hotkeystring | false'k'Letter that toggles the palette with ⌘ or Ctrl; false turns it off.
containedbooleanfalseFill the nearest positioned ancestor instead of the viewport.
footerbooleantrueShow the keyboard hint bar.

アクセシビリティ

  • role dialog with aria-modal; the field is a combobox that controls a listbox, with grouped role option rows.
  • aria-activedescendant announces the active option while focus stays in the field.
  • Up and Down (or Ctrl+N and Ctrl+P) move, Home and End jump, Enter chooses, Escape clears the query and then closes; Tab stays inside.
  • Focus moves to the field on open and returns to the previous element on close.
  • The empty state is a role status message.
  • With reduced motion the panel only fades and the highlight jumps.

モーション

Panel enters 260ms ease-out from 6px above at 0.98 scale, leaves in 140ms; the highlight glides 200ms ease-out; the enter glyph slides in 4px.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

CommandPalette.jsxcommand-palette.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

ナビゲーション
「ドックバー」は Pro に含まれます。解除するとプレビューが表示されます。