Skip to content

components/Menu: Restore Modal focus return when menu items close - #81164

Merged
ciampo merged 8 commits into
trunkfrom
codex/issue-80734-menu-focus-handoff
Aug 11, 2026
Merged

components/Menu: Restore Modal focus return when menu items close#81164
ciampo merged 8 commits into
trunkfrom
codex/issue-80734-menu-focus-handoff

Conversation

@ciampo

@ciampo ciampo commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

What?

Closes #80734.

Restores focus return when a legacy Modal opens from a closing Menu. It also makes the default Modal scroll lock work without WordPress common.css.

The existing hideOnClick behavior is unchanged.

Why?

#77460 made menus unmount immediately to preserve the menu-to-Modal scroll-lock handoff. The focused item now disappeared before Modal captured its focus-return target, so focus returned to the page instead of the root menu button.

How?

When an item is about to close its menu and focus is still inside it, focus moves to the root menu button before Ariakit unmounts the menu. The immediate teardown and Ariakit activation safeguards remain unchanged.

Modal now includes the default body.modal-open { overflow: hidden; } style. Custom bodyOpenClassName values still require their own scroll-lock CSS.

Testing Instructions

  1. Open Components / Actions / Menu / With Modal in Storybook with Global CSS: Font only.
  2. Open the menu and select Open modal.
  3. Confirm that the menu closes and scrolling stays locked.
  4. Close the Modal.
  5. Confirm that scrolling is restored and focus returns to Open menu.

Testing Instructions for Keyboard

Repeat using Enter to select Open modal.

Screenshots or screencast

Before After
Kapture.2026-08-04.at.17.41.19.mp4
Kapture.2026-08-04.at.17.47.06.mp4

Use of AI Tools

Codex was used to inspect the regression history and installed source, implement the focused change and tests, and run verification. The resulting diff and test evidence were reviewed locally.

@github-actions github-actions Bot added the [Package] Components /packages/components label Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

Size Change: +266 B (0%)

Total Size: 7.65 MB

📦 View Changed
Filename Size Change
build/scripts/components/index.min.js 278 kB +225 B (+0.08%)
build/styles/components/style-rtl.css 18.1 kB +11 B (+0.06%)
build/styles/components/style-rtl.min.css 15 kB +10 B (+0.07%)
build/styles/components/style.css 18.1 kB +10 B (+0.06%)
build/styles/components/style.min.css 15 kB +10 B (+0.07%)

compressed-size-action

@ciampo ciampo self-assigned this Aug 4, 2026
@ciampo ciampo added the [Type] Bug An existing feature does not function as intended label Aug 4, 2026
Comment thread packages/components/src/menu/stories/index.story.tsx
@github-actions github-actions Bot added [Package] DataViews /packages/dataviews [Package] UI /packages/ui labels Aug 4, 2026
@github-project-automation github-project-automation Bot moved this to 🔎 Needs Review in WordPress 7.1 Editor Tasks Aug 4, 2026
@ciampo ciampo added the Backport to WP 7.1 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Aug 4, 2026
Comment thread packages/components/CHANGELOG.md Outdated
Comment thread packages/components/src/menu/types.ts Outdated
@github-actions github-actions Bot removed the [Package] UI /packages/ui label Aug 4, 2026
@ciampo
ciampo marked this pull request as ready for review August 4, 2026 17:53
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

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: ciampo <mciampini@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: yashjawale <yashjawale@git.wordpress.org>

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

@ciampo ciampo changed the title Menu: Always close items and preserve Modal focus return components/Menu: Always close items and preserve Modal focus return Aug 4, 2026
@ciampo
ciampo requested a review from t-hamano August 4, 2026 17:54
@ciampo
ciampo force-pushed the codex/issue-80734-menu-focus-handoff branch from 713d598 to ed6a4b3 Compare August 4, 2026 21:20
@github-actions github-actions Bot removed the [Package] DataViews /packages/dataviews label Aug 4, 2026
@ciampo ciampo changed the title components/Menu: Always close items and preserve Modal focus return components/Menu: Restore Modal focus return when menu items close Aug 4, 2026
@ciampo
ciampo requested a review from mirka August 4, 2026 21:33
@t-hamano

t-hamano commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

I will soon start the release process for 7.1 RC1. Let's consider releasing this PR for RC2 if possible.

@mirka mirka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm doing the test instructions in Storybook, and I noticed that the scroll lock only works when the "WordPress" styles are injected (common.css). It doesn't work with "font only". I think maybe a overflow: hidden is missing on body?

Comment thread packages/components/src/menu/stories/index.story.tsx
Comment thread packages/components/src/menu/test/index.tsx Outdated
@ciampo
ciampo force-pushed the codex/issue-80734-menu-focus-handoff branch from ed6a4b3 to 3eb5247 Compare August 10, 2026 11:43
@ciampo

