Skip to content

Commit

Permalink
Modal: Fix loss of focus when clicking outside
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Jul 14, 2023
1 parent 05e02e8 commit 91822b7
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/components/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ function UnforwardedModal(
[ hasScrolledContent ]
);

const onOverlayPress: React.PointerEventHandler< HTMLDivElement > = (
event
) => {
if ( event.target === event.currentTarget ) {
event.preventDefault();
onRequestClose( event );
}
};

return createPortal(
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
Expand All @@ -179,6 +188,9 @@ function UnforwardedModal(
overlayClassName
) }
onKeyDown={ handleEscapeKeyDown }
onPointerDown={
shouldCloseOnClickOutside ? onOverlayPress : undefined
}
>
<StyleProvider document={ document }>
<div
Expand Down

0 comments on commit 91822b7

Please sign in to comment.