From 86a3668c88d11cb1200c28839c6afffa40426d20 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Tue, 27 Jul 2021 14:56:21 +0100 Subject: [PATCH] Fix disappearing modal after tab switch --- .../src/lib/remix-ui-modal-dialog.tsx | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/libs/remix-ui/modal-dialog/src/lib/remix-ui-modal-dialog.tsx b/libs/remix-ui/modal-dialog/src/lib/remix-ui-modal-dialog.tsx index ec016068e9..6a764a85e2 100644 --- a/libs/remix-ui/modal-dialog/src/lib/remix-ui-modal-dialog.tsx +++ b/libs/remix-ui/modal-dialog/src/lib/remix-ui-modal-dialog.tsx @@ -16,6 +16,24 @@ export const ModalDialog = (props: ModalDialogProps) => { modal.current.focus() }, [props.hide]) + useEffect(() => { + function handleBlur (e) { + if (!e.currentTarget.contains(e.relatedTarget)) { + e.stopPropagation() + if (document.activeElement !== this) { + handleHide() + } + } + } + if (modal.current) { + modal.current.addEventListener('blur', handleBlur) + + return () => { + modal.current.removeEventListener('blur', handleBlur) + } + } + }, [modal.current]) + const modalKeyEvent = (keyCode) => { if (keyCode === 27) { // Esc if (props.cancelFn) props.cancelFn() @@ -40,13 +58,6 @@ export const ModalDialog = (props: ModalDialogProps) => { handleHide() } - const handleBlur = (e) => { - if (!e.currentTarget.contains(e.relatedTarget)) { - e.stopPropagation() - handleHide() - } - } - return (
{ >