Skip to content

fix(DismissableLayer): keep body pointer-events locked when the last scroll lock releases - #2832

Merged
zernonia merged 2 commits into
unovue:v2from
61021:fix/dismissable-layer-body-lock-handoff
Aug 4, 2026
Merged

zernonia merged 2 commits into
unovue:v2from
61021:fix/dismissable-layer-body-lock-handoff

Conversation

@61021

@61021 61021 commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2784

What happens

useBodyScrollLock and DismissableLayer both write document.body.style.pointerEvents without consulting each other, and the scroll lock's resetBodyStyle() — which runs when the last scroll-lock holder releases — cleared it unconditionally.

In the repro, the last scroll-lock holder is the closing popover itself: PopoverContentModal holds useBodyScrollLock(true) until its content unmounts, i.e. until the exit animation finishes. The dialog that opened mid-animation disables outside pointer events through its DismissableLayer, but holds no scroll lock of its own — a dialog's lock lives on DialogOverlayImpl, and the repro (legitimately) renders no overlay. So when the popover's exit animation ends, its scroll lock is the last one out and wipes the body lock the dialog still depends on.

Instrumented trace of one handoff on v2 HEAD (temporary logs in both writers):

+840ms   DismissableLayer apply: size 0→1, saved original "" → body 'none'   (popover layer)
+848ms   useBodyScrollLock nextTick apply: body 'none'                        (popover lock)
+1075ms  DismissableLayer add: size 1→2                                       (dialog layer)
+1484ms  useBodyScrollLock resetBodyStyle: body ''      ← popover unmounts: the stomp
+1486ms  DismissableLayer delete: size 2→1 → correctly no restore             (damage done)

DismissableLayer's own bookkeeping is correct throughout — the dialog's layer is registered, and the departing layer doesn't restore because it isn't the last one. Nothing re-applies the style afterwards because no layer-registry state changes again.

Relationship to the issue's analysis

The watchEffect re-trigger mechanism described in #2784 matches the pre-2.9.10 DismissableLayer and was fixed by #2678 (the repro pins 2.9.6, which predates it). The leak itself is not stale, though — running the repro's own Auto-run ×20 (headless Chromium, its "exit animation played" self-check green):

