Skip to content

Shortcut

Renders a keyboard shortcut as key caps. Modifier keys are spelled to match the reader's platform.

tsx
import { Shortcut } from 'neba';

<Shortcut keys="Mod+K" />
<Shortcut keys="Ctrl+Alt+Delete" os="windows" />
<Shortcut keys={['Mod', '+']} />;

Props

PropTypeDefaultDescription
keys * string | string[]The keys. A string is split on `+`: 'Mod+Shift+P'. The array form is for a shortcut whose key is itself a plus
os'auto' | 'mac' | 'windows' | 'linux''auto'Which keyboard to name the modifiers for. `auto` asks the browser; the three explicit values are for pages that have to name a platform rather than the reader's
separatorReactNodeWhat goes between two keys. Omit it for the platform's own convention: a `+` off a Mac, and nothing at all on one
variantshared'solid' | 'outline' | 'text''outline'Weight of the key cap. `outline` is the default: a key cap is a hairline box, which is what it has looked like in every manual ever printed
sizeshared'xs' | 'sm' | 'md' | 'lg' | 'xl''md'Height and type scale of the caps. One step below the control of the same size, exactly as a Chip is
colorshared'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'info''secondary'Semantic colour role. `secondary` by default, because a key cap is chrome rather than an action
densityshared'default' | 'compact''compact'Horizontal padding of the caps only. `compact` by default, because a cap sits inside a line of text
elevationshared0 | 1 | 2 | 30Drop shadow depth. This is a picture of a key, not a key: which is exactly why raising it is tempting and wrong

MenuItem's shortcut prop is the slot this component was written for.

tsx
<MenuItem shortcut={<Shortcut keys="Mod+E" />}>Rename</MenuItem>

Examples

keys

A string is split on +. The array form is only needed when a key is itself a plus. Single-character tokens are capitalised to match what is printed on the cap, and an unrecognised token is drawn exactly as written.

os and Mod

Mod is the token for "the platform's primary modifier": Command () on macOS, Control everywhere else. Unlike the other tokens, which only change spelling, Mod changes which key it names.

os defaults to auto, which asks the browser for the current platform. Name mac, windows or linux for documentation that describes a specific platform.

The same strings are what the library binds: shortcut on CommandPalette and shortcuts on TextField, NumberField and Combobox all read this vocabulary. So do the aliases: Cmd, Command, Meta and Win are one key, and Esc, Return, Opt and Up are spellings of Escape, Enter, Alt and ArrowUp. A key cap and the key that fires it are the same string.

separator and variant

Omit separator and the platform convention is used: macOS runs the symbols together (⇧⌘P), the others join with +. Pass one and that character is used instead.

Key caps sit one step below the control heights, like a Chip, and are set in a monospaced face.

Server rendering

os="auto" depends on the browser, so under SSR the first frame renders the default and switches to the real platform's spelling after hydration: a Mac reader sees Ctrl briefly, then . Name the os on screens where that transition matters.

Accessibility

  • A screen reader cannot read a symbol like as a key name. Every key drawn as a glyph carries a visually hidden name alongside it, so the shortcut is announced as "Command K".

Released under the MIT License