restore page scroll after modal is closed

This commit is contained in:
Pramod Mahajan 2025-05-19 11:50:50 +05:30
parent dbc1ae2c63
commit f8f43b62c6

View File

@ -15,7 +15,7 @@ const GlobalModel = ({
useEffect(() => { useEffect(() => {
const modalElement = modalRef.current; const modalElement = modalRef.current;
const modalInstance = new window.bootstrap.Modal(modalElement, { const modalInstance = new window.bootstrap.Modal(modalElement, {
backdrop: false // Disable backdrop backdrop: false,
}); });
if (isOpen) { if (isOpen) {
@ -26,16 +26,27 @@ useEffect(() => {
const handleHideModal = () => { const handleHideModal = () => {
closeModal(); closeModal();
// FIX: Remove any lingering body classes/styles
document.body.classList.remove('modal-open');
document.body.style.overflow = '';
document.body.style.paddingRight = '';
}; };
modalElement.addEventListener('hidden.bs.modal', handleHideModal); modalElement.addEventListener('hidden.bs.modal', handleHideModal);
return () => { return () => {
modalElement.removeEventListener('hidden.bs.modal', handleHideModal); modalElement.removeEventListener('hidden.bs.modal', handleHideModal);
// Also clean up just in case component unmounts
document.body.classList.remove('modal-open');
document.body.style.overflow = '';
document.body.style.paddingRight = '';
}; };
}, [isOpen, closeModal]); }, [isOpen, closeModal]);
// Dynamically set the modal size classes (modal-sm, modal-lg, modal-xl) // Dynamically set the modal size classes (modal-sm, modal-lg, modal-xl)
const modalSizeClass = size ? `modal-${size}` : ''; // Default is empty if no size is specified const modalSizeClass = size ? `modal-${size}` : ''; // Default is empty if no size is specified