React hook for github's hotkey package.
- Install
npm i -S @letslearn/hotkey
- Usage in react hook
function KeyCount() {
let [count, setCount] = React.useState(0);
console.log("hotkey is comming ", useHotkey);
let elementRef = useHotkey("h");
return <h1 ref={elementRef} onClick={() => setCount(prev => prev + 1)}>Type h to count: {count}</h1>
}
By default, key shortcuts are registerd in global level, if you want to enable shortcut only when using is typing in particular element, we can scope out the when to fire the short cut
function KeyCount() {
let elementRef = useHotkey("h", {scope: "root"});
}
Options
{
scope: "string"
}
scope parameter takes the id of element to enable shortcuts inside element.