仕組み
Renders the semantic nesting browsers expect: an outer kbd for the shortcut and one inner kbd per key. A raised cap is a faint vertical gradient with a hairline and a 2px skirt drawn as an inset shadow, so it reads as a physical key without a heavy border; glyph keys are square and word keys get side padding. Pressing a cap sinks the face by the skirt height on a short spring and tints it with the accent. In the demo the rows play their shortcuts in turn, and pressing keys on your own keyboard lights the matching row.
向いている場面
- Shortcut hints in menus, tooltips, command palettes and help pages.
- Onboarding that teaches a shortcut by showing the keys being pressed.
向かない場面
- Touch-only interfaces where there is no keyboard to press.
使い方
import { Kbd } from './Kbd.jsx'
const isApple = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform)
export function SearchHint() {
return (
<p>
Press <Kbd shortcut="mod+k" platform={isApple ? 'mac' : 'other'} /> to search, or <Kbd shortcut="g then i" /> for the inbox.
</p>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
shortcut | string | string[][] | — | Keys joined with + and steps with “then” or a comma: 'mod+shift+p', 'g then i'. Named keys: mod, cmd, ctrl, alt, shift, enter, esc, tab, backspace, space and the four arrows; other keys show as typed, single letters upper-cased. |
children | ReactNode | — | Without shortcut, a single cap with your own content. |
platform | 'mac' | 'other' | 'mac' | Apple glyphs (⌘ ⌥ ⇧) or words (Ctrl, Alt, Shift); mod maps to Command or Ctrl. Decide it after mount to avoid a hydration mismatch. |
size / variant | 'sm' | 'md' | 'lg' / 'raised' | 'flat' | 'md' / 'raised' | 22, 26 or 40px caps; raised has a physical skirt, flat is a quiet chip for menus. |
pressed | boolean | string[] | false | Sinks every cap, or only the listed keys. |
thenLabel | string | 'then' | Word between steps, shown and spoken. |
アクセシビリティ
- The outer kbd carries an aria-label with spoken key names (“Command Shift P”), because glyphs such as ⌘ are read inconsistently; the caps themselves are hidden from screen readers.
- The word between steps is part of the spoken label.
- Caps are not interactive; pressing state is visual only.
- Reduced motion keeps the colour change and removes the sink.
モーション
Press: 140ms spring sink by the skirt height with a colour fade.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
Kbd.jsxkbd.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。



