# Kbd Display a keyboard key, shortcut or sequence within text and controls. [Interactive guide](/docs/components/kbd) · Forge 3.2.0 Use the API for your installed version. Run `npx --no-install wss3-forge api --json` inside the project. ## Implementation guidance ### When to use it Use Kbd for a visible key label and KbdGroup for keys pressed together. ### How to compose it Register the shortcut on the owning action and give symbolic keys a spoken name when they convey information. ### What to verify Try the actual shortcut in its intended context and verify it preserves normal editing and activation. ## Usage ```tsx import { Kbd, KbdGroup } from 'wss3-forge' ``` - Use one Kbd per physical key. Group simultaneous keys with KbdGroup and describe sequential presses in reading order. - Give symbolic keys a spoken name. Hide decorative key hints inside buttons when their accessible names already describe the action. - Kbd displays a label. Bind the shortcut on the owning control and publish it with aria-keyshortcuts. The search example selects its text while the field has focus. Save and Undo accept their letter shortcuts while either button has focus. - Use Shortcut from the main Forge entry for a compact chord with automatic plus signs. ```tsx CtrlB ``` ## Runnable examples ### Modifier keys ```tsx import { createContext, useContext } from 'react' import * as DefaultKeys from 'wss3-forge' import { Direction, VStack } from 'wss3-forge' type KeyParts = Pick const KeyLibrary = createContext(DefaultKeys) function KeyExamples({ rtl = false }: { rtl?: boolean }) { const { Kbd, KbdGroup } = useContext(KeyLibrary) return ⌘⇧⌥⌃ CtrlB } export default function Example() { return () } ``` ### Group ```tsx import { createContext, useContext } from 'react' import * as DefaultKeys from 'wss3-forge' import { Box, Text } from 'wss3-forge' type KeyParts = Pick const KeyLibrary = createContext(DefaultKeys) function Group() { const { Kbd, KbdGroup } = useContext(KeyLibrary) return Press ShiftTab to focus the previous control. Press Enter to activate a focused button. } export default function Example() { return () } ``` ### Button ```tsx import { createContext, useContext, useState } from 'react' import * as DefaultKeys from 'wss3-forge' import { Button } from 'wss3-forge' type KeyParts = Pick const KeyLibrary = createContext(DefaultKeys) function AcceptKey() { const { Kbd } = useContext(KeyLibrary) const [accepted, setAccepted] = useState(false) return } export default function Example() { return () } ``` ### Tooltip ```tsx import { createContext, useContext, useState } from 'react' import * as DefaultKeys from 'wss3-forge' import { Button, HStack, Text, Tooltip, VStack } from 'wss3-forge' type KeyParts = Pick const KeyLibrary = createContext(DefaultKeys) function TooltipKeys() { const { Kbd } = useContext(KeyLibrary) const [status, setStatus] = useState('Draft') return { if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || event.nativeEvent.isComposing) return const key = event.key.toLowerCase() if (key === 's' || key === 'u') { event.preventDefault() event.stopPropagation() setStatus(key === 's' ? 'Saved' : 'Undone') } }}> Save draft S}> Undo save U}> {status} } export default function Example() { return () } ``` ### Input group ```tsx import { createContext, useContext } from 'react' import { Search16Regular } from '@fluentui/react-icons' import * as DefaultKeys from 'wss3-forge' import { Box, InputGroup, InputGroupAddon, InputGroupInput } from 'wss3-forge' type KeyParts = Pick const KeyLibrary = createContext(DefaultKeys) function ShortcutInput() { const { Kbd, KbdGroup } = useContext(KeyLibrary) const isApple = typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform) return { const modifier = isApple ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey if (modifier && !event.altKey && !event.shiftKey && !event.nativeEvent.isComposing && event.key.toLowerCase() === 'k') { event.preventDefault() event.stopPropagation() event.currentTarget.select() } }} /> } export default function Example() { return () } ``` ### RTL ```tsx import { createContext, useContext } from 'react' import * as DefaultKeys from 'wss3-forge' import { Direction, VStack } from 'wss3-forge' type KeyParts = Pick const KeyLibrary = createContext(DefaultKeys) function KeyExamples({ rtl = false }: { rtl?: boolean }) { const { Kbd, KbdGroup } = useContext(KeyLibrary) return ⌘⇧⌥⌃ CtrlB } export default function Example() { return () } ``` ### Appearance ```tsx import { createContext, useContext } from 'react' import * as DefaultKeys from 'wss3-forge' import { HStack, Text, VStack } from 'wss3-forge' type KeyParts = Pick const KeyLibrary = createContext(DefaultKeys) function Appearance() { const { Kbd } = useContext(KeyLibrary) return {(['default', 'outline'] as const).map(variant => EscEsc {variant === 'outline' ? 'Outline' : 'Default'} )} } export default function Example() { return () } ``` ### Forge shortcut ```tsx import { Shortcut } from 'wss3-forge' export default function Example() { return () } ``` ## Primitive libraries ### Base UI Both parts render native kbd elements and accept native attributes and refs. ```sh npm install wss3-forge @base-ui/react ``` ```tsx import { Kbd, KbdGroup } from 'wss3-forge/base-ui' import { Direction, VStack } from 'wss3-forge' function KeyExamples({ rtl = false }: { rtl?: boolean }) { return ⌘⇧⌥⌃ CtrlB } export default function Example() { return () } ``` #### Group ```tsx import { Kbd, KbdGroup } from 'wss3-forge/base-ui' import { Box, Text } from 'wss3-forge' function Group() { return Press ShiftTab to focus the previous control. Press Enter to activate a focused button. } export default function Example() { return () } ``` #### Button ```tsx import { Kbd } from 'wss3-forge/base-ui' import { useState } from 'react' import { Button } from 'wss3-forge' function AcceptKey() { const [accepted, setAccepted] = useState(false) return } export default function Example() { return () } ``` #### Tooltip ```tsx import { Kbd } from 'wss3-forge/base-ui' import { useState } from 'react' import { Button, HStack, Text, Tooltip, VStack } from 'wss3-forge' function TooltipKeys() { const [status, setStatus] = useState('Draft') return { if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || event.nativeEvent.isComposing) return const key = event.key.toLowerCase() if (key === 's' || key === 'u') { event.preventDefault() event.stopPropagation() setStatus(key === 's' ? 'Saved' : 'Undone') } }}> Save draft S}> Undo save U}> {status} } export default function Example() { return () } ``` #### Input group ```tsx import { Kbd, KbdGroup } from 'wss3-forge/base-ui' import { Search16Regular } from '@fluentui/react-icons' import { Box, InputGroup, InputGroupAddon, InputGroupInput } from 'wss3-forge' function ShortcutInput() { const isApple = typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform) return { const modifier = isApple ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey if (modifier && !event.altKey && !event.shiftKey && !event.nativeEvent.isComposing && event.key.toLowerCase() === 'k') { event.preventDefault() event.stopPropagation() event.currentTarget.select() } }} /> } export default function Example() { return () } ``` #### RTL ```tsx import { Kbd, KbdGroup } from 'wss3-forge/base-ui' import { Direction, VStack } from 'wss3-forge' function KeyExamples({ rtl = false }: { rtl?: boolean }) { return ⌘⇧⌥⌃ CtrlB } export default function Example() { return () } ``` #### Appearance ```tsx import { Kbd } from 'wss3-forge/base-ui' import { HStack, Text, VStack } from 'wss3-forge' function Appearance() { return {(['default', 'outline'] as const).map(variant => EscEsc {variant === 'outline' ? 'Outline' : 'Default'} )} } export default function Example() { return () } ``` ### Radix The Radix entry shares the native keyboard markup and Forge appearance props. ```sh npm install wss3-forge radix-ui ``` ```tsx import { Kbd, KbdGroup } from 'wss3-forge/radix' import { Direction, VStack } from 'wss3-forge' function KeyExamples({ rtl = false }: { rtl?: boolean }) { return ⌘⇧⌥⌃ CtrlB } export default function Example() { return () } ``` #### Group ```tsx import { Kbd, KbdGroup } from 'wss3-forge/radix' import { Box, Text } from 'wss3-forge' function Group() { return Press ShiftTab to focus the previous control. Press Enter to activate a focused button. } export default function Example() { return () } ``` #### Button ```tsx import { Kbd } from 'wss3-forge/radix' import { useState } from 'react' import { Button } from 'wss3-forge' function AcceptKey() { const [accepted, setAccepted] = useState(false) return } export default function Example() { return () } ``` #### Tooltip ```tsx import { Kbd } from 'wss3-forge/radix' import { useState } from 'react' import { Button, HStack, Text, Tooltip, VStack } from 'wss3-forge' function TooltipKeys() { const [status, setStatus] = useState('Draft') return { if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || event.nativeEvent.isComposing) return const key = event.key.toLowerCase() if (key === 's' || key === 'u') { event.preventDefault() event.stopPropagation() setStatus(key === 's' ? 'Saved' : 'Undone') } }}> Save draft S}> Undo save U}> {status} } export default function Example() { return () } ``` #### Input group ```tsx import { Kbd, KbdGroup } from 'wss3-forge/radix' import { Search16Regular } from '@fluentui/react-icons' import { Box, InputGroup, InputGroupAddon, InputGroupInput } from 'wss3-forge' function ShortcutInput() { const isApple = typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform) return { const modifier = isApple ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey if (modifier && !event.altKey && !event.shiftKey && !event.nativeEvent.isComposing && event.key.toLowerCase() === 'k') { event.preventDefault() event.stopPropagation() event.currentTarget.select() } }} /> } export default function Example() { return () } ``` #### RTL ```tsx import { Kbd, KbdGroup } from 'wss3-forge/radix' import { Direction, VStack } from 'wss3-forge' function KeyExamples({ rtl = false }: { rtl?: boolean }) { return ⌘⇧⌥⌃ CtrlB } export default function Example() { return () } ``` #### Appearance ```tsx import { Kbd } from 'wss3-forge/radix' import { HStack, Text, VStack } from 'wss3-forge' function Appearance() { return {(['default', 'outline'] as const).map(variant => EscEsc {variant === 'outline' ? 'Outline' : 'Default'} )} } export default function Example() { return () } ``` ### React Aria Both parts use React Aria Keyboard. Set slot="keyboard" on the group inside a MenuItem to associate the shortcut with its description. ```sh npm install wss3-forge react-aria-components ``` ```tsx import { Kbd, KbdGroup } from 'wss3-forge/react-aria' import { Direction, VStack } from 'wss3-forge' function KeyExamples({ rtl = false }: { rtl?: boolean }) { return ⌘⇧⌥⌃ CtrlB } export default function Example() { return () } ``` #### Group ```tsx import { Kbd, KbdGroup } from 'wss3-forge/react-aria' import { Box, Text } from 'wss3-forge' function Group() { return Press ShiftTab to focus the previous control. Press Enter to activate a focused button. } export default function Example() { return () } ``` #### Button ```tsx import { Kbd } from 'wss3-forge/react-aria' import { useState } from 'react' import { Button } from 'wss3-forge' function AcceptKey() { const [accepted, setAccepted] = useState(false) return } export default function Example() { return () } ``` #### Tooltip ```tsx import { Kbd } from 'wss3-forge/react-aria' import { useState } from 'react' import { Button, HStack, Text, Tooltip, VStack } from 'wss3-forge' function TooltipKeys() { const [status, setStatus] = useState('Draft') return { if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || event.nativeEvent.isComposing) return const key = event.key.toLowerCase() if (key === 's' || key === 'u') { event.preventDefault() event.stopPropagation() setStatus(key === 's' ? 'Saved' : 'Undone') } }}> Save draft S}> Undo save U}> {status} } export default function Example() { return () } ``` #### Input group ```tsx import { Kbd, KbdGroup } from 'wss3-forge/react-aria' import { Search16Regular } from '@fluentui/react-icons' import { Box, InputGroup, InputGroupAddon, InputGroupInput } from 'wss3-forge' function ShortcutInput() { const isApple = typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform) return { const modifier = isApple ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey if (modifier && !event.altKey && !event.shiftKey && !event.nativeEvent.isComposing && event.key.toLowerCase() === 'k') { event.preventDefault() event.stopPropagation() event.currentTarget.select() } }} /> } export default function Example() { return () } ``` #### RTL ```tsx import { Kbd, KbdGroup } from 'wss3-forge/react-aria' import { Direction, VStack } from 'wss3-forge' function KeyExamples({ rtl = false }: { rtl?: boolean }) { return ⌘⇧⌥⌃ CtrlB } export default function Example() { return () } ``` #### Appearance ```tsx import { Kbd } from 'wss3-forge/react-aria' import { HStack, Text, VStack } from 'wss3-forge' function Appearance() { return {(['default', 'outline'] as const).map(variant => EscEsc {variant === 'outline' ? 'Outline' : 'Default'} )} } export default function Example() { return () } ``` ## Root package API ### Kbd Display a keyboard key, shortcut or sequence within text and controls. Props type: `KbdProps` Native attributes: AriaAttributes, DOMAttributes, HTMLAttributes **Composition exports** - `KbdGroup` (named export) - `size?` — `'sm' | 'md'` Default `'md'`. Options `"sm"`, `"md"`. Compact 16px or standard 20px key height. - `variant?` — `'default' | 'outline'` Default `'default'`. Options `"default"`, `"outline"`. A neutral filled or outlined key. - `children?` — `ReactNode` The key name, symbol or icon. KbdGroup accepts multiple keys. - `ref?` — `React.LegacyRef | undefined` ### KbdGroup Props type: `HTMLAttributes` Native attributes: AriaAttributes, DOMAttributes, HTMLAttributes - `ref?` — `React.LegacyRef | undefined` ### Text Set the size, weight and color of a paragraph, caption or inline phrase in a Forge interface. Props type: `TextProps` Native attributes: AriaAttributes, DOMAttributes, HTMLAttributes - `children` — `ReactNode` - `as?` — `'p' | 'span' | 'div' | 'label'` Default `'p'`. Options `"label"`, `"div"`, `"p"`, `"span"`. Rendered HTML element - `size?` — `'xs' | 'sm' | 'md' | 'lg'` Default `'md'`. Options `"sm"`, `"md"`, `"lg"`, `"xs"`. Font size and corresponding line height - `weight?` — `'normal' | 'medium' | 'semibold' | 'bold'` Default `'normal'`. Options `"medium"`, `"bold"`, `"normal"`, `"semibold"`. Font weight - `color?` — `'primary' | 'secondary' | 'muted' | 'brand' | 'success' | 'warning' | 'error' | string` Default `'primary'`. Use a semantic color such as secondary, muted or error, or supply a CSS color. - `align?` — `'left' | 'center' | 'right' | 'justify'` Options `"center"`, `"left"`, `"right"`, `"justify"`. Horizontal alignment within the available width - `truncate?` — `boolean | number` Use true for a single line or a positive integer for a line limit. - `italic?` — `boolean` Italic style - `underline?` — `boolean` Underline decoration - `strikethrough?` — `boolean` Line-through decoration - `uppercase?` — `boolean` Uppercase with letter spacing - `className?` — `string` - `style?` — `React.CSSProperties` ### Shortcut Props type: `ShortcutProps` - `keys` — `string[]` - `size?` — `'sm' | 'md'` Default `'md'`. Options `"sm"`, `"md"`. - `className?` — `string` - `style?` — `React.CSSProperties` ## Related types ```ts export interface KbdProps extends HTMLAttributes { size?: 'sm' | 'md' variant?: 'default' | 'outline' } interface TextProps extends HTMLAttributes { children: ReactNode as?: 'p' | 'span' | 'div' | 'label' size?: 'xs' | 'sm' | 'md' | 'lg' weight?: 'normal' | 'medium' | 'semibold' | 'bold' color?: 'primary' | 'secondary' | 'muted' | 'brand' | 'success' | 'warning' | 'error' | string align?: 'left' | 'center' | 'right' | 'justify' truncate?: boolean | number // true for single line, number for multi-line italic?: boolean underline?: boolean strikethrough?: boolean uppercase?: boolean className?: string style?: React.CSSProperties } interface ShortcutProps { keys: string[] size?: 'sm' | 'md' className?: string style?: React.CSSProperties } ``` ## Optional primitive contracts - [wss3-forge/base-ui · Kbd contract](/ai/skill/primitive-api/Kbd.json) - [wss3-forge/radix · Kbd contract](/ai/skill/primitive-api/Kbd.json) - [wss3-forge/react-aria · Kbd contract](/ai/skill/primitive-api/Kbd.json)