# HotkeysProvider, Default Options, and Hotkey Scopes in Octane

`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:

- Override default options for hotkeys or recorders in part of your app
- Supply [app hotkeys](./app-hotkeys) and load or save custom user bindings
- Group shortcuts into scopes that turn on and off together, such as modals, panels, or tabs

## Set default options

Pass `defaultOptions` to configure defaults for everything nested inside the provider:

```tsx
import { HotkeysProvider } from '@tanstack/octane-hotkeys'

function Root() @{
  <HotkeysProvider
    defaultOptions={{
      hotkey: { requireReset: true, timeout: 1500 },
      hotkeyRecorder: { idleTimeoutMs: 2000 },
    }}
  >
    <App />
  </HotkeysProvider>
}
```

- **`hotkey`.** Sets defaults for every `useHotkey` and `useHotkeys` inside the provider (`requireReset` applies to chords, and `timeout` applies to sequences). Hook-level options always override provider defaults. See [Hotkey options](./hotkeys#hotkey-options).
- **`hotkeyRecorder`.** Sets defaults for every `useHotkeyRecorder` inside the provider. See [Hotkey Recording](./custom-hotkey-recording).
- **`keyboardLayout: false`.** Prevents display formatters from querying the browser's keyboard layout map. See [Label physical keys for the user's keyboard](./formatting-display#label-physical-keys-for-the-users-keyboard).

Nested providers merge their `defaultOptions` over their parent's, so a feature subtree can customize defaults without replacing root settings.

## Provide app hotkeys

Pass your declared [app hotkeys](./app-hotkeys), the user's saved overrides, and a change handler to the root provider:

```tsx
<HotkeysProvider
  appHotkeys={appHotkeys}
  customHotkeys={savedHotkeys}
  onCustomHotkeysChange={saveHotkeys}
>
  <App />
