mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
20 lines
510 B
TypeScript
20 lines
510 B
TypeScript
import React, { useEffect } from "react";
|
|
|
|
const useOutsideKeydownDetector = (ref: React.RefObject<HTMLElement>, callback: () => void) => {
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
if (ref.current && !ref.current.contains(event.target as Node)) {
|
|
callback();
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
document.addEventListener("keydown", handleKeyDown);
|
|
|
|
return () => {
|
|
document.removeEventListener("keydown", handleKeyDown);
|
|
};
|
|
});
|
|
};
|
|
|
|
export default useOutsideKeydownDetector;
|