fix(swmansion): report 0 as prevIndex for the programmatic open - #48
Merged
Merged
Conversation
`lastIndexRef` seeded from the `index` state, which starts at `openIndex` for portal and inline sheets — mounting *is* the open there, and native animates in from the collapsed detent. So `expand()` emitted `onIndexChange(openIndex, openIndex)` and consumers branching on the 0 -> open transition (haptics on open, for one) never fired. Portal sheets unmount on close, so the ref reset every cycle and the transition was never observable at all; only persistent sheets (`defaultIndex: -1`, so `index` starts at 0) got it right. Seed the tracker at 0 instead: the sheet is off screen until `expand()` lands, in every mode. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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
onIndexChange(nextIndex, prevIndex)never reports the0 -> opentransition for portal and inline sheets, so a consumer handler like this one never fires:lastIndexRef— the source ofprevIndex— was seeded from theindexstate, and that state does not start at 0 everywhere:defaultIndexindexexpand()emits0openIndex(openIndex, openIndex)❌BottomSheetPortal0openIndex(openIndex, openIndex)❌BottomSheetPersistent-10(openIndex, 0)✅For portal/inline sheets mounting is the open — native animates in from the collapsed detent — so the initial
indexis the animation's target, not the sheet's current position. SeedingprevIndexfrom it hides the very transition the second argument exists to expose.It fails on every open, not just the first:
close()does resync the tracker to 0, but a portal sheet unmounts on close, so the ref is recreated atopenIndexon the next mount.Fix
Seed the tracker at 0. The sheet is off screen until
expand()lands, in every mode, so 0 is the truthful previous position.Verified against a rendered adapter tree in both modes — before:
defaultIndex 0 -> [[1, 1]],defaultIndex -1 -> [[1, 0]]; after:[[1, 0]]in both.Note
Side effect worth a look: on a
switchrestore the coordinator drivesclose()thenexpand()again, so the restored sheet underneath now also reports(openIndex, 0)and would re-fire an open-haptic. That reads as correct to me — it is a fresh open from the consumer's side — but it is a behavior change.No regression test added yet; say the word and I'll add one to
src/__tests__/.yarn lint,yarn typecheck,yarn test(122/122) all pass.🤖 Generated with Claude Code