</HotkeysProvider>
```

Nested providers inherit `appHotkeys` and `customHotkeys` from above, so configure them once at the root.

## Group hotkeys into scopes

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`](./hotkeys#enable-and-disable-hotkeys) 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:

```tsx
import { HotkeysProvider, useHotkey } from '@tanstack/octane-hotkeys'

function App() @{
  <HotkeysProvider>
    <Toolbar />
    <HotkeysProvider scope="editor">
      <Editor />
    </HotkeysProvider>
  </HotkeysProvider>
}

function Editor() @{
  // Belongs to 'editor'
  useHotkey('Mod+B', () => toggleBold())
  <div>...</div>
}
```

Keep these scope rules in mind:

- **Providers without `scope` inherit.** A nested provider that only sets `defaultOptions` stays in its parent's scope.
- **Scopes are identified by name.** Two mounted providers with `scope="editor"` share the same `'editor'` scope, which stays active until both unmount. Give independent regions distinct names so they toggle separately.

## Scopes and targets

A scope and a [`target`](./hotkeys#hotkey-targets) both constrain when a hotkey fires, on 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 | Component tree | 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:

```tsx
import { useState } from 'octane'

function Editor() @{
  const [outline, setOutline] = useState<HTMLElement | null>(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: outline })

  <nav ref={setOutline} tabIndex={0}>...</nav>
}
```

## Nested scopes win

When multiple active scopes register the same chord on the same [`target`](./hotkeys#hotkey-targets), or a sequence that completes in more than one of them, only the highest-precedence scope runs:

1. A nested scope beats its parent scopes.
2. Sibling scopes resolve by activation order, so the most recently activated scope wins.

```tsx
function Page() @{
  <HotkeysProvider scope="document">
    <DocumentView />
  </HotkeysProvider>
}

function DocumentView() @{
  useHotkey('Mod+S', () => saveDocument())
  <HotkeysProvider scope="comments">
    <CommentBox />
  </HotkeysProvider>
}

function CommentBox() @{
  // Wins while mounted because 'comments' is nested inside 'document'
  useHotkey('Mod+S', () => postComment())
  <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.

## Turn a scope off with `enabled`

Pass `enabled={false}` to suspend a scope without unmounting its tree, which fits tabs, split views, or background drawers that preserve DOM state:

```tsx
function Folders() @{
  const [current, setCurrent] = useState('inbox')
  @for (const name of folders; key name) {
    <HotkeysProvider 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))
  <div>...</div>
}
```

`FolderPane` stays unaware of which tab is selected. Note these details:

- **`enabled` cascades downward.** Disabling a provider deactivates every nested scope inside it, even if that provider sets no `scope` of its own. A scopeless provider with `enabled={false}` leaves its own hotkeys active because `'global'` cannot be disabled.
- **Inactive scopes stay listed.** Their hotkeys remain visible in devtools and report `'suspended'` in [`useAppHotkeyList`](./cheat-sheet-ui-helpers#list-every-app-hotkey).

## Exclusive scopes

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:

```tsx
function EditDialog({ onClose }: { onClose: () => void }) @{
  <HotkeysProvider scope="edit-dialog" exclusive>
    <DialogBody onClose={onClose} />
  </HotkeysProvider>
}

function DialogBody({ onClose }: { onClose: () => void }) @{
  useHotkeys([
    { hotkey: 'Escape', callback: onClose },
    { hotkey: 'Mod+S', callback: () => saveAndClose() },
  ])
  <div role="dialog">...</div>
}
```

While open, `Mod+S` saves the dialog rather than the page behind it. Nested scopes inside the dialog also keep working.

### Allow other scopes

Pass `exclusive={{ allow: [...] }}` to keep specific scopes active alongside an exclusive one:

```tsx
<HotkeysProvider
  scope="edit-dialog"
  exclusive={{ allow: ['global', 'command-palette'] }}
>
  <DialogBody />
</HotkeysProvider>
```

- Listed scopes still work only if they're mounted and enabled (`'global'` is always active).
- Allowed scopes still obey normal precedence. A newer allowed popover opening over the dialog gets `Escape` first.
- Inline `allow` arrays are compared by value, so a new array on each update doesn't reactivate the scope.

### Stacked exclusive scopes

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.

## Choose a scope for one hotkey

Pass `scope` to a single call to break out of the nearest provider:

```tsx
function Sidebar() @{
  // Stays in 'global' even when Sidebar is rendered inside a feature scope
  useHotkey('Mod+K', () => openPalette(), { scope: 'global' })
  <nav>...</nav>
}
```

A hotkey can name any scope, though it fires only while a provider or `manager.activateScope` has that scope active.

## Scopes and app hotkeys

An [app hotkey](./app-hotkeys) can declare a `scope` directly, which takes precedence over the provider where it registers:

```ts
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' },
})
```

- Declarations without `scope` inherit the provider's scope at registration time.
- **Conflict detection.** `findConflicts` and `setCustomHotkey` allow different named scopes (`document` and `comments`) to share keys. Any app hotkey in `'global'` or without a declared scope conflicts with every other app hotkey using the same binding, so declare `scope` on bindings intended to coexist.
- [`useAppHotkeyList`](./cheat-sheet-ui-helpers#list-every-app-hotkey) marks enabled registrations as `'suspended'` when none of their scopes are active.

## Scopes outside Octane

If you integrate non-Octane overlays, such as third-party dialogs, activate their scopes imperatively via [Activate a scope](./hotkey-manager#activate-a-scope).

## Suspend character key shortcuts

Pass `suspendCharacterKeys` to `HotkeysProvider` to pause document-level character key shortcuts, such as `K` or `['G', 'H']`, across the app while the prop is `true`. Suspended hotkeys report the status `'suspended'`, like hotkeys in an inactive scope, and the prop applies even when `enabled` is `false`. Wire it to a user setting as shown in [Accessibility](./accessibility#let-users-turn-off-character-key-shortcuts).

## Inspect scopes

The [devtools](../../../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](./hotkey-manager#read-scope-state).

See the [Scopes example](../examples/scopes) for nested, `enabled`, and `exclusive` scopes working together.
