Skip to main content

Shortcut

Component for displaying keyboard shortcuts and key combinations. Renders a semantic `<kbd>` element and supports interactive and static variants.

Import

import { Shortcut } from '@vortexlabs/vortex';
import type { ShortcutProps } from '@vortexlabs/vortex';

Display

<Shortcut>⌘K</Shortcut>

Allied components

Other links

⌘K
<Shortcut
  type="outline"
  size="md"
  active={false}
>
  ⌘K
</Shortcut>

Features

  • Supports active state.
  • Customizable for various contexts.
  • Includes useShortcuts hook for managing keyboard shortcuts.
  • Can be used in forms, dialogs, and toolbars.

Type

Shortcut has two visual styles. The "outline" type renders with a border and background. The "plain" type renders without any visual container.

⌘K⌘K
<Shortcut type="outline">⌘K</Shortcut>
<Shortcut type="plain">⌘K</Shortcut>

Sizes

Shortcut is available in two sizes: "sm" and "md". The default size is "md".

⌘K⌘K⌘K⌘K
<Shortcut size="sm">⌘K</Shortcut>
<Shortcut size="md">⌘K</Shortcut>
<Shortcut size="sm" type="plain">⌘K</Shortcut>
<Shortcut size="md" type="plain">⌘K</Shortcut>

Interactive

Providing an onClick handler makes the shortcut interactive — it renders as a button with hover and focus states. Use ariaLabel to provide an accessible name when the shortcut is clickable.

⌘K
<Shortcut onClick={() => {}} ariaLabel="Activate shortcut">⌘K</Shortcut>

Active

The active prop renders the shortcut in its pressed state. Only available on the "outline" type.

⌘K⌘K
<Shortcut active>⌘K</Shortcut>
<Shortcut>⌘K</Shortcut>

Hooks

Use the useShortcuts hook to bind keyboard shortcuts and drive the active prop based on whether a key combination is currently pressed. checkShortcutState returns true while the combo is held.

⌘Kactive while ⌘K is held
import { useShortcuts } from "@vortexlabs/vortex";

const { checkShortcutState } = useShortcuts(
{
"Mod+k": (e) => {
e?.preventDefault();
// handle shortcut
},
},
[],
{ preventDefault: true },
);

<Shortcut active={checkShortcutState("Mod+k")}>⌘K</Shortcut>

Accessibility

Shortcut renders a semantic <kbd> element to convey keyboard shortcut meaning to assistive technologies.

Description
Renders as a semantic <kbd> element.
When onClick is provided, the component renders as a button. Use ariaLabel to provide an accessible name.
ariaLabel and onClick are not available on the "plain" type.