umami/hooks/useEscapeKey.js

22 lines
450 B
JavaScript
Raw Normal View History

import { useEffect, useCallback } from 'react';
2023-05-18 08:20:06 +02:00
export function useEscapeKey(handler) {
const escFunction = useCallback(event => {
if (event.keyCode === 27) {
handler(event);
}
}, []);
useEffect(() => {
document.addEventListener('keydown', escFunction, false);
return () => {
document.removeEventListener('keydown', escFunction, false);
};
}, [escFunction]);
return null;
}
2023-05-18 08:20:06 +02:00
export default useEscapeKey;