HotkeysProvider is optional. Without one, every hotkey uses the built-in defaults and belongs to the 'global' scope, staying active whenever its component is mounted.
Add HotkeysProvider to:
Pass defaultOptions to configure defaults for everything nested inside the provider:
import { HotkeysProvider } from '@tanstack/react-hotkeys'
function Root() {
return (
<HotkeysProvider
defaultOptions={{
hotkey: { requireReset: true, timeout: 1500 },
hotkeyRecorder: { idleTimeoutMs: 2000 },
}}
>
<App />
</HotkeysProvider>
)
}Nested providers merge their defaultOptions over their parent's, so a feature subtree can customize defaults without replacing root settings.
Pass your declared app hotkeys, the user's saved overrides, and a change handler to the root provider:
<HotkeysProvider
appHotkeys={appHotkeys}
customHotkeys={savedHotkeys}
onCustomHotkeysChange={saveHotkeys}
>
<App />
</HotkeysProvider>Nested providers inherit appHotkeys and customHotkeys from above, so configure them once at the root.
To pass appHotkeys from a React Server Component, such as Next.js app/layout.tsx, render AppHotkeysProvider instead of HotkeysProvider. See App hotkeys with Server Components.
Apps constantly reuse keys. Escape should close the active dialog rather than the panel behind it, and Mod+S in a comment box should post the comment instead of saving the document. Rather than passing enabled to every hook and coordinating across components, wrap regions of your component tree in scoped providers.
Every useHotkey and useHotkeys call inherits the scope of its nearest scoped ancestor provider. Components outside any scoped provider belong to 'global', which is always active:
import { HotkeysProvider, useHotkey } from '@tanstack/react-hotkeys'
function App() {
return (
<HotkeysProvider>
<Toolbar />
<HotkeysProvider scope="editor">
<Editor />
</HotkeysProvider>
</HotkeysProvider>
)
}
function Editor() {
// Belongs to 'editor'
useHotkey('Mod+B', () => toggleBold())
return <div>...</div>
}Keep two scope rules in mind:
A scope and a target both constrain when a hotkey fires, using two different axes:
| Scope | Target | |
|---|---|---|
| Controls | Whether the hotkey is active | Which DOM events reach the hotkey |
| Driven by | Mounted and enabled providers | Focus location |
| Follows | React tree (including portals) | DOM hierarchy |
| Configured via | HotkeysProvider scope or scope option | target option |
| Best for | Dialogs, panels, tabs, views | Focusable widgets (lists, code editors) |
A hotkey can use both at once:
function Editor() {
const outlineRef = useRef<HTMLElement>(null)
// Active anywhere while the 'editor' scope is active
useHotkey('Mod+B', () => toggleBold())
// Active in 'editor' AND only while focus is inside the outline
useHotkey('Delete', () => deleteHeading(), { target: outlineRef })
return <nav ref={outlineRef} tabIndex={0}>...</nav>
}When multiple active scopes register the same chord on the same target, or a sequence that completes in more than one of them, only the highest-precedence scope runs:
function Page() {
return (
<HotkeysProvider scope="document">
<DocumentView />
</HotkeysProvider>
)
}
function DocumentView() {
useHotkey('Mod+S', () => saveDocument())
return (
<HotkeysProvider scope="comments">
<CommentBox />
</HotkeysProvider>
)
}
function CommentBox() {
// Wins while mounted because 'comments' is nested inside 'document'
useHotkey('Mod+S', () => postComment())
return <textarea />
}When CommentBox unmounts, Mod+S automatically goes back to saving the document.
Element-targeted hotkeys still fire before document hotkeys when focus is inside that element, provided their scope is active. Because registrations in different scopes don't conflict, conflictBehavior won't warn about them.
Pass enabled={false} to suspend a scope without unmounting its tree, which fits tabs, split views, or background drawers that preserve DOM state:
function Folders() {
const [current, setCurrent] = useState('inbox')
return folders.map((name) => (
<HotkeysProvider key={name} scope={name} enabled={current === name}>
<FolderPane name={name} />
</HotkeysProvider>
))
}
function FolderPane({ name }: { name: string }) {
// Only the active folder pane handles Alt+N
useHotkey('Alt+N', () => addItem(name))
return <div>...</div>
}FolderPane stays unaware of which tab is selected. Note two details:
An exclusive scope suspends all other scopes, including 'global', while active. Use it for modals, command palettes, and confirmation dialogs that should own the keyboard:
function EditDialog({ onClose }: { onClose: () => void }) {
return (
<HotkeysProvider scope="edit-dialog" exclusive>
<DialogBody onClose={onClose} />
</HotkeysProvider>
)
}
function DialogBody({ onClose }: { onClose: () => void }) {
useHotkeys([
{ hotkey: 'Escape', callback: onClose },
{ hotkey: 'Mod+S', callback: () => saveAndClose() },
])
return createPortal(<div role="dialog">...</div>, document.body)
}While open, Mod+S saves the dialog rather than the page behind it. Because React context follows the component tree rather than the DOM, the createPortal content still inherits 'edit-dialog'. Nested scopes inside the dialog also keep working.
Pass exclusive={{ allow: [...] }} to keep specific scopes active alongside an exclusive one:
<HotkeysProvider
scope="edit-dialog"
exclusive={{ allow: ['global', 'command-palette'] }}
>
<DialogBody />
</HotkeysProvider>When two exclusive scopes are active at once, such as a confirmation modal opened from inside an edit dialog, the higher-precedence one wins and only its allow list applies. Give each exclusive dialog a unique scope name. Activating the same exclusive scope twice logs a dev warning because both providers would stay active together.
Pass scope to a single hook to break out of the nearest provider:
function Sidebar() {
// Stays in 'global' even when Sidebar is rendered inside a feature scope
useHotkey('Mod+K', () => openPalette(), { scope: 'global' })
}A hotkey can name any scope, though it fires only while a provider or manager.activateScope has that scope active.
An app hotkey can declare a scope directly, which takes precedence over the provider where it registers:
export const appHotkeys = defineAppHotkeys({
'doc.save': { hotkey: 'Mod+S', meta: { name: 'Save' }, scope: 'document' },
'comment.post': { hotkey: 'Mod+S', meta: { name: 'Post comment' }, scope: 'comments' },
'palette.open': { hotkey: 'Mod+K', meta: { name: 'Command palette' }, scope: 'global' },
})If you integrate non-React overlays, such as third-party dialogs, activate their scopes imperatively via Activate a scope.
The devtools group registrations by scope and display which scopes are active, suspended, allowed, or exclusive. To read the same state inside components, see Read scope state.
See the Scopes example for nested, enabled, and exclusive scopes working together.