From c27a491c3f1972fd3f89c25c8d7f9a1aa66f332b Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 2 Sep 2025 22:30:11 +0800 Subject: [PATCH] fix(modal): improve modal exit handling with deferred value - Updated the `useHasModal` hook to utilize `useDeferredValue`, addressing issues with simultaneous hotkey responses during modal exit. - This change enhances the responsiveness and reliability of modal interactions, ensuring a smoother user experience. Signed-off-by: Innei --- .../renderer/src/components/ui/modal/stacked/hooks.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/desktop/layer/renderer/src/components/ui/modal/stacked/hooks.tsx b/apps/desktop/layer/renderer/src/components/ui/modal/stacked/hooks.tsx index d7172f0c5..6c5c5ce2c 100644 --- a/apps/desktop/layer/renderer/src/components/ui/modal/stacked/hooks.tsx +++ b/apps/desktop/layer/renderer/src/components/ui/modal/stacked/hooks.tsx @@ -2,7 +2,7 @@ import { Button } from "@follow/components/ui/button/index.js" import { atom, useAtomValue } from "jotai" import type { DragControls } from "motion/react" import type { ResizeCallback, ResizeStartCallback } from "re-resizable" -import { use, useState } from "react" +import { use, useDeferredValue, useState } from "react" import { flushSync } from "react-dom" import { useTranslation } from "react-i18next" import { useContextSelector } from "use-context-selector" @@ -202,4 +202,9 @@ export const useDialog = (): DialogInstance => { } const modalStackLengthAtom = atom((get) => get(modalStackAtom).length) -export const useHasModal = () => useAtomValue(modalStackLengthAtom) > 0 +export const useHasModal = () => { + // The keydown event of modal exit is triggered in the same loop, + // leading to unexpected simultaneous responses to other hotkeys, + // so deferredValue is added to delay the update + return useDeferredValue(useAtomValue(modalStackLengthAtom) > 0) +}