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(); + }); +});