Skip to content

Commit

Permalink
Fix modal closing when a contained iframe is focused
Browse files Browse the repository at this point in the history
Uses the overlay element to request close instead of focus outside
hook.
  • Loading branch information
stokesman committed Jun 26, 2023
1 parent fe28cc9 commit 2068546
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/components/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
* External dependencies
*/
import classnames from 'classnames';
import type { ForwardedRef, KeyboardEvent, UIEvent } from 'react';
import type {
ForwardedRef,
KeyboardEvent,
PointerEventHandler,
UIEvent,
} from 'react';

/**
* WordPress dependencies
Expand All @@ -20,7 +25,6 @@ import {
useInstanceId,
useFocusReturn,
useFocusOnMount,
__experimentalUseFocusOutside as useFocusOutside,
useConstrainedTabbing,
useMergeRefs,
} from '@wordpress/compose';
Expand Down Expand Up @@ -77,7 +81,6 @@ function UnforwardedModal(
const focusOnMountRef = useFocusOnMount( focusOnMount );
const constrainedTabbingRef = useConstrainedTabbing();
const focusReturnRef = useFocusReturn();
const focusOutsideProps = useFocusOutside( onRequestClose );
const contentRef = useRef< HTMLDivElement >( null );
const childrenContainerRef = useRef< HTMLDivElement >( null );

Expand Down Expand Up @@ -170,6 +173,10 @@ function UnforwardedModal(
[ hasScrolledContent ]
);

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

return createPortal(
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
Expand All @@ -179,6 +186,9 @@ function UnforwardedModal(
overlayClassName
) }
onKeyDown={ handleEscapeKeyDown }
onPointerDown={
shouldCloseOnClickOutside ? onOverlayPress : undefined
}
>
<StyleProvider document={ document }>
<div
Expand All @@ -200,9 +210,6 @@ function UnforwardedModal(
aria-labelledby={ contentLabel ? undefined : headingId }
aria-describedby={ aria.describedby }
tabIndex={ -1 }
{ ...( shouldCloseOnClickOutside
? focusOutsideProps
: {} ) }
onKeyDown={ onKeyDown }
>
<div
Expand Down

0 comments on commit 2068546

Please sign in to comment.