Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modal: fix closing when contained iframe is focused #51602

Merged
merged 16 commits into from
Oct 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Call onRequestClose for nested modal when outer modal unmounts
  • Loading branch information
stokesman committed Oct 5, 2023
commit 183b9a3659671e540e589e96e72c18e678cdaaab
14 changes: 9 additions & 5 deletions packages/components/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,20 @@ function UnforwardedModal(

const dismissers = useContext( context );
const isLevel0 = dismissers === level0Dismissers;
const nestedDismissers = useRef< typeof level0Dismissers >( [] );

// Updates the stack tracking open modals at this level and calls
// onRequestClose for the prior modal if applicable.
// onRequestClose for any prior and/or nested modals as applicable.
useEffect( () => {
dismissers.push( refOnRequestClose );
const [ first, second ] = dismissers;
if ( second ) first?.current?.();
return () => void dismissers.shift();

const nested = nestedDismissers.current;
return () => {
nested[ 0 ]?.current?.();
dismissers.shift();
};
}, [ dismissers ] );

// Adds/removes the value of bodyOpenClassName to body element.
Expand Down Expand Up @@ -344,10 +350,8 @@ function UnforwardedModal(
</div>
);

const nextLevelDismissers = useRef( [] );

return createPortal(
<context.Provider value={ nextLevelDismissers.current }>
<context.Provider value={ nestedDismissers.current }>
{ modal }
</context.Provider>,
document.body
Expand Down