Open tjcarroll11 opened 4 years ago
I had the same issue, I solved it using innerRef
and useLayoutEffect
I hope this helps you:
import React, { useRef, useLayoutEffect } from 'react';
import { HotKeys } from 'react-hotkeys';
const FocusHotKeys: React.FC<Props> = ({ children, handlers, keyMap }) => {
const ref = useRef<HTMLDivElement>(null);
useLayoutEffect((): void => {
if (ref.current) {
ref.current.focus();
}
}, [ref]);
return (
<HotKeys innerRef={ref} handlers={handlers} keyMap={keyMap}>
{children}
</HotKeys>
);
};
export default FocusHotKeys;
type Props = {
handlers: Readonly<{ [key: string]: (keyEvent?: KeyboardEvent) => void }>;
keyMap: Readonly<{ [key: string]: string }>;
};
Thanks for posting your issue.
Unfortunately I do not have the time to actively work on this package, but I am seeking other active maintainers. If you are willing to create a pull request or help out, that would be an excellent way of moving this forward.
Describe the bug When my
<HotKeys></HotKeys>
tag is dynamically added to the UI, its handlers are getting ignored.How are you using react hotkeys components? (HotKeys, GlobalHotKeys, IgnoreKeys etc)
Here is a trimmed down version of what I'm doing
Expected behavior Hotkey handlers for component in focus are triggered
Platform (please complete the following information):
Are you willing and able to create a PR request to fix this issue? Yes, if given a little guidance
Include the smallest log that includes your issue:
I've found that in another place, when I see "Ignored as it was not expected, and has already been simulated.", my hotkeys all start failing. If I unfocus and refocus, it will fix it. In this case, it only fixes it for literally 1 keypress.
What Configuration options are you using?