Skip to content

Conversation

@tellthemachines
Copy link
Contributor

What?

Fixes issue noticed in testing #71944 . More context here.

Testing Instructions

The bug occurs when nesting a Menu inside a Modal inside a Dropdown, which is what #71944 does. You can test that PR to repro the bug:

  1. Enable the Dataviews: new media modal experiment.
  2. In the editor, add an Image block and populate it with an image.
  3. Click the "Replace" button in the block toolbar, then "Open media library" - you should see the modal containing the dataviews picker.
  4. Click the filter button and then click outside the resulting dropdown to close it. the whole modal disappears.

With this patch applied, the modal should stay open when clicking outside the filters menu.

modal_closing.mp4

@tellthemachines tellthemachines self-assigned this Oct 16, 2025
@tellthemachines tellthemachines added [Type] Bug An existing feature does not function as intended [Package] Components /packages/components labels Oct 16, 2025
@github-actions
Copy link

github-actions bot commented Oct 16, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ramonjd
Copy link
Member

ramonjd commented Oct 16, 2025

Just checking, should the base of this PR be try/new-media-modal?

@tellthemachines
Copy link
Contributor Author

Just checking, should the base of this PR be try/new-media-modal?

I deliberately set it against trunk that so this fix could be merged independently of the media modalPR, because it's still in early stages.

Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a minimum repro case in a unit test or a temporary Storybook story? With that, I want us to determine if this should be patched at the Popover level, rather than in Dropdown. (If the same issue happens with a "Menu inside a Modal inside a Popover", then maybe it should be fixed in Popover.)

@tellthemachines
Copy link
Contributor Author

@mirka I just added a couple stories testing Menu inside both Popover and Dropdown, and you're correct that the problem is with Popover 😅 so I'll update the fix. Thanks for pointing it out!

@tellthemachines tellthemachines changed the title Fix Dropdown closing unexpectedly when a Menu inside it is closed. Fix Popover closing unexpectedly when a Menu inside it is closed. Oct 17, 2025
@tellthemachines
Copy link
Contributor Author

tellthemachines commented Oct 17, 2025

That failing e2e looks like it could be related, but I've been unable to reproduce it locally so far 😕

Update: I have reproed it now, had to run the build task 🤦

@github-actions
Copy link

github-actions bot commented Oct 17, 2025

Flaky tests detected in eabb81c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/18702089247
📝 Reported issues:

@ramonjd
Copy link
Member

ramonjd commented Oct 20, 2025

I checked the new stories and added a onFocusOutside callback to <InsidePopover /> and things are working as I'd expect.

Kapture.2025-10-20.at.11.52.28.mp4

Keen to hear what @mirka thinks!

floatingElement?.contains( blurTarget );
// Only proceed if the blur is actually from this popover
if ( ! isBlurFromThisPopover ) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, what should happen to blur events that aren't from this popover? (if anything)

I was just asking myself if the logic should wrap the callbacks. E.g.,

				// Check if blur is from this popover's reference element or its floating content
				const isBlurFromThisPopover =
					( referenceElement &&
						'contains' in referenceElement &&
						referenceElement.contains( blurTarget ) ) ||
					( floatingElement &&
						floatingElement.contains( blurTarget ) );

				// Only proceed if the blur is actually from this popover
				if ( isBlurFromThisPopover ) {
					if ( onFocusOutside ) {
						onFocusOutside( event );
					} else if ( onClose ) {
						onClose();
					}
				}

But if there's no need to, then ignore me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on the assumption that if a blur happens anywhere unrelated to the popover, the popover shouldn't react to it 😅

asking myself if the logic should wrap the callbacks

It should make no difference. I prefer the early return myself as it makes it easier to read.

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the change here, nice work getting to the bottom of what was causing the issue!

I'm working on the assumption that if a blur happens anywhere unrelated to the popover, the popover shouldn't react to it 😅

I think that sounds like how it should work, too. The existing behaviour feels to me like the source of the bug, that is, the focus-outside was overreaching 😄

It's testing well for me in Storybook:

If I comment out the changes to popover/index.tsx then we can see the bug, where the dropdown unexpectedly closes:

2025-10-21.13.42.07.mp4

And with this PR applied, everything feels stable:

2025-10-21.13.43.18.mp4

LGTM!

@tellthemachines
Copy link
Contributor Author

Thanks for the reviews folks! I'll go ahead and remove the storybook stories because they were just for testing purposes.

@tellthemachines tellthemachines force-pushed the fix/dropdown-closing-unexpectedly branch from 8781dd3 to 9c3c8e9 Compare October 21, 2025 22:56
@tellthemachines tellthemachines enabled auto-merge (squash) October 22, 2025 01:14
@tellthemachines tellthemachines merged commit 8501118 into trunk Oct 22, 2025
34 checks passed
@tellthemachines tellthemachines deleted the fix/dropdown-closing-unexpectedly branch October 22, 2025 01:37
@github-actions github-actions bot added this to the Gutenberg 22.0 milestone Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Components /packages/components [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants