Skip to content

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

Open

Description

@srobroek

Prerequisites

  • I have read the Contributing Guidelines.
  • I agree to follow the Code of Conduct.
  • I have searched for existing issues that already report this problem, without success.

Ionic Framework Version

v9.x, Nightly

Current Behavior

When a second sheet modal is presented before the first sheet's present() has resolved, the page throws RangeError: Maximum call stack size exceeded and the UI stops responding.

The loop runs like this:

  1. When the first modal's enter animation ends, it focuses its wrapper. The second modal is already the topmost overlay.
  2. The overlay focus trap (trapShadowFocus in core/src/utils/overlays.ts, a capture listener on document) sees focus outside the topmost overlay. The second modal has no element that matches focusableQueryString, so the trap focuses the second modal's host synchronously.
  3. The first modal's onModalFocus (core/src/components/modal/modal.tsx, lines 1225-1236 on main) sees its host focused with no focused element in its shadow root, and calls dragHandleEl.focus() (line 1234). In v9 every sheet has a focusable handle, because handleBehavior defaults to cycle.
  4. The trap moves focus back to the second modal, and steps 3 and 4 repeat until the call stack overflows.

If the second modal contains a radio group, the trap focuses it through the async setFocus(), and the loop does not start. The guard added in #31293 (shadowRoot.activeElement == null) does not stop this case, because the host itself is focused and nothing inside it is.

Stack (Chromium, 9.0.10, unminified build):

RangeError: Maximum call stack size exceeded.
    at focusVisibleElement (helpers.js:165:6)
    at focusElementInContext (overlays.js:124:7)
    at focusFirstDescendant (overlays.js:106:3)
    at trapShadowFocus (overlays.js:239:7)
    at trapKeyboardFocus (overlays.js:247:5)
    at HTMLDocument.<anonymous> (overlays.js:256:7)

Results from the reproduction, 20 headless Playwright runs per cell:

@ionic/core Chromium WebKit
9.0.10 20 of 20 18 of 20
9.0.11 20 of 20 19 of 20
9.0.12-nightly.20261116 20 of 20 20 of 20

Expected Behavior

Presenting a second sheet before the first sheet has finished presenting does not loop. A modal that is not the topmost overlay does not move focus to its own drag handle while the focus trap moves focus into the overlay above it.

Steps to Reproduce

  1. Clone the reproduction, then run npm install and npx vite.
  2. Open the page and the browser console.
  3. Tap Present both. It calls first.present() without awaiting it, then calls second.present().
  4. The console shows RangeError: Maximum call stack size exceeded.

Code Reproduction URL

https://github.com/srobroek/ionic-sheet-focus-repro

Ionic Info

The reproduction uses @ionic/core only, with no framework, built with Vite 7.

Ionic CLI       : 7.2.1
@ionic/core     : 9.0.10 (also tested 9.0.11 and 9.0.12-nightly.20261116)
NodeJS          : v24.22.0
npm             : 11.19.1
OS              : Linux 6.12
Browsers        : Playwright 1.65.0 Chromium and WebKit, headless

Additional Information

Possible fix: in onModalFocus, call dragHandleEl.focus() only when this modal is the topmost presented overlay, for example when getPresentedOverlay(document, 'ion-modal') === this.el (core/src/utils/overlays.ts, line 481).

Workaround: a capture-phase focus listener on document.documentElement that calls stopPropagation() when the event target is an ion-modal that is not document.activeElement. The reproduction includes it behind ?guard=1, and with it the error occurred in 0 of 20 runs on each version in Chromium.

Related: #24491 is an older focus trap recursion with stacked overlays, fixed in #24611. #31293 added the host-focus guard in onModalFocus.

This issue and the reproduction were prepared with the help of an AI assistant. I verified that the reproduction triggers the error and that the workaround removes it.

Activity

  1. changed the title [-]bug: stacked sheet modals recurse infinitely (RangeError) when a lower sheet's present() finishes after an upper one is presented[/-] [+]bug: ion-modal sheets presented back to back overflow the call stack[/+] on Nov 16, 2026
  2. thetaPC commented on Nov 16, 2026

    @thetaPC
    Contributor

    Thanks for the report, and for the detailed analysis.

    Verified on 9.0.11. When the first sheet's present() finishes after the second sheet is already open, the first sheet focuses its dialog, the overlay focus trap moves focus into the top sheet, and the first sheet's focus handler then runs on that same event. By then nothing inside the first sheet is focused, so the handler sends focus to the first sheet's drag handle, and the trap sends it back. The loop happens whether or not the top sheet has focusable content: a button inside it still loops, and only a radio group avoids it, because the trap focuses that one asynchronously.

    Workarounds, all verified on 9.0.11:

    • Wait for the first sheet to finish presenting before presenting the second:
    await firstModal.present();
    await secondModal.present();
    • Set handle-behavior="none" on the sheets. This also removes tapping the handle to cycle through breakpoints.
    • The capture-phase focus listener from your reproduction also stops it, but awaiting present() is the smaller change.

    Tracked as a bug in core.

  3. joewoodhouse commented on Nov 19, 2026

    @joewoodhouse
    Contributor

    This also reproduces on v8 when the sheets set handleBehavior="cycle" explicitly, so it is not limited to v9's new default.

    I ran the same back-to-back case (first.present() not awaited, then await second.present(), both sheets with non-focusable content) against @ionic/core from jsDelivr with headless Playwright:

    @ionic/core handleBehavior Chromium WebKit
    8.7.17 cycle overflows overflows
    8.7.17 default (none) no error no error
    8.8.20 cycle overflows overflows
    8.8.20 default (none) no error no error
    9.0.12 default (cycle) overflows overflows

    One run per cell, so I can't speak to how often it happens.

    A second way into the same loop on 8.x: with a cycle sheet already presented and another modal presented on top of it, calling focus() on the lower sheet's host overflows the stack on 8.7.17, 8.8.15, 8.8.16, 8.8.20 and 9.0.12, whether or not the top modal has a focusable button. In WebKit a Tab key press does the same on 8.x.

    We see this in production on 8.7.17 (iOS WKWebView) with a cycle sheet that can have another overlay presented over it. In my reproduction the page stayed usable after the error: taps, typing and dismissing still worked.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions