Skip to content

Commit

Permalink
Moved document property to config object (#34)
Browse files Browse the repository at this point in the history
Co-authored-by: bot-bigbinary <[email protected]>
  • Loading branch information
josephmathew900 and bot-bigbinary authored Nov 21, 2024
1 parent 04933e1 commit 146dd2d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# @bigbinary/neeto-hotkeys

The `neeto-hotkeys` package provides the `useHotKeys` hook, a versatile utility for managing hotkeys in an application.
This hook allows you to define specific hotkey combinations and associate them with corresponding handler functions.
The associated handler is invoked upon pressing the configured hotkey(s), enabling you to execute actions in response to keyboard input.
The `neeto-hotkeys` package provides the `useHotKeys` hook, a versatile utility
for managing hotkeys in an application. This hook allows you to define specific
hotkey combinations and associate them with corresponding handler functions. The
associated handler is invoked upon pressing the configured hotkey(s), enabling
you to execute actions in response to keyboard input.

## Installation

Expand Down Expand Up @@ -40,6 +42,7 @@ yarn add @bigbinary/neeto-hotkeys

- `config`: A config object which has 3 properties `mode`, `unbindOnUnmount` &
`enabled`.

1. mode: The available values for mode are `default`, `global` & `scoped`.
- default: It is the default mode. Handlers will only be called if the
user is outside of a textarea, input, or select element.
Expand All @@ -55,8 +58,11 @@ yarn add @bigbinary/neeto-hotkeys
value to `false`.
3. enabled: By default its value will be `true`. Setting this to `false` will
not register the hotkey.

- `externalDocument`: This is an optional argument. If you want to listen for hotkeys on an external document (e.g., an iframe), pass the reference of that document as the 4th argument for useHotKeys hook. If you do not provide this argument, the hook will listen for hotkeys on the current document by default.
4. document: This is an optional property. If you want to listen for hotkeys
on an external document (e.g., an iframe), pass the reference of that
document using the `document` property in the `config` object. If you do
not provide this property, the hook will listen for hotkeys on the current
document by default.

### Return value:

Expand All @@ -65,8 +71,8 @@ yarn add @bigbinary/neeto-hotkeys

### Usage:

Following illustrates the usage of `useHotKeys` hook in implementing shortcut for
Sidebar opening.
Following illustrates the usage of `useHotKeys` hook in implementing shortcut
for Sidebar opening.

```jsx
import useHotKeys from "@bigbinary/neeto-hotkeys";
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type ConfigType = {
mode?: "default" | "global" | "scoped";
unbindOnUnmount?: boolean;
enabled?: boolean;
document?: Document;
};

export default function useHotkeys(
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const DEFAULT_CONFIG = {
mode: MODES.default,
unbindOnUnmount: true,
enabled: true,
document: undefined,
};

export const MAC_TO_WINDOWS_KEYS_MAP = {
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
unBindHotKey,
} from "src/utils";

const useHotKeys = (hotkey, handler, userConfig, externalDocument) => {
const useHotKeys = (hotkey, handler, config) => {
const ref = useRef(null);
const handlerRef = useRef(handler);

Expand All @@ -21,8 +21,8 @@ const useHotKeys = (hotkey, handler, userConfig, externalDocument) => {
);

const memoizedConfig = useMemo(
() => mergeLeft(userConfig, DEFAULT_CONFIG),
[userConfig?.enabled, userConfig?.mode, userConfig?.unbindOnUnmount]
() => mergeLeft(config, DEFAULT_CONFIG),
[config?.enabled, config?.mode, config?.unbindOnUnmount, config?.document]
);

useEffect(() => {
Expand All @@ -33,7 +33,7 @@ const useHotKeys = (hotkey, handler, userConfig, externalDocument) => {
hotkey: convertedHotkey,
handler: (...args) => handlerRef.current(...args),
ref,
externalDocument,
targetDocument: memoizedConfig.document,
});

return () => {
Expand All @@ -43,7 +43,7 @@ const useHotKeys = (hotkey, handler, userConfig, externalDocument) => {
hotkey: convertedHotkey,
});
};
}, [convertedHotkey, externalDocument, memoizedConfig]);
}, [convertedHotkey, memoizedConfig]);

return memoizedConfig.mode === MODES.scoped ? ref : null;
};
Expand Down
12 changes: 3 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,21 @@ export const convertHotkeyToUsersPlatform = hotkey => {
return convertHotKeyToWindows(hotkey);
};

export const bindHotKey = ({
mode,
hotkey,
handler,
ref,
externalDocument,
}) => {
export const bindHotKey = ({ mode, hotkey, handler, ref, targetDocument }) => {
let mousetrapInstance;

switch (mode) {
case MODES.global:
Mousetrap.bindGlobal(hotkey, handler);
break;
case MODES.scoped:
mousetrapInstance = Mousetrap(externalDocument ?? ref.current).bind(
mousetrapInstance = Mousetrap(targetDocument ?? ref.current).bind(
hotkey,
handler
);
break;
default:
mousetrapInstance = Mousetrap(externalDocument ?? document).bind(
mousetrapInstance = Mousetrap(targetDocument ?? document).bind(
hotkey,
handler
);
Expand Down

0 comments on commit 146dd2d

Please sign in to comment.