Skip to content

fix(modal): only the topmost sheet modal redirects focus to its handle - #31805

Open
AdityaxDeore wants to merge 1 commit into
ionic-team:mainfrom
AdityaxDeore:fix/31757-modal-sheet-focus-loop
Open

AdityaxDeore wants to merge 1 commit into
ionic-team:mainfrom
AdityaxDeore:fix/31757-modal-sheet-focus-loop

Conversation

@AdityaxDeore

Copy link
Copy Markdown

Fixes #31757

Problem

Two ion-modal sheets presented back to back overflow the call stack. When the focus trap moves focus between the sheets, the covered sheet's onModalFocus (added by #31293) also redirects focus to its own drag handle, the trap moves focus back again, and the two ping-pong until the stack overflows. It also reproduces on v8 when the sheets set handleBehavior="cycle" explicitly.

Fix

Guard the redirect in onModalFocus (core/src/components/modal/modal.tsx) so only the topmost presented modal redirects focus to its handle:

getPresentedOverlay(document, 'ion-modal') === el

A covered sheet receiving focus directly now leaves focus on its host, where the trap can move it to the top overlay without a second redirect. No other behaviour changes: the guard only narrows when the existing redirect fires.

Testing

New regression test core/src/components/modal/test/modal-sheet-focus.spec.tsx (Stencil spec test, two sheet modals with handleBehavior="cycle"; the overlay-hidden class is removed from both hosts to simulate presentation, which is what makes getPresentedOverlay() count a modal as presented):

  • Covered sheet: focus on its host must NOT call its drag handle's focus
  • Topmost sheet: focus on its host MUST still call its drag handle's focus

Fail-before on the unfixed code: 1 failed / 1 passed (the covered-sheet test failed — the handle's focus was called). Pass-after: 2/2. Neighbouring specs (modal-attributes, modal-id, overlay-focus-boundary): 3 suites, 15 tests, all passed. Core build, eslint and prettier are clean.

Disclosure: AI coding assistance was used in preparing this change.

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 ionic-team#31757
@AdityaxDeore
AdityaxDeore requested a review from a team as a code owner November 23, 2026 08:21
@AdityaxDeore
AdityaxDeore requested a review from ShaneK November 23, 2026 08:21
@vercel

vercel Bot commented Nov 23, 2026

Copy link
Copy Markdown

@AdityaxDeore is attempting to deploy a commit to the Ionic Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the package: core @ionic/core package label Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: core @ionic/core package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ion-modal sheets presented back to back overflow the call stack

1 participant