⌨️
React Hotkeys Hook
React Hook for Mousetrap
Usage
Install via NPM:
yarn add reakeys
Then in your component, just add the useHotkeys
hook and specify your keys like:
import React, { FC } from 'react';
import { useHotkeys } from 'reakeys';
export const YourComponent: FC = () => {
useHotkeys([
{
name: 'Dashboard',
keys: 'mod+shift+d',
category: 'Navigation',
callback: event => {
event.preventDefault();
history.push('/dashboard');
}
}
]);
};
Below are the options you can set in the hook array:
type HotkeyShortcuts = {
name: string;
category?: string;
description?: string;
keys: string | string[];
ref?: any;
hidden?: boolean;
callback: (e: ExtendedKeyboardEvent, combo: string) => void;
}
You can also get all the hotkeys that registered by just calling the useHotkeys
hook and it will return the current hotkeys.
const hotkeys = useHotkeys();
This is useful for creating a dialog to present the user with all the options.