Skip to main content
Mantı UI logoMantı UIv0.13.0
Studio

useShortcut

useShortcut works with Manti components or any other element.

mod+i

Import

import { useShortcut, useShortcuts } from '@manti-ui/react/shortcut';

The hooks are also exported from @manti-ui/react.

One shortcut

useShortcut('mod+k', () => setOpen(true));

Pass a ref as scope to fire only while focus is inside a region:

Editor regionClick to focus, then press ⌘S / Ctrl+S to save.Not saved

Several shortcuts

useShortcuts({
  'mod+k': openSearch,
  'mod+/': toggleHelp,
  'g d': goToDashboard,
});
mod+bmod+/g d
Press a shortcut…

Form fields

Shortcuts are ignored in input, textarea, select, and contenteditable elements by default. Set enableOnFormElements to opt in.

Submitted ×0

Syntax

FormExample
Modifiermod+k, ctrl+shift+p
Keyescape, enter, /
Sequenceg d

mod means Command on macOS and Control elsewhere.

API

useShortcut(combo, handler, options?): void
useShortcuts(map, options?): void
OptionDefaultPurpose
scope'global'Window or a ref-scoped subtree
preventDefaulttrueStop the browser's matching action
enabledtrueToggle the binding
enableOnFormElementsfalseAllow shortcuts while typing
stopPropagationfalseStop event propagation
sequenceTimeout1000Sequence timeout in milliseconds

The implementation is SSR-safe and attaches listeners only in an effect.

Last updated

Built end-to-end with Manti UI components and design tokens. Manti UI is a framework-agnostic design system on Zag.js.

GitHub