mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-24 10:16:19 +01:00
20 lines
428 B
JavaScript
20 lines
428 B
JavaScript
|
import { useEffect, useCallback } from 'react';
|
||
|
|
||
|
export default 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;
|
||
|
}
|