Skip to content

feat: add onexit callback to all primitives#157

Merged
metonym merged 1 commit into
masterfrom
metonym/pasted-text-review-v5
Jul 5, 2026
Merged

feat: add onexit callback to all primitives#157
metonym merged 1 commit into
masterfrom
metonym/pasted-text-review-v5

Conversation

@metonym

@metonym metonym commented Jul 5, 2026

Copy link
Copy Markdown
Owner

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.

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
render Bot temporarily deployed to metonym/pasted-text-review-v5 - svelte-intersection-observer PR #157 July 5, 2026 21:46 Destroyed
@metonym
metonym merged commit e1a3caa into master Jul 5, 2026
3 checks passed
@metonym
metonym deleted the metonym/pasted-text-review-v5 branch July 5, 2026 21:48
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.

1 participant