fix(scroll): reference-count locks so stacked locks release correctly#640
Merged
Conversation
size-limit report 📦
|
lockScroll early-returned undefined when the container was already locked, so a second locker (e.g. a modal opening a dropdown, or two modals on the document) got no unlock function and its callback never fired. Worse, releasing the first lock restored the scroll while the second was still open. Track an active-lock count per container: apply styles only for the first lock, restore only when the last lock is released, and always return a valid (idempotent) unlock function. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01We9NqptZjfbvXRL4hE1xri
62819da to
e46a561
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
lockScrollshort-circuited when the container was already locked:This returns
undefined, so a second locker (e.g. a modal opening a dropdown, or two modals both locking the document) gets no unlock function and itscallbacknever fires. And because there's no tracking of who holds the lock, releasing the first lock restores the scroll while the second is still open — the page becomes scrollable under an open overlay, and the second overlay has no way to unlock.Fix
Reference-count active locks per container (via
WeakMap): apply the style changes only for the first lock, restore only when the last lock is released, and always return a valid, idempotent unlock function.Behavioural note:
lockScrollnow always returns an unlock function (previously it could returnundefined). Callers that didunlock?.()are unaffected.Verification
Logic simulated:
tsc --noEmit(utilities) → 0 errors.oxlint→ clean.How to test
Found during a full package bug review. Complementary to the StyleCache-scoping and iOS-guard PRs in the same cluster.
Generated by Claude Code