mirror of
https://github.com/docmost/docmost.git
synced 2026-08-27 08:47:06 +08:00
13 lines
418 B
TypeScript
13 lines
418 B
TypeScript
import { useEffect } from "react";
|
|
|
|
export function useEscapeClose(opened: boolean, onClose: () => void) {
|
|
useEffect(() => {
|
|
if (!opened) return;
|
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === "Escape" && !e.defaultPrevented) onClose();
|
|
};
|
|
document.addEventListener("keydown", onKeyDown);
|
|
return () => document.removeEventListener("keydown", onKeyDown);
|
|
}, [opened, onClose]);
|
|
}
|