feat: add onexit callback to all primitives#157
Merged
Conversation
Completes the enter/leave pair: onintersect fires on intersecting entries, onexit fires when an element transitions out of view. The initial off-screen report does not fire onexit — exit means "was in view, no longer is", tracked per element. - IntersectionObserver / MultipleIntersectionObserver: onexit prop - intersect / intersectAttachment: "exit" CustomEvent (onexit handler typed via the svelte/elements augmentation) - createIntersectionGroup: per-node onexit option - previous-state tracking resets when an observer is rebuilt (action config change, group shared-option rebuild)
render
Bot
temporarily deployed
to
metonym/pasted-text-review-v5 - svelte-intersection-observer PR #157
July 5, 2026 21:46
Destroyed
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.
Adds an onexit callback that fires when an observed element transitions from intersecting to not intersecting, completing the enter/leave pair alongside the existing onintersect. It does not fire on the initial off-screen report of an element that starts out of view, since that isn't really an exit.
Implemented across all primitives: IntersectionObserver and MultipleIntersectionObserver get an onexit prop, the intersect action/attachment dispatch an exit CustomEvent (typed via the svelte/elements augmentation), and createIntersectionGroup gets a per-node onexit option. Each requires tracking the previous intersecting state per element, which is reset whenever the underlying observer is rebuilt (action config change, group shared-option rebuild) so a fresh initial report never fires a phantom exit.
Added unit tests for the transition-only firing behavior, initial-report suppression, and reset-on-rebuild across each primitive, plus type tests. Updated README docs, including renaming the callbacks section and fixing its inbound anchor links, and reworking the autoplaying-video example to use the onintersect/onexit pair.
Main risk is around the state tracking: IntersectionObserver relies on the existing bindable intersecting prop as the transition source, and createIntersectionGroup uses a parallel map keyed by element. If a consumer clears that map or writes to intersecting directly in an unexpected way, onexit could misfire or fail to fire, but this mirrors how the existing onintersect logic already depends on that state.