Skip to content

Commit 4ff0b72

Browse files
kaigritunKai Gritun
andauthored
fix: prevent duplicate MutationObserver when autoAnimate called twice (#234)
When autoAnimate() is called multiple times on the same element (e.g., in React StrictMode which renders components twice), a new MutationObserver was being created each time. This caused child-added animations to fail because multiple observers were interfering with each other. Now we check if a MutationObserver already exists for the element before creating a new one. This ensures idempotent behavior when autoAnimate() is called repeatedly on the same element. Fixes #232 Co-authored-by: Kai Gritun <kai@kaigritun.com>
1 parent 624d9c1 commit 4ff0b72

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎src/index.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -909,9 +909,11 @@ export default function autoAnimate(
909909
...(config as Partial<AutoAnimateOptions>),
910910
})
911911
}
912-
const mo = new MutationObserver(handleMutations)
913-
mo.observe(el, { childList: true })
914-
mutationObservers.set(el, mo)
912+
if (!mutationObservers.has(el)) {
913+
const mo = new MutationObserver(handleMutations)
914+
mo.observe(el, { childList: true })
915+
mutationObservers.set(el, mo)
916+
}
915917
parents.add(el)
916918
}
917919
}

0 commit comments

Comments
 (0)