Repository navigation
fix(overlays): respect keyboardClose - #31867
Conversation
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 is attempting to deploy a commit to the Ionic Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
thetaPC
left a comment
There was a problem hiding this comment.
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.
| /** | ||
| * 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(); | ||
| } |
There was a problem hiding this comment.
- Skipping only the
blur()leaves the input focused inside the view container, andapplyRootLockstill setsaria-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'saria-hiddenin 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.
|
Thanks for the review! I've made all three changes in c29be72. |
|
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. |
Issue number: resolves #31199
What is the current behavior?
Presenting an overlay blurs the focused element before checking
keyboardClose, so a popover withkeyboardClose={false}still closes the on-screen keyboard, which is what that option opts out of.What is the new behavior?
restoreElementFocusskips the blur whenkeyboardCloseisfalse, so the field keeps focus and the keyboard stays open.applyRootLockdoes not setaria-hiddenon the view container while focus stays in the page withkeyboardClose={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.keyboardClose={true}still blur and still hide the root, and focus still returns to the field after dismiss in both cases.ion-alertstill takes focus when presented, same asmain.Does this introduce a breaking change?
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 theapplyRootLockchange, the new aria-hidden assertion fails. I also checked a popover and a modal withkeyboardClose={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, andprettier --checkon the changed files. Not run: WebKit, Firefox, a real device.