Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion core/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
eventMethod,
FOCUS_TRAP_DISABLE_CLASS,
GESTURE,
getPresentedOverlay,
prepareOverlay,
present,
restoreRootFocusTrapAccessibility,
Expand Down Expand Up @@ -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();
}
};
Expand Down
58 changes: 58 additions & 0 deletions core/src/components/modal/test/modal-sheet-focus.spec.tsx
Original file line number Diff line number Diff line change
@@ -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: () => (
<div>
<ion-modal breakpoints={[0, 0.5, 1]} initialBreakpoint={0.5} handleBehavior="cycle" overlayIndex={1} />
<ion-modal breakpoints={[0, 0.5, 1]} initialBreakpoint={0.5} handleBehavior="cycle" overlayIndex={2} />
</div>
),
});
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();
});
});