@bonsai-components/react-global-keys
Version:
A simple way to add global keys
61 lines (60 loc) • 2.12 kB
TypeScript
import React, { Component } from 'react';
export declare type Modifiers = {
meta?: boolean;
ctrl?: boolean;
alt?: boolean;
shift?: boolean;
};
export declare type BindingByKey = {
key: string;
action: (e?: React.KeyboardEvent<Element>) => void;
description?: string;
modifier?: Modifiers;
};
export declare type BindingByCode = {
code: string;
action: (e?: React.KeyboardEvent<Element>) => void;
description?: string;
modifier?: Modifiers;
};
export declare type KeyBinding = BindingByKey | BindingByCode;
export declare type BindingDescriptorByKey = Pick<BindingByKey, 'key' | 'description' | 'modifier'>;
export declare type BindingDescriptorByCode = Pick<BindingByCode, 'code' | 'description' | 'modifier'>;
export declare type KeyBindingDescriptor = BindingDescriptorByKey | BindingDescriptorByCode;
export declare type GlobalKeysContextProps = {
/**
* Set to see console messages
*/
debug?: boolean;
/**
* Some OS intercept meta; set this so ctrl can be used as well as meta for
* meta binds
*/
useCtrlAsMetaAlternative?: boolean;
disableKeybindings?: boolean;
children: React.ReactNode;
};
declare const initialState: {
addKeyBinding: (_keyBinding: KeyBinding) => string | undefined;
getKeyBindingDescriptors: () => KeyBindingDescriptor[];
keyBindings: Map<string, KeyBinding>;
};
export declare type GlobalKeysContextState = typeof initialState;
export declare const GlobalKeysContext: React.Context<{
addKeyBinding: (_keyBinding: KeyBinding) => string | undefined;
getKeyBindingDescriptors: () => KeyBindingDescriptor[];
keyBindings: Map<string, KeyBinding>;
}>;
export declare class GlobalKeysProvider extends Component<GlobalKeysContextProps, GlobalKeysContextState> {
readonly state: GlobalKeysContextState;
private handleKeyDown;
private encodeKeyEvent;
private encodeKeyBinding;
private encodeModifierStates;
private addListener;
private removeListener;
componentDidMount(): void;
componentWillUnmount(): void;
render(): JSX.Element;
}
export {};