Repository navigation
fix: don't re-observe a detached element after destroy(), and clean up a removed child on disable() - #247
Open
AndreasOhlsson wants to merge 2 commits into
Open
fix: don't re-observe a detached element after destroy(), and clean up a removed child on disable()#247AndreasOhlsson wants to merge 2 commits into
AndreasOhlsson wants to merge 2 commits into
Conversation
|
@AndreasOhlsson is attempting to deploy a commit to the Formkit Team on Vercel. A member of the Team first needs to authorize it. |
A position update queued with lowPriority() before destroy() can run after it. updatePos() then calls observePosition() on the detached element and creates a new IntersectionObserver rooted at document.documentElement, which keeps the detached subtree alive for the rest of the session. Fixes formkit#246
AndreasOhlsson
force-pushed
the
fix/observe-detached-element
branch
from
October 8, 2026 10:35
6d40a49 to
c029f01
Compare
disable() cancels every running animation on the parent's children. A cancelled animation never fires "finish", so a child removed just before disable() never reaches cleanUp: it stays in the DOM, absolutely positioned at its old place, and keeps its delete marker, so getElements bails and the parent stops animating until it unmounts. Finish the animation of a child marked for deletion instead, which runs its finish handler and so cleanUp. Every other child is still cancelled.
This branch has not been deployed
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.
Fixes #246, and a second 0.10 cleanup bug in
disable()(below).What happens
A position update queued with
lowPriority()(requestIdleCallback) beforedestroy()can still run after it, for example when the component unmounts during a busy route change.updatePos(el)then callsobservePosition(el), which creates a newIntersectionObserverrooted atdocument.documentElementon the now-detached element. Nothing disconnects that observer, and it keeps the detached subtree alive for the rest of the session.The fix
observePosition()returns early for an element that is no longer in the document, after disconnecting any old observer. Every path that (re)creates an observer goes through this function, so one guard covers all of them:updateAllPosfinishedhandlersObserving a disconnected element measures nothing, so no animation behaviour changes for connected elements.
Evidence
destroy()and ran after it.destroy().useAutoAnimateon a timeline that remounts on navigation), over 60 navigations:Relation to #242
#242 (which fixes #180) tracks the poll start timers and stops polling in cleanup. That closes the timer-side leaks, but an idle callback that is already queued still reaches
observePosition()afterdestroy(). The two changes complement each other and don't conflict.I didn't add a test. The race needs a busy main thread around unmount, which the existing coarse
memory.spec.tsdoesn't cover. Happy to add a targeted e2e test modelled on it if you'd like one.Second fix:
disable()during a removal leaves the removed child behindWhat happens
Suppose
disable()is called while a removed child is still playing its exit animation:getElementsreturns early while any child still carries the delete marker, so later adds and removes no longer animate until the parent unmounts.Why it happens
In 0.10,
disable()cancels every running animation on the parent's children. A cancelled animation never firesfinish, and thefinishlistener is what callscleanUpfor a removed child. In 0.8.2,disable()only removed the parent from the enabled set.Steps
const controller = autoAnimate(list), with the default 250 ms duration.controller.disable()within 250 ms.The fix
disable()now finishes the animation of a child marked for deletion, which runs itsfinishhandler and socleanUp. Every other child is still cancelled, sodisable()still stops in-flight animations immediately.Where we hit it: a list that disables animations while it preserves scroll position. Un-including one row removes it, and toggling another row within 250 ms disables the list mid-exit. We run this change as a local patch, and the ghost row is gone.
I can split this into its own PR if you'd prefer one fix per PR.
Diagnosed with heap snapshots and a Playwright repro.