Skip to content

Commit

Permalink
Monopolize open modal
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Sep 5, 2023
1 parent 9cd88e8 commit df1e700
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/components/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import Button from '../button';
import StyleProvider from '../style-provider';
import type { ModalProps } from './types';

// Used to count the number of open modals.
// Used to track and dismiss the prior modal when another opens.
let openModalCount = 0;
let dismissOpenModal: ModalProps[ 'onRequestClose' ];

function UnforwardedModal(
props: ModalProps,
Expand Down Expand Up @@ -104,7 +105,10 @@ function UnforwardedModal(
if ( openModalCount === 1 ) {
ariaHelper.hideApp( ref.current );
document.body.classList.add( bodyOpenClassName );
} else if ( openModalCount > 1 ) {
dismissOpenModal();
}
dismissOpenModal = onRequestClose;

return () => {
openModalCount--;
Expand All @@ -114,7 +118,7 @@ function UnforwardedModal(
ariaHelper.showApp();
}
};
}, [ bodyOpenClassName ] );
}, [ bodyOpenClassName, onRequestClose ] );

// Calls the isContentScrollable callback when the Modal children container resizes.
useLayoutEffect( () => {
Expand Down

0 comments on commit df1e700

Please sign in to comment.