diff --git a/core/src/utils/overlays.ts b/core/src/utils/overlays.ts index 944ee227638..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); }; @@ -579,7 +586,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 +679,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..644b1eb371b 100644 --- a/core/src/utils/test/overlays/overlays.e2e.ts +++ b/core/src/utils/test/overlays/overlays.e2e.ts @@ -651,5 +651,113 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => await expect(page.locator('#last')).toBeFocused(); }); + + 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', + }); + // 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(); + 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('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', + }); + 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('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', + }); + // 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(); + }); }); });