From 5b233b25e41e35d213f5058fc5f8d53910daa2f7 Mon Sep 17 00:00:00 2001 From: Avinash Kumar Deepak Date: Fri, 6 Mar 2026 23:48:33 +0530 Subject: [PATCH] fix: escape key, stale timer ref, body overflow in Modal (#362) --- src/component/modals/ParentModal.jsx | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/component/modals/ParentModal.jsx b/src/component/modals/ParentModal.jsx index 95c0998..285c9a8 100644 --- a/src/component/modals/ParentModal.jsx +++ b/src/component/modals/ParentModal.jsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import ReactModal from 'react-modal'; import './parent-modal.css'; @@ -9,23 +9,26 @@ const Modal = ({ }) => { const [curClass, setCurClass] = useState(''); const [isOpen, setIsOpen] = useState(ModelOpen); - const [stid, setStid] = useState(null); + const stid = useRef(null); useEffect(() => { if (ModelOpen === true) { setIsOpen(true); setCurClass('closing'); - clearTimeout(stid); - setStid(setTimeout(() => { + clearTimeout(stid.current); + document.body.style.overflow = 'hidden'; + stid.current = setTimeout(() => { setIsOpen(true); setCurClass(''); - }, 20)); + }, 20); } else { setCurClass('closing'); - clearTimeout(stid); - setStid(setTimeout(() => { + clearTimeout(stid.current); + document.body.style.overflow = ''; + stid.current = setTimeout(() => { setIsOpen(false); - }, 400)); + }, 400); } + return () => clearTimeout(stid.current); }, [ModelOpen]); if (!isOpen) return ''; @@ -33,7 +36,7 @@ const Modal = ({