From 5a033af73d94d475243c288a0dc8f50911ad9423 Mon Sep 17 00:00:00 2001 From: Aditya Chandrajit Deore <170283215+AdityaxDeore@users.noreply.github.com> Date: Mon, 23 Nov 2026 08:21:09 +0000 Subject: [PATCH] fix(modal): only the topmost sheet modal redirects focus to its handle When two sheet modals are presented back to back, the focus trap moves focus between them; the covered sheet's onModalFocus also redirected focus to its own drag handle, the trap moved focus back again, and the two ping-ponged until the call stack overflowed. Guard the redirect with getPresentedOverlay(document, 'ion-modal') === el so only the topmost presented modal redirects focus to its handle. A covered sheet receiving focus directly leaves focus on its host, where the trap can move it to the top overlay without a second redirect. Regression test: core/src/components/modal/test/modal-sheet-focus.spec.tsx (2 tests: covered sheet does not redirect, topmost sheet still does). Fail-before: 1 failed / 1 passed; pass-after: 2/2. Neighbouring specs (modal-attributes, modal-id, overlay-focus-boundary): 3 suites, 15 tests, all passed. Core build, eslint and prettier clean. Fixes #31757 --- core/src/components/modal/modal.tsx | 15 ++++- .../modal/test/modal-sheet-focus.spec.tsx | 58 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 core/src/components/modal/test/modal-sheet-focus.spec.tsx diff --git a/core/src/components/modal/modal.tsx b/core/src/components/modal/modal.tsx index 49e6731023c..d214f3147a6 100644 --- a/core/src/components/modal/modal.tsx +++ b/core/src/components/modal/modal.tsx @@ -16,6 +16,7 @@ import { eventMethod, FOCUS_TRAP_DISABLE_CLASS, GESTURE, + getPresentedOverlay, prepareOverlay, present, restoreRootFocusTrapAccessibility, @@ -1230,8 +1231,20 @@ export class Modal implements ComponentInterface, OverlayInterface { * true when a shadow child (the dialog wrapper present() focuses) is focused. * shadowRoot's activeElement is null only when the host was focused directly, * so redirect to the handle only then and leave present()'s wrapper focus intact. + * + * Only the topmost presented modal may redirect focus to its handle. + * When two sheets are presented back to back, the focus trap moves focus + * between them; if the covered sheet also redirected focus to its own + * handle, the trap would move focus back again, and the two would + * ping-pong until the call stack overflows. */ - if (ev.target === el && el.shadowRoot?.activeElement == null && dragHandleEl && dragHandleEl.tabIndex !== -1) { + if ( + ev.target === el && + el.shadowRoot?.activeElement == null && + dragHandleEl && + dragHandleEl.tabIndex !== -1 && + getPresentedOverlay(document, 'ion-modal') === el + ) { dragHandleEl.focus(); } }; diff --git a/core/src/components/modal/test/modal-sheet-focus.spec.tsx b/core/src/components/modal/test/modal-sheet-focus.spec.tsx new file mode 100644 index 00000000000..d9ccafb197f --- /dev/null +++ b/core/src/components/modal/test/modal-sheet-focus.spec.tsx @@ -0,0 +1,58 @@ +import { h } from '@stencil/core'; +import { newSpecPage } from '@stencil/core/testing'; + +import { Modal } from '../modal'; + +/** + * Regression tests for ionic-team/ionic-framework#31757: two sheet modals + * presented back to back overflow the call stack. + * + * When the focus trap moves focus between the sheets, the covered sheet's + * `onModalFocus` redirected focus to its own drag handle, the trap moved + * focus back again, and the two ping-ponged until the stack overflowed. + * Only the topmost presented modal may redirect focus to its handle. + * + * The `overlay-hidden` class is removed from both hosts to simulate + * presentation (`present()` removes it); the spec harness does not run the + * full presentation flow. + */ +describe('modal sheet focus', () => { + const setup = async () => { + const page = await newSpecPage({ + components: [Modal], + template: () => ( +
+ + +
+ ), + }); + const [first, second] = Array.from(page.body.querySelectorAll('ion-modal')); + // Simulate presentation: present() removes `overlay-hidden`, which is + // what makes getPresentedOverlay() count a modal as presented. + first.classList.remove('overlay-hidden'); + second.classList.remove('overlay-hidden'); + const firstHandle = first.shadowRoot!.querySelector('.modal-handle') as HTMLElement; + const secondHandle = second.shadowRoot!.querySelector('.modal-handle') as HTMLElement; + const focusFirstHandle = jest.spyOn(firstHandle, 'focus'); + const focusSecondHandle = jest.spyOn(secondHandle, 'focus'); + + return { first, second, focusFirstHandle, focusSecondHandle }; + }; + + it('should not redirect focus to the covered sheet modal handle', async () => { + const { first, focusFirstHandle } = await setup(); + + first.dispatchEvent(new FocusEvent('focus')); + + expect(focusFirstHandle).not.toHaveBeenCalled(); + }); + + it('should still redirect focus to the topmost sheet modal handle', async () => { + const { second, focusSecondHandle } = await setup(); + + second.dispatchEvent(new FocusEvent('focus')); + + expect(focusSecondHandle).toHaveBeenCalled(); + }); +});