Skip to content

fix(overlays): respect keyboardClose - #31867

Merged
thetaPC merged 2 commits into
ionic-team:mainfrom
MAmmaadTehseen:fix/31199-keyboard-close
Dec 5, 2026
Merged

thetaPC merged 2 commits into
ionic-team:mainfrom
MAmmaadTehseen:fix/31199-keyboard-close

Conversation

@MAmmaadTehseen

@MAmmaadTehseen MAmmaadTehseen commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Issue number: resolves #31199


What is the current behavior?

Presenting an overlay blurs the focused element before checking keyboardClose, so a popover with keyboardClose={false} still closes the on-screen keyboard, which is what that option opts out of.

What is the new behavior?

  • restoreElementFocus skips the blur when keyboardClose is false, so the field keeps focus and the keyboard stays open.
  • applyRootLock does not set aria-hidden on the view container while focus stays in the page with keyboardClose={false}, so the page behind stays visible to assistive tech while the field keeps focus, and Chrome does not log the blocked aria-hidden warning.
  • The default behavior is unchanged: overlays with keyboardClose={true} still blur and still hide the root, and focus still returns to the field after dismiss in both cases.

ion-alert still takes focus when presented, same as main.

Does this introduce a breaking change?

  • Yes
  • No

Other information

Tests run (Chrome only, Pixel 5 profile): the new e2e tests and the rest of core/src/utils/test/overlays/overlays.e2e.ts, 26 passed. Without the applyRootLock change, the new aria-hidden assertion fails. I also checked a popover and a modal with keyboardClose={false} in Chrome with accessibility on: the field keeps focus and typing, the page is not hidden, and no aria-hidden warning is logged. Also run: test.spec (107 suites, 918 tests passed), lint.ts, and prettier --check on the changed files. Not run: WebKit, Firefox, a real device.

Skip blurring the focused element in restoreElementFocus when the
overlay has keyboardClose={false}, since blurring would close the
mobile keyboard, which keyboardClose={false} opts out of. Blur as
before for every other overlay, and still return focus to the
element when the overlay is dismissed in both cases.

Fixes ionic-team#31199
@MAmmaadTehseen
MAmmaadTehseen requested a review from a team as a code owner December 3, 2026 14:16
@vercel

vercel Bot commented Dec 3, 2026

Copy link
Copy Markdown

@MAmmaadTehseen is attempting to deploy a commit to the Ionic Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
ionic-framework Ready Ready Preview Dec 5, 2026 12:09am UTC

Request Review

@thetaPC thetaPC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for picking this up!

Skipping the blur() is the right start, but on its own it brings back the "Blocked aria-hidden" warning for apps with ion-router-outlet. Limiting it to text fields wouldn't help, since any focused element inside the hidden container triggers it. Details inline.

Comment on lines +681 to +687
/**
* Blur the active element to prevent a11y warnings, unless the overlay
* opted out of closing the keyboard, since blurring would close it.
*/
if (keyboardClose !== false) {
previousElement.blur();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Skipping only the blur() leaves the input focused inside the view container, and applyRootLock still sets aria-hidden="true" on that container.
  • With the markup from the new tests on 9.0.15, Chrome then logs "Blocked aria-hidden on an element because its descendant retained focus", the warning fix(overlays,picker): remove invalid aria-hidden attribute #30563 added this blur() to avoid. Safari doesn't block it, so there the focused input stays inside a hidden subtree.
  • With keyboardClose={false} the user keeps working in the page, so the page should stay visible while focus is in it. Skipping the container's aria-hidden in that case keeps focus on the input with no warning.

Both spots are outside the diff, so please update OverlayWithFocusTrapProps and applyRootLock like this:

type OverlayWithFocusTrapProps = HTMLIonOverlayElement & {
  focusTrap?: boolean;
  keyboardClose?: boolean;
  showBackdrop?: boolean;
  backdropBreakpoint?: number;
};

const applyRootLock = (el: OverlayWithFocusTrapProps) => {
  const viewContainer = getViewContainer();
  // Hiding the container the overlay now sits in would hide the overlay too.
  const containsOverlay = !!viewContainer?.contains(el);
  // With keyboardClose false, focus stays in the page, so the page stays visible.
  const keepsPageFocus = el.keyboardClose === false && !!viewContainer?.contains(document.activeElement);

  if (!containsOverlay && !keepsPageFocus) {
    setRootAriaHidden(true);
  }

  document.body.classList.add(BACKDROP_NO_SCROLL);
};

Do not set aria-hidden on the view container while focus stays in the
page when an overlay is presented with keyboardClose false, so Chrome
does not block it or log the aria-hidden warning. Rename the new tests
and check that the container stays visible.
@MAmmaadTehseen

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've made all three changes in c29be72.

@thetaPC thetaPC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@thetaPC

thetaPC commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Congrats on your first contribution to Ionic Framework, and thanks for turning the review around so quickly! This fix will be included in the next release.

@thetaPC
thetaPC added this pull request to the merge queue Dec 5, 2026
Merged via the queue into ionic-team:main with commit ffddf77 Dec 5, 2026
56 checks passed

This branch was successfully deployed

1 active deployment
Preview — c29be724 Deployed Dec 5, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: IonPopover keyboardClose={false} does not prevent keyboard dismissal since ionic 8.7.4

2 participants