Skip to content

Commit

Permalink
Add options parameter to useHotkeys hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasan Afzal committed Dec 27, 2019
1 parent cfaae44 commit bce88f0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import hotkeys, {HotkeysEvent} from 'hotkeys-js';
import {useCallback, useEffect} from "react";
import hotkeys, { HotkeysEvent } from "hotkeys-js";
import { useCallback, useEffect } from "react";

type CallbackFn = (event: KeyboardEvent, handler: HotkeysEvent) => void;
type Options = {
filter?: typeof hotkeys.filter;
};

export function useHotkeys(keys: string, callback: CallbackFn, deps: any[] = []) {
export function useHotkeys(
keys: string,
callback: CallbackFn,
deps: any[] = [],
options: Options = {}
) {
const memoisedCallback = useCallback(callback, deps);

useEffect(() => {
if (options.filter) hotkeys.filter = options.filter;

hotkeys(keys, memoisedCallback);

return () => hotkeys.unbind(keys, memoisedCallback);
}, [memoisedCallback]);
}, [memoisedCallback, options]);
}

0 comments on commit bce88f0

Please sign in to comment.