build result
2.9.6 (repro's pin) 20/20 leaks
2.9.9 20/20
2.9.10 (first release with #2678) 20/20
2.10.1 (latest) 20/20
v2 HEAD 20/20
v2 HEAD + this PR 0/20

The exit animation isn't essential either: the same stomp happens with no animation at all when a modal popover opens and closes over an overlay-less modal dialog (covered by the second regression test).

The fix

resetBodyStyle() now leaves pointerEvents alone while any DismissableLayer still has disableOutsidePointerEvents active; that layer's cleanup restores it once the last disabling layer is gone. The scroll lock's other resets (overflow, padding/margin, --scrollbar-width) are unchanged.

To let useBodyScrollLock (in shared/) read the layer registry without importing the component — DismissableLayer.vue imports from the shared barrel, so that would be a cycle — the module-level context moves verbatim to DismissableLayer/context.ts. The .vue re-exports it, so the existing import surface is unchanged (context isn't in the package's public exports; the only other consumer, DismissableLayerBranch.vue, is updated).

Tests

  • Two red→green regressions for this bug: a scroll-lock-holding modal layer unmounting while an overlay-less modal layer is open — in both open orders (popover first / dialog first).
  • Two more pinning the layer-registry half: the older sibling layer leaving while a newer one is open — the mirrored direction of the [Bug]: Dropdown inside dialog strips pointer-events style from body #2674 suite, which only covered inner-closes-first.
  • Full core suite: 95 files / 1974 tests green; vue-tsc and lint clean.

Out of scope (happy to follow up)

The underlying shape — two owners writing one property — has two more edges this PR deliberately doesn't touch:

  1. DismissableLayer can capture the scroll lock's 'none' as originalBodyPointerEvents (when a layer mounts while a bodyLock is already active) and write it back on final restore, leaving the body at pointer-events: none with nothing open. Pre-existing, reachable both before and after this PR.
  2. The reverse stomp: DismissableLayer's restore doesn't consult still-active scroll locks.

A shared, refcounted owner for body pointer-events (first acquirer saves the original, last release restores it) would close the whole class, and seems to line up with where #2822 wants v3 to go. Glad to do that as a follow-up if you'd take it — kept this one minimal since the guarded path is the one #2784 actually hits.

Summary by CodeRabbit

  • Bug Fixes
    • Improved modal and dismissible-layer handoffs to keep background interaction blocked while another layer remains active.
    • Ensured body interaction styles are restored correctly only after all active layers are closed.
    • Added regression coverage for scroll-lock transitions and sibling modal layers.

…scroll lock releases

useBodyScrollLock's resetBodyStyle cleared document.body.style.pointerEvents
unconditionally once the last scroll-lock holder released. When that holder
was a closing modal Popover whose content unmounts after its exit animation,
and a modal layer without its own scroll lock (e.g. a Dialog rendered without
an Overlay) was already open, the body lock owned by DismissableLayer was
wiped and everything behind the open modal became clickable (unovue#2784).

Only clear body pointer-events when no DismissableLayer currently disables
outside pointer events. The layer registry moves to a plain module
(DismissableLayer/context.ts) so useBodyScrollLock can consult it without
importing the component and creating a cycle through the shared barrel.

Fixes unovue#2784

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0218adbb-767e-41f7-b919-af5228a5794b

📥 Commits

Reviewing files that changed from the base of the PR and between d06df3b and ddae02d.

📒 Files selected for processing (1)
  • packages/core/src/DismissableLayer/DismissableLayer.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/DismissableLayer/DismissableLayer.test.ts

📝 Walkthrough

Walkthrough

This change centralizes DismissableLayer state in a shared reactive context. Body scroll-lock cleanup preserves document.body.style.pointerEvents while another modal layer remains active. Regression tests cover layered handoffs and final restoration.

Changes

Dismissable layer lock coordination

Layer / File(s) Summary
Shared dismissable-layer registry
packages/core/src/DismissableLayer/context.ts, packages/core/src/DismissableLayer/DismissableLayer.vue, packages/core/src/DismissableLayer/DismissableLayerBranch.vue
DismissableLayer and DismissableLayerBranch now use the shared reactive context registry for layer and pointer-event state.
Pointer-event lock handoff and regression coverage
packages/core/src/shared/useBodyScrollLock.ts, packages/core/src/DismissableLayer/DismissableLayer.test.ts
Body pointer-event cleanup preserves the lock while any modal layer remains active. Tests cover sibling-layer transitions, cleanup order, and restoration after the final layer closes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

Possibly related PRs

  • unovue/reka-ui#2678 — Both PRs modify DismissableLayer pointer-event state management and layered cleanup tests.
  • unovue/reka-ui#2790 — Both PRs modify shared layer context and body pointer-event cleanup.
  • unovue/reka-ui#2801 — Both PRs modify useBodyScrollLock and dismissable-layer lock coordination.

Sequence Diagram(s)

sequenceDiagram
  participant Popover
  participant Dialog
  participant DismissableLayerContext
  participant BodyScrollLock
  participant DocumentBody
  Popover->>DismissableLayerContext: Register modal layer
  DocumentBody->>DocumentBody: Set pointerEvents to none
  Dialog->>DismissableLayerContext: Register while Popover closes
  Popover->>DismissableLayerContext: Remove closing layer
  DismissableLayerContext->>BodyScrollLock: Report active Dialog layer
  BodyScrollLock->>DocumentBody: Preserve pointerEvents none
  Dialog->>DismissableLayerContext: Remove final modal layer
  BodyScrollLock->>DocumentBody: Restore original pointerEvents
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the fix that preserves body pointer-events locking during DismissableLayer handoffs.
Linked Issues check ✅ Passed The changes address issue #2784 by retaining body pointer-events blocking until the last modal DismissableLayer closes.
Out of Scope Changes check ✅ Passed All changes support the pointer-events locking fix, including shared context access, cleanup logic, and regression tests.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/reka-ui@2832

commit: ddae02d

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/DismissableLayer/DismissableLayer.test.ts`:
- Around line 137-141: Update the beforeEach for “sibling layers with
disableOutsidePointerEvents” to clear both module-level registries,
context.layersRoot and context.layersWithOutsidePointerEventsDisabled, before
each test. Keep the existing document body and pointer-events resets unchanged,
matching the cleanup used in the “scroll-lock handoff” describe block.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: cf37d833-9c1f-438d-8761-2d67c29cd371

📥 Commits

Reviewing files that changed from the base of the PR and between c6e1460 and d06df3b.

📒 Files selected for processing (5)
  • packages/core/src/DismissableLayer/DismissableLayer.test.ts
  • packages/core/src/DismissableLayer/DismissableLayer.vue
  • packages/core/src/DismissableLayer/DismissableLayerBranch.vue
  • packages/core/src/DismissableLayer/context.ts
  • packages/core/src/shared/useBodyScrollLock.ts

…reEach

Matches the scroll-lock handoff suite so the suite stays order-independent
even if a failing test skips its unmounts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@zernonia
zernonia merged commit a24ea69 into unovue:v2 Aug 4, 2026
7 checks passed
zernonia pushed a commit that referenced this pull request Sep 4, 2026
…the registry

Three fixes that shipped on v2 after the layer stack manager landed
patched code the manager replaced, so they never reached v3:

- #2863: outside-pointer subscribers now pass `() => props.present` as
  `enabled`. A hidden `unmountOnHide: false` layer no longer keeps a
  touch deferral alive and dismisses itself on the tap that opened it.
- #2892: the inside-tree branch of `handlePointerDown` now cancels a
  pending touch deferral, so a scroll-cancelled outside tap cannot fire
  a stale `pointerDownOutside` on the next inside tap.
- #2832: `layerStack` exports `hasBodyPointerEventsLock()` and
  `useBodyScrollLock` consults it before clearing body `pointer-events`,
  so releasing the last scroll lock no longer unlocks a modal layer that
  has no scroll lock of its own (#2784).

And one bug specific to v3: layer ordering was read from a document-wide
`querySelectorAll('[data-dismissable-layer]')`, which cannot see layers
inside shadow roots, so two nested layers in one shadow root both
indexed -1 and a pointerdown inside the child dismissed the parent.
Ordering now comes from the manager's own registry, with composed
containment checks. Subscribers that never register a stack layer
(Editable) keep the previous document-order rule.

Tests are ported from the v2 commits and adapted to `resetLayerStack()`;
a shadow-root case covers the ordering fix.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RjAHnVc7rPjMzA2srYUtiu
zernonia added a commit that referenced this pull request Sep 4, 2026
… review (#2908)

* fix(Label): forward attrs on the `#render` path

Label kept Vue's default `inheritAttrs`, but `useRender`'s `#render` slot
makes the component root a fragment, so class, id, aria-* and listeners
passed by the consumer were silently dropped with an "Extraneous non-props
attributes" warning.

Mirror the `Primitive` compat shim: disable attr inheritance and fold
`useAttrs()` into the props handed to `useRender`, so attrs reach the
element exactly once on both the default and the `#render` path. Consumer
listeners merge after Label's own, matching Vue's fallthrough order.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RjAHnVc7rPjMzA2srYUtiu

* fix(RadioGroup): stop falling back to the item value for aria-label

Port of #2861 onto the v3 shadow-DOM-aware lookup. The item value is not
an accessible name, so `aria-label` now resolves only from an associated
`[for]` label (or an explicit attribute) and is otherwise omitted.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RjAHnVc7rPjMzA2srYUtiu

* test(DismissableLayer): make the dispatch benchmarks exercise the manager

Two of the three benches never reached the code they claim to measure:
the outside-pointerdown bench dispatched before subscribers armed (arming
is a macrotask after registration), and the Escape bench dispatched a
non-bubbling keydown on `document` while the shared listener lives on
`window`. Both early-returned on every iteration.

Mount and arm in `setup`, dispatch alone in the measured body, and bubble
the keydown from the top layer element. Document what each bench measures
and that only ratios against a v2 run are meaningful.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RjAHnVc7rPjMzA2srYUtiu

* fix(Switch): run consumer listeners before the internal toggle

The `useSwitch()` conversion bound the composable's `onClick`/`onKeydown`
ahead of `$attrs` in one `mergeProps` call. `mergeProps` chains same-named
listeners in argument order, so the internal toggle ran first and a
consumer `@click` observed the post-toggle model, unlike v2 where
`v-bind="$attrs"` preceded `@click`.

Bind the composable's handlers as directives after `v-bind`, which
restores v2 precedence on both axes: a consumer attribute still overrides
the component's `role`/`aria-*`, and a consumer listener still runs first.
The characterization tests now assert the order instead of only that
both listeners fire.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RjAHnVc7rPjMzA2srYUtiu

* fix(DismissableLayer): port v2 dismissal fixes and order layers from the registry

Three fixes that shipped on v2 after the layer stack manager landed
patched code the manager replaced, so they never reached v3:

- #2863: outside-pointer subscribers now pass `() => props.present` as
  `enabled`. A hidden `unmountOnHide: false` layer no longer keeps a
  touch deferral alive and dismisses itself on the tap that opened it.
- #2892: the inside-tree branch of `handlePointerDown` now cancels a
  pending touch deferral, so a scroll-cancelled outside tap cannot fire
  a stale `pointerDownOutside` on the next inside tap.
- #2832: `layerStack` exports `hasBodyPointerEventsLock()` and
  `useBodyScrollLock` consults it before clearing body `pointer-events`,
  so releasing the last scroll lock no longer unlocks a modal layer that
  has no scroll lock of its own (#2784).

And one bug specific to v3: layer ordering was read from a document-wide
`querySelectorAll('[data-dismissable-layer]')`, which cannot see layers
inside shadow roots, so two nested layers in one shadow root both
indexed -1 and a pointerdown inside the child dismissed the parent.
Ordering now comes from the manager's own registry, with composed
containment checks. Subscribers that never register a stack layer
(Editable) keep the previous document-order rule.

Tests are ported from the v2 commits and adapted to `resetLayerStack()`;
a shadow-root case covers the ordering fix.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RjAHnVc7rPjMzA2srYUtiu

* test(Label, DismissableLayer): fix two new tests that failed in CI

- Label: VTU's `trigger` cannot set `detail` on a UIEvent in jsdom
  (getter-only), so dispatch a real `MouseEvent` with `detail: 2`.
- DismissableLayer shadow-root case: the child's outside subscriber arms on
  a macrotask scheduled during the re-render microtask, which was queued
  after the test's `sleep` timer. The first pointerdown then hit an unarmed
  subscriber and left `isPointerInside` set, so the outside pointerdown was
  treated as a cancelled inside press. Flush the render with `nextTick`
  before waiting for arming.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RjAHnVc7rPjMzA2srYUtiu

---------

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DismissableLayer modal: body pointer-events blocking is lost when one modal layer opens while another (animated) modal layer is closing

2 participants