From b5ec1fe51880dd37182e18cc30bc6257ca32fdac Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 27 Jul 2023 11:09:04 +0800 Subject: [PATCH] press esc to close settings window --- src/settings.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/settings.tsx b/src/settings.tsx index 4fd2c78..da916e9 100644 --- a/src/settings.tsx +++ b/src/settings.tsx @@ -1,5 +1,5 @@ import { createRef } from "preact"; -import { StateUpdater, useState } from "preact/hooks"; +import { StateUpdater, useEffect, useState } from "preact/hooks"; import { ChatStore, clearTotalCost, getTotalCost } from "./app"; import models from "./models"; import { TemplateChatStore } from "./chatbox"; @@ -177,6 +177,22 @@ export default (props: { const importFileRef = createRef(); const [totalCost, setTotalCost] = useState(getTotalCost()); + + useEffect(() => { + const handleKeyPress = (event: any) => { + if (event.keyCode === 27) { + // keyCode for ESC key is 27 + props.setShow(false); + } + }; + + document.addEventListener("keydown", handleKeyPress); + + return () => { + document.removeEventListener("keydown", handleKeyPress); + }; + }, []); // The empty dependency array ensures that the effect runs only once + return (
props.setShow(false)}