Skip to content

Commit 0760fdf

Browse files
Added ability to use useHotkeys inside and iframe (#23)
* Added a optional argument to accept the document of the iframe * Added the readme for the new prop * Updated the readme
1 parent 6d4316b commit 0760fdf

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# @bigbinary/neeto-hotkeys
22

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

77
## Installation
@@ -56,6 +56,8 @@ yarn add @bigbinary/neeto-hotkeys
5656
3. enabled: By default its value will be `true`. Setting this to `false` will
5757
not register the hotkey.
5858

59+
- `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.
60+
5961
### Return value:
6062

6163
- `inputRef`: A `ref` which needs to be attached to the input element to be
@@ -86,4 +88,3 @@ Hotkeys are a fundamental aspect of many applications, enhancing user efficiency
8688
and interactivity. The useHotKeys hook simplifies the implementation of these
8789
hotkeys by allowing you to specify the hotkey combinations in various formats,
8890
associated handlers, and configuration options.
89-

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
unBindHotKey,
1010
} from "src/utils";
1111

12-
const useHotKeys = (hotkey, handler, userConfig) => {
12+
const useHotKeys = (hotkey, handler, userConfig, externalDocument) => {
1313
const ref = useRef(null);
1414
const convertedHotkey = convertHotkeyToUsersPlatform(hotkey);
1515
const config = mergeLeft(userConfig, DEFAULT_CONFIG);
@@ -26,6 +26,7 @@ const useHotKeys = (hotkey, handler, userConfig) => {
2626
hotkey: convertedHotkey,
2727
handler,
2828
ref,
29+
externalDocument,
2930
});
3031

3132
return () => {

src/utils.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,30 @@ export const convertHotkeyToUsersPlatform = hotkey => {
3131
return convertHotKeyToWindows(hotkey);
3232
};
3333

34-
export const bindHotKey = ({ mode, hotkey, handler, ref }) => {
34+
export const bindHotKey = ({
35+
mode,
36+
hotkey,
37+
handler,
38+
ref,
39+
externalDocument,
40+
}) => {
3541
let mousetrapInstance;
3642

3743
switch (mode) {
3844
case MODES.global:
3945
Mousetrap.bindGlobal(hotkey, handler);
4046
break;
4147
case MODES.scoped:
42-
mousetrapInstance = Mousetrap(ref.current).bind(hotkey, handler);
48+
mousetrapInstance = Mousetrap(externalDocument ?? ref.current).bind(
49+
hotkey,
50+
handler
51+
);
4352
break;
4453
default:
45-
mousetrapInstance = Mousetrap.bind(hotkey, handler);
54+
mousetrapInstance = Mousetrap(externalDocument ?? document).bind(
55+
hotkey,
56+
handler
57+
);
4658
}
4759

4860
return mousetrapInstance;

0 commit comments

Comments
 (0)