From a2259f40ed8451f1f087e83baee5a6253da38b4a Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Sat, 12 Oct 2024 13:31:15 -0400 Subject: [PATCH] feat(contextmenu): adds escape keyevent closing menu --- src/components/ContextMenuPortal.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/ContextMenuPortal.tsx b/src/components/ContextMenuPortal.tsx index 2c57cab..e57278a 100644 --- a/src/components/ContextMenuPortal.tsx +++ b/src/components/ContextMenuPortal.tsx @@ -1,4 +1,4 @@ -import { useClickOutside, useMouse } from "@kaioken-core/hooks"; +import { useClickOutside, useKeyStroke, useMouse } from "@kaioken-core/hooks"; import { Portal, signal, useEffect, useRef } from "kaioken"; namespace ContextMenuPortal { @@ -20,6 +20,14 @@ export function ContextMenuPortal({ children, open, closeAction }: ContextMenuPo useEffect(() => { if (!open) return pos.value = { x: mouse.x, y: mouse.y } + function _handleEscapeKey(e: KeyboardEvent) { + if (e.key !== "Escape") return + closeAction?.() + } + document.addEventListener("keydown", _handleEscapeKey) + return () => { + document.removeEventListener("keydown", _handleEscapeKey) + } }, [open]) if (!open) return null