ciampo commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

I'm doing the test instructions in Storybook, and I noticed that the scroll lock only works when the "WordPress" styles are injected (common.css). It doesn't work with "font only". I think maybe a overflow: hidden is missing on body?

I believe that was already the case for Modal prior to this PR, but I agree it makes sense to add overflow: hidden styles to Modal too, which will make it more "standalone"—I went ahead and added them.

I also improved the JSDocs for the bodyOpenClassName prop to make it clear that passing a custom class name there will also require custom handling of the scroll-lock styles (same behavior as currently on trunk, just more explicitly documented).

This PR should hopefully be ready for a final review round.

@mirka mirka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also smoke tested in the app 👍

@ciampo
ciampo merged commit 13a0338 into trunk Aug 11, 2026
62 of 64 checks passed
@ciampo
ciampo deleted the codex/issue-80734-menu-focus-handoff branch August 11, 2026 15:46
@github-project-automation github-project-automation Bot moved this from 🔎 Needs Review to ✅ Done in WordPress 7.1 Editor Tasks Aug 11, 2026
@github-actions github-actions Bot added this to the Gutenberg 23.8 milestone Aug 11, 2026
@github-actions

Copy link
Copy Markdown

There was a conflict while trying to cherry-pick the commit to the wp/7.1 branch. Please resolve the conflict manually and create a PR to the wp/7.1 branch.

PRs to wp/7.1 are similar to PRs to trunk, but you should base your PR on the wp/7.1 branch instead of trunk.

# Checkout the wp/7.1 branch instead of trunk.
git checkout wp/7.1

# Create a new branch for your PR.
git checkout -b my-branch

# Cherry-pick the commit.
git cherry-pick 13a0338a03ae579ee9298ee99596cb07f789ddee

# Check which files have conflicts.
git status

# Resolve the conflict...
# Add the resolved files to the staging area.
git status
git add .
git cherry-pick --continue

# Push the branch to the repository
git push origin my-branch

# Create a PR and set the base to the wp/7.1 branch.
# See https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request.

@t-hamano

Copy link
Copy Markdown
Contributor

I submitted a PR because the cherry-pick failed. The conflicting part is unrelated to the current logic change, so I intend to merge it after smoke testing if there are no issues. #81446

@t-hamano t-hamano added the [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). label Aug 11, 2026
@github-actions

Copy link
Copy Markdown

There was a conflict while trying to cherry-pick the commit to the wp/7.1 branch. Please resolve the conflict manually and create a PR to the wp/7.1 branch.

PRs to wp/7.1 are similar to PRs to trunk, but you should base your PR on the wp/7.1 branch instead of trunk.

# Checkout the wp/7.1 branch instead of trunk.
git checkout wp/7.1

# Create a new branch for your PR.
git checkout -b my-branch

# Cherry-pick the commit.
git cherry-pick 13a0338a03ae579ee9298ee99596cb07f789ddee

# Check which files have conflicts.
git status

# Resolve the conflict...
# Add the resolved files to the staging area.
git status
git add .
git cherry-pick --continue

# Push the branch to the repository
git push origin my-branch

# Create a PR and set the base to the wp/7.1 branch.
# See https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request.

t-hamano added a commit that referenced this pull request Aug 11, 2026
…1164)

* Menu: Restore focus handoff when items close

* Menu: Preserve default modal dismissal coverage

* Menu: Preserve hideOnClick behavior during focus handoff

* Menu: Add focus handoff changelog

* Menu: Cover closing and keep-open Modal stories

* Menu: Simplify Modal regression coverage

* Modal: Add default scroll lock styles

* Menu: Follow current import conventions

---

Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: yashjawale <yashjawale@git.wordpress.org>
@t-hamano t-hamano added Backported to WP Core Pull request that has been successfully merged into WP Core and removed Backport to WP 7.1 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Aug 12, 2026
shail-mehta pushed a commit that referenced this pull request Aug 12, 2026
…1164)

* Menu: Restore focus handoff when items close

* Menu: Preserve default modal dismissal coverage

* Menu: Preserve hideOnClick behavior during focus handoff

* Menu: Add focus handoff changelog

* Menu: Cover closing and keep-open Modal stories

* Menu: Simplify Modal regression coverage

* Modal: Add default scroll lock styles

* Menu: Follow current import conventions

---

Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: yashjawale <yashjawale@git.wordpress.org>
@ciampo ciampo mentioned this pull request Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backported to WP Core Pull request that has been successfully merged into WP Core [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Components /packages/components [Type] Bug An existing feature does not function as intended

Projects

Development

Successfully merging this pull request may close these issues.

Menu: Focus is not returned to the trigger button after closing a Modal opened from a menu item

3 participants