UNPKG

@tanstack/react-hotkeys

Version:

React adapter for TanStack Hotkeys

84 lines • 3.49 kB
import { UseHotkeyCommonOptions } from "./useHotkey.js"; import { AppHotkeys, CustomHotkeys, HotkeyPlatform } from "@tanstack/hotkeys"; import { HotkeyRecorderDefaults, HotkeysContextValue, HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter"; import { ReactElement, ReactNode } from "react"; export interface HotkeysProviderOptions { /** * The user's platform, for labels from `useFormatForDisplay`, hotkeys, * recorders, and app hotkey checks such as conflicts. Defaults to detection. * When server rendering, set it from the request with * `detectPlatform(userAgent)`. */ platform?: HotkeyPlatform; /** * Whether labels from `useFormatForDisplay` follow the user's keyboard * layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout * loads from the browser when the first label mounts. Only Chromium * browsers report one. Set `false` to never ask the browser for it. * Default: `true`. */ keyboardLayout?: boolean; /** * Defaults for every hotkey registered by `useHotkey` and `useHotkeys`. * `requireReset` applies to chords and `timeout` applies to sequences. */ hotkey?: UseHotkeyCommonOptions; /** Defaults for every `useHotkeyRecorder`. */ hotkeyRecorder?: HotkeyRecorderDefaults; } type HotkeysContextValue$1 = HotkeysContextValue<HotkeysProviderOptions>; export interface HotkeysProviderProps<T extends AppHotkeys = AppHotkeys> extends HotkeysProviderScopeProps { children: ReactNode; /** * Every app hotkey, from `defineAppHotkeys`. Listed by `useAppHotkeyList` * whether or not their components have mounted. It can start as `undefined` * and arrive later, such as from a lazy import. */ appHotkeys?: T; /** * The user's saved custom hotkeys. When the value changes, such as after * saved data loads or the user signs in, it replaces the current custom * hotkeys. `undefined` leaves them unchanged; `null` clears them. Unknown * ids and invalid entries are ignored. */ customHotkeys?: CustomHotkeys<T> | null; /** Called with the custom hotkeys after every change, ready to save. */ onCustomHotkeysChange?: (customHotkeys: CustomHotkeys<T>) => void; /** Merged with the parent provider's defaults. */ defaultOptions?: HotkeysProviderOptions; } /** * Provides default options, app hotkeys, and the user's custom hotkeys. * * Nest providers to create hotkey scopes. A nested provider inherits app * hotkeys and merges default options. With `scope`, its hotkeys fire only * while it is mounted and enabled; with `exclusive`, only its hotkeys fire. * * @example * ```tsx * <HotkeysProvider * appHotkeys={appHotkeys} * customHotkeys={JSON.parse(localStorage.getItem('hotkeys') ?? '{}')} * onCustomHotkeysChange={(custom) => * localStorage.setItem('hotkeys', JSON.stringify(custom)) * } * > * <App /> * </HotkeysProvider> * ``` * * @example * ```tsx * function EditDialog() { * return ( * <HotkeysProvider scope="edit-dialog" exclusive={{ allow: ['global'] }}> * <DialogBody /> * </HotkeysProvider> * ) * } * ``` */ export declare function HotkeysProvider<T extends AppHotkeys = AppHotkeys>({ children, appHotkeys, customHotkeys, onCustomHotkeysChange, defaultOptions, scope, exclusive, enabled }: HotkeysProviderProps<T>): ReactElement; export declare function useHotkeysContext(): HotkeysContextValue$1 | null; export declare function useDefaultHotkeysOptions(): HotkeysProviderOptions; export {};