@react-hookz/web
Version:
React hooks done right, for browser and SSR.
30 lines (29 loc) • 1.4 kB
TypeScript
import { DependencyList, RefObject } from 'react';
export declare type IKeyboardEventPredicate = (event: KeyboardEvent) => boolean;
export declare type IKeyboardEventFilter = null | undefined | string | boolean | IKeyboardEventPredicate;
export declare type IKeyboardEventHandler<T extends EventTarget> = (this: T, event: KeyboardEvent) => void;
export declare type IUseKeyboardEventOptions<T extends EventTarget> = {
/**
* Event name that triggers handler.
* @default `keydown`
*/
event?: 'keydown' | 'keypress' | 'keyup';
/**
* Target that should emit event.
* @default window
*/
target?: RefObject<T> | T | null;
/**
* Options that will be passed to underlying `useEventListener` hook.
*/
eventOptions?: boolean | AddEventListenerOptions;
};
/**
* Executes callback when keyboard event occurred on target (window by default).
*
* @param keyOrPredicate Filters keypresses on which `callback` will be executed.
* @param callback Function to call when key is pressed and `keyOrPredicate` matches positive.
* @param deps Dependencies list that will be passed to underlying `useMemo`.
* @param options Hook options.
*/
export declare function useKeyboardEvent<T extends EventTarget>(keyOrPredicate: IKeyboardEventFilter, callback: IKeyboardEventHandler<T>, deps?: DependencyList, options?: IUseKeyboardEventOptions<T>): void;