inkdrop-command-palette/lib/navigation/hook.js

30 lines
736 B
JavaScript
Raw Permalink Normal View History

2022-08-22 12:20:24 +00:00
"use babel";
import handleEvents from "./handleEvents";
import { useRef, useEffect } from "react";
/**
* A react hook to enable arrow key navigation on a component.
* @param {*} param0
* @returns a useRef, which can be applied to a component
*/
export default function useArrowKeyNavigation(props) {
2022-08-23 01:49:14 +00:00
const { selectors, modal } = props || {};
2022-08-22 12:20:24 +00:00
const parentNode = useRef();
useEffect(() => {
const eventHandler = (event) => {
2022-08-23 01:49:14 +00:00
handleEvents({
event,
parentNode: parentNode.current,
selectors,
modal: modal,
});
2022-08-22 12:20:24 +00:00
};
document.addEventListener("keydown", eventHandler);
return () => document.removeEventListener("keydown", eventHandler);
}, []);
return parentNode;
}