@keybindy/react
Version:
Keybindy for React: Simple, scoped keyboard shortcuts that require little setup. designed to smoothly blend in with your React applications, allowing for robust keybinding functionality without the overhead.
73 lines (72 loc) • 2.37 kB
TypeScript
import ShortcutManager from '@keybindy/core';
import type { Keys, Shortcut, ShortcutHandler, ShortcutOptions } from '@keybindy/core';
/**
* React hook to manage keyboard shortcuts using a shared instance of `ShortcutManager`.
* Automatically cleans up shortcuts registered by the component on unmount.
*
* @param {Object} config - Configuration object.
* @param {boolean} [config.logs=false] - Whether to enable debug logs in the console.
*
* @returns {Object} Object containing shortcut management methods.
*
* @example
* const {
* register,
* setScope,
* getCheatSheet,
* } = useKeybindy({ logs: true });
*
* useEffect(() => {
* register(['ctrl', 's'], () => save(), {
* scope: 'editor',
* data: { description: 'Save document' }
* });
* setScope('editor');
* }, []);
*/
export declare function useKeybindy({ logs, onShortcutFired, }?: {
logs?: boolean;
onShortcutFired?: (info: Shortcut) => void;
}): {
register: (keys: Keys[] | Keys[][], handler: ShortcutHandler, options?: ShortcutOptions) => void;
unregister: (keys: Keys[], scope?: string) => void;
enable: (keys: Keys[], scope?: string) => void;
disable: (keys: Keys[], scope?: string) => void;
toggle: (keys: Keys[], scope?: string) => void;
setScope: (scope: string) => void;
getCheatSheet: (scope?: string) => {
keys: string[];
}[] | undefined;
destroy: () => void;
getScopeInfo: (scope?: string) => {
shortcuts: {
keys: string[];
id: string;
enabled: boolean;
data?: Record<string, string>;
}[];
isActive?: boolean;
} | Record<string, {
shortcuts: {
keys: string[];
id: string;
enabled: boolean;
data?: Record<string, string>;
}[];
isActive?: boolean;
}> | undefined;
getActiveScope: () => string | undefined;
popScope: () => void;
pushScope: (scope: string) => void;
resetScope: () => void;
getScopes: () => string[] | undefined;
isScopeActive: (scope: string) => boolean | undefined;
onTyping: (callback: (payload: {
key: string;
event: KeyboardEvent;
}) => void) => void;
enableAll: (scope?: string) => void;
clear: () => void;
disableAll: (scope?: string) => void;
manager: ShortcutManager;
};