From fd949da3f3d472869b1f26c95a1fc9297dd1d8dd Mon Sep 17 00:00:00 2001 From: Ammad Tehseen Khan Date: Thu, 3 Dec 2026 18:25:05 +0500 Subject: [PATCH 1/2] fix(overlays): respect keyboardClose 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 #31199 --- core/src/utils/overlays.ts | 13 ++- core/src/utils/test/overlays/overlays.e2e.ts | 107 +++++++++++++++++++ 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/core/src/utils/overlays.ts b/core/src/utils/overlays.ts index 944ee227638..96c611d40a4 100644 --- a/core/src/utils/overlays.ts +++ b/core/src/utils/overlays.ts @@ -579,7 +579,7 @@ export const present = async ( * from returning focus as a result. */ if (overlay.el.tagName !== 'ION-TOAST') { - restoreElementFocus(overlay.el); + restoreElementFocus(overlay.el, overlay.keyboardClose); } /** @@ -672,14 +672,19 @@ export const present = async ( * to where they were before they * opened the overlay. */ -const restoreElementFocus = async (overlayEl: any) => { +const restoreElementFocus = async (overlayEl: any, keyboardClose?: boolean) => { let previousElement = document.activeElement as HTMLElement | null; if (!previousElement) { return; } - // Ensure active element is blurred to prevent a11y warning issues - previousElement.blur(); + /** + * 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(); + } const shadowRoot = previousElement?.shadowRoot; if (shadowRoot) { diff --git a/core/src/utils/test/overlays/overlays.e2e.ts b/core/src/utils/test/overlays/overlays.e2e.ts index dc4fa4a4feb..7fedafa5ed8 100644 --- a/core/src/utils/test/overlays/overlays.e2e.ts +++ b/core/src/utils/test/overlays/overlays.e2e.ts @@ -651,5 +651,112 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => await expect(page.locator('#last')).toBeFocused(); }); + + test('popover with keyboardClose false should keep focus on the input while presented', async ({ page }) => { + test.info().annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31199', + }); + // Nest the input under the app root so the popover locks it, as in a real app. + await page.setContent( + ` + +
+ +
+
+ + Popover Content + + `, + config + ); + + const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent'); + const popover = page.locator('ion-popover'); + const input = page.locator('#test-input'); + + await input.click(); + await expect(input).toBeFocused(); + + await popover.evaluate((el: HTMLIonPopoverElement) => el.present()); + await ionPopoverDidPresent.next(); + + // The input keeps focus while the popover is presented + await expect(input).toBeFocused(); + + // Typing still goes into the input + await page.keyboard.press('a'); + await expect(input).toHaveValue('a'); + }); + + test('popover with default keyboardClose should remove focus from the input', async ({ page }) => { + test.info().annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31199', + }); + await page.setContent( + ` + + Popover Content + + + `, + config + ); + + const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent'); + const popover = page.locator('ion-popover'); + const input = page.locator('#test-input'); + + await input.click(); + await expect(input).toBeFocused(); + + await popover.evaluate((el: HTMLIonPopoverElement) => el.present()); + await ionPopoverDidPresent.next(); + + // The overlay takes focus, so it is removed from the input + await expect(input).not.toBeFocused(); + }); + + test('popover with keyboardClose false should return focus to the input on dismiss', async ({ page }) => { + test.info().annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31199', + }); + // Nest the input under the app root so the popover locks it, as in a real app. + await page.setContent( + ` + +
+ +
+
+ + Popover Content + + `, + config + ); + + const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent'); + const ionPopoverDidDismiss = await page.spyOnEvent('ionPopoverDidDismiss'); + const popover = page.locator('ion-popover'); + const input = page.locator('#test-input'); + + await input.click(); + await expect(input).toBeFocused(); + + await popover.evaluate((el: HTMLIonPopoverElement) => el.present()); + await ionPopoverDidPresent.next(); + + await expect(input).toBeFocused(); + + await popover.evaluate((el: HTMLIonPopoverElement) => el.dismiss()); + await ionPopoverDidDismiss.next(); + + // Focus is restored to the input after the popover is dismissed + await expect(input).toBeFocused(); + }); }); }); From c29be7244838cd5cab80bbba0876683d96931196 Mon Sep 17 00:00:00 2001 From: Ammad Tehseen Khan Date: Fri, 4 Dec 2026 12:57:39 +0500 Subject: [PATCH 2/2] fix(overlays): keep the page visible when keyboardClose is false 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. --- core/src/utils/overlays.ts | 9 ++++++++- core/src/utils/test/overlays/overlays.e2e.ts | 7 ++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/src/utils/overlays.ts b/core/src/utils/overlays.ts index 96c611d40a4..53219c6b2cc 100644 --- a/core/src/utils/overlays.ts +++ b/core/src/utils/overlays.ts @@ -45,6 +45,7 @@ export const activeAnimations = new WeakMap(); type OverlayWithFocusTrapProps = HTMLIonOverlayElement & { focusTrap?: boolean; + keyboardClose?: boolean; showBackdrop?: boolean; backdropBreakpoint?: number; }; @@ -527,10 +528,16 @@ export const cleanupRootFocusTrapAccessibility = () => { * below, which have to stay in lockstep. */ const applyRootLock = (el: OverlayWithFocusTrapProps) => { + const viewContainer = getViewContainer(); // Hiding the container the overlay now sits in would hide the overlay too. - if (!getViewContainer()?.contains(el)) { + 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); }; diff --git a/core/src/utils/test/overlays/overlays.e2e.ts b/core/src/utils/test/overlays/overlays.e2e.ts index 7fedafa5ed8..644b1eb371b 100644 --- a/core/src/utils/test/overlays/overlays.e2e.ts +++ b/core/src/utils/test/overlays/overlays.e2e.ts @@ -652,7 +652,7 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => await expect(page.locator('#last')).toBeFocused(); }); - test('popover with keyboardClose false should keep focus on the input while presented', async ({ page }) => { + test('should keep focus on the input while presenting a popover with keyboardClose false', async ({ page }) => { test.info().annotations.push({ type: 'issue', description: 'https://github.com/ionic-team/ionic-framework/issues/31199', @@ -684,13 +684,14 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => // The input keeps focus while the popover is presented await expect(input).toBeFocused(); + await expect(page.locator('#ion-view-container-root')).not.toHaveAttribute('aria-hidden', 'true'); // Typing still goes into the input await page.keyboard.press('a'); await expect(input).toHaveValue('a'); }); - test('popover with default keyboardClose should remove focus from the input', async ({ page }) => { + test('should blur the input when presenting a popover with the default keyboardClose', async ({ page }) => { test.info().annotations.push({ type: 'issue', description: 'https://github.com/ionic-team/ionic-framework/issues/31199', @@ -719,7 +720,7 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => await expect(input).not.toBeFocused(); }); - test('popover with keyboardClose false should return focus to the input on dismiss', async ({ page }) => { + test('should return focus to the input when dismissing a popover with keyboardClose false', async ({ page }) => { test.info().annotations.push({ type: 'issue', description: 'https://github.com/ionic-team/ionic-framework/issues/31199',