Kbd
@codeui/solid v0.3.0
A keycap that shows a key or a keyboard shortcut.
When to use
Use Kbd to show one key the user can press: in running text ("Press Esc"), next to a menu item, or inside a button that has a shortcut. It renders a native kbd element in the mono font. One Kbd holds one key, so a shortcut such as ⇧⌘E is three Kbd elements side by side.
When to choose another component
- Use
Badgefor a status or a tag.Kbdalways means "a key on the keyboard". - Use the
rightSlotofDropdownMenuItemas the place for a menu shortcut, and putKbdelements inside it. - Use a code element in your own markup for code, paths and values.
Kbdadds a keycap shape that code does not need.
Live demo
import { Kbd, Stack, Text } from "@codeui/solid";
export default function KbdBasic() {
return (
<Stack direction="row" gap="2" align="center">
<Text tone="muted">Press</Text>
<Kbd>Esc</Kbd>
<Text tone="muted">to close the export panel.</Text>
</Stack>
);
}Anatomy
Tap a part to outline it in the demo.
- 01Kbd
kbdclasses.root
| Export | Renders | classes key | data-cui | Role |
|---|---|---|---|---|
Kbd | kbd | root | kbd | None |
The root also renders data-size (sm or md) and data-variant (keycap or hint), with the defaults sm and keycap.
Variants
Size
import { Kbd, Stack } from "@codeui/solid";
export default function KbdSize() {
return (
<Stack direction="row" gap="6" align="center">
<Stack direction="row" gap="1" align="center">
<Kbd size="sm">⌘</Kbd>
<Kbd size="sm">K</Kbd>
</Stack>
<Stack direction="row" gap="1" align="center">
<Kbd size="md">⌘</Kbd>
<Kbd size="md">K</Kbd>
</Stack>
</Stack>
);
}size sets the height, the minimum width and the font size.
size | Minimum height and width | Font size |
|---|---|---|
sm | 16px (--cui-space-4) | 11px (--cui-font-size-badge) |
md | 20px (--cui-space-5) | 12px (--cui-font-size-xs) |
The default is sm. Use md next to md text or inside a larger control.
Variant
import { Kbd, Stack } from "@codeui/solid";
export default function KbdVariant() {
return (
<Stack direction="row" gap="6" align="center">
<Stack direction="row" gap="1" align="center">
<Kbd variant="keycap">⇧</Kbd>
<Kbd variant="keycap">⌘</Kbd>
<Kbd variant="keycap">E</Kbd>
</Stack>
<Stack direction="row" gap="1" align="center">
<Kbd variant="hint">⇧</Kbd>
<Kbd variant="hint">⌘</Kbd>
<Kbd variant="hint">E</Kbd>
</Stack>
</Stack>
);
}keycap draws a filled key with a divider border. hint drops the border and uses the hint text color, so the key reads as secondary. Use hint where the shortcut sits next to a label that matters more, such as inside a button or a menu item. The default is keycap.
Single keys
import { Kbd, Stack } from "@codeui/solid";
import { For } from "solid-js";
const keys = ["Esc", "Enter", "Tab", "Space", "⌫", "↑", "↓", "←", "→", "/", "?"];
export default function KbdKeys() {
return (
<Stack direction="row" gap="1.5" wrap>
<For each={keys}>{key => <Kbd>{key}</Kbd>}</For>
</Stack>
);
}Write the key as the user sees it on the keyboard. Use words for named keys (Esc, Enter, Tab) and symbols for arrows and modifiers. Inkstone uses ⌘ and → in rendered text.
Chords and sequences
import { Kbd, Stack, Text } from "@codeui/solid";
export default function KbdChord() {
return (
<Stack gap="3">
<Stack direction="row" gap="4" align="center" justify="between">
<Text>Export as PNG</Text>
<Stack as="kbd" direction="row" gap="1" align="center">
<Kbd>⌘</Kbd>
<Kbd>⇧</Kbd>
<Kbd>E</Kbd>
</Stack>
</Stack>
<Stack direction="row" gap="4" align="center" justify="between">
<Text>Go to the snippet library</Text>
<Stack direction="row" gap="1" align="center">
<Kbd>G</Kbd>
<Text size="xs" tone="hint">
then
</Text>
<Kbd>L</Kbd>
</Stack>
</Stack>
</Stack>
);
}For a chord, render one Kbd per key inside an outer kbd. The HTML specification uses a kbd nested in a kbd for a key combination. <Stack as="kbd" direction="row" gap="1"> gives you that outer element with the gap. List modifiers in the platform order. On macOS that order is ⌃ ⌥ ⇧ ⌘.
For a sequence, where the user presses one key and then another, put a word such as "then" between the keys.
Platform modifiers
import { Kbd, Stack, Text } from "@codeui/solid";
import { createSignal, onSettled, Show } from "solid-js";
/** True on macOS, iOS and iPadOS. Reads the browser, so call it only after mount. */
function isApplePlatform() {
return /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent);
}
export default function KbdPlatform() {
// The server cannot know the platform. Render Ctrl first, then switch after hydration.
const [apple, setApple] = createSignal(false);
onSettled(() => { setApple(isApplePlatform()); });
return (
<Stack direction="row" gap="4" align="center" justify="between">
<Text>Copy snippet</Text>
<Stack as="kbd" direction="row" gap="1" align="center">
<Show when={apple()} fallback={<Kbd>Ctrl</Kbd>}>
<Kbd>⌘</Kbd>
</Show>
<Kbd>C</Kbd>
</Stack>
</Stack>
);
}macOS shows ⌘ where Windows and Linux show Ctrl. Kbd does not detect the platform, so you pick the label. The demo renders Ctrl on the server and in the first client render, then switches to ⌘ in onMount on Apple platforms. Reading navigator during render would give the server and the client different markup and break hydration.
Ready API
Kbd
Kbd renders a kbd element with the kit keycap class.
Prop
Type
Every other prop goes to the kbd element. Kbd wraps no Kobalte primitive, so there is no inherited table. @codeui/solid exports Kbd and KbdProps.
Styled parts API
Import the parts from @codeui/solid/kbd/parts.
Root
Root renders the same kbd element as Kbd. It takes size, variant, class and every kbd attribute, but not classes. RootProps is JSX.IntrinsicElements["kbd"] plus KbdVariants. The entry also exports kbdParts and the types KbdPart, KbdClasses, KbdSize, KbdVariant and KbdVariants.
Prop
Type
Styles export
kbdStyles
Call kbdStyles.root() for the base class. On custom markup, set data-size and data-variant, such as data-size="sm" data-variant="keycap". kbdStyles and KbdStyles are exported from @codeui/solid/kbd/parts.
Accessibility
Kbd renders the native kbd element, which has no ARIA role. Screen readers read its text. They read symbols such as ⌘ and ⇧ inconsistently, so do not rely on the keycap to announce a shortcut.
Put the shortcut on the control it triggers instead. Set aria-keyshortcuts on the button or menu item, with key names from the ARIA specification, such as Meta+K or Shift+Meta+S. Then set aria-hidden="true" on the visual keycaps, so the accessible name stays "Search snippets" and not "Search snippets ⌘ K".
Kbd is not focusable and has no keyboard behavior. It only describes a key.
SSR notes
Kbd renders completely on the server. It has no effects, IDs or portals. Platform detection is your code, and it must run on the client after hydration, as the platform demo shows.
Theming hooks
| Variable | Use |
|---|---|
--cui-font-family-mono | Font family |
--cui-font-weight-medium | Weight |
--cui-font-line-height-compact | Line height |
--cui-font-size-badge, --cui-font-size-xs | Font size for sm and md |
--cui-space-4, --cui-space-5 | Minimum size for sm and md |
--cui-space-1, --cui-space-1-5 | Inline padding for sm and md |
--cui-radius-badge | Corner radius |
--cui-action-neutral-base | Fill |
--cui-action-neutral-text | Text color of keycap |
--cui-text-hint | Text color of hint |
--cui-border-width, --cui-border-divider | Border of keycap |
The defaults are in Tokens.
Composition examples
In a button
import { Button, Kbd, Stack } from "@codeui/solid";
export default function KbdInButton() {
return (
<Button variant="secondary" aria-keyshortcuts="Meta+K">
Search snippets
<Stack as="kbd" direction="row" gap="0.5" aria-hidden="true">
<Kbd variant="hint">⌘</Kbd>
<Kbd variant="hint">K</Kbd>
</Stack>
</Button>
);
}Button lays out its children with an 8px gap, so the keycaps sit after the label with no extra markup. The hint variant keeps them quieter than the label.
In a menu
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuSeparator,
DropdownMenuTrigger,
Kbd,
Stack,
} from "@codeui/solid";
export default function KbdInMenu() {
return (
<DropdownMenu>
<DropdownMenuTrigger as={Button} variant="secondary">
Export
</DropdownMenuTrigger>
<DropdownMenuPortal>
<DropdownMenuContent>
<DropdownMenuItem
aria-keyshortcuts="Meta+S"
rightSlot={
<Stack as="kbd" direction="row" gap="0.5" aria-hidden="true">
<Kbd variant="hint">⌘</Kbd>
<Kbd variant="hint">S</Kbd>
</Stack>
}
>
Save as PNG
</DropdownMenuItem>
<DropdownMenuItem
aria-keyshortcuts="Shift+Meta+S"
rightSlot={
<Stack as="kbd" direction="row" gap="0.5" aria-hidden="true">
<Kbd variant="hint">⇧</Kbd>
<Kbd variant="hint">⌘</Kbd>
<Kbd variant="hint">S</Kbd>
</Stack>
}
>
Save as SVG
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem>Copy link</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenuPortal>
</DropdownMenu>
);
}Pass the keycaps to rightSlot, which aligns them to the end of the item. Set aria-keyshortcuts on each item. For a searchable list of commands, see Run commands.
Related
DropdownMenushows shortcuts inrightSlot.CommandMenuis the place to list every shortcut in an app.Textsets the running text around a key.