-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix animateMini crash when element removed from DOM during scroll cancellation #3510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Code Review: PR #3510SummaryThis PR fixes a crash in ✅ Positive Aspects1. Correct Problem IdentificationThe fix correctly identifies that calling WAAPI's 2. Minimal, Focused ChangeThe implementation change is surgical - only adding the necessary guard without over-engineering. This follows the CLAUDE.md guidance to avoid over-engineering solutions. 3. Good Test Coverage
🔍 Potential Issues & Suggestions1. Missing Test: Verify commitStyles Actually CalledSeverity: Medium The current tests only verify "does not throw", but don't verify that test("should call commitStyles when element is connected", () => {
const element = document.createElement("div")
document.body.appendChild(element)
const animation = new NativeAnimation({
element,
name: "opacity",
keyframes: [0, 1],
duration: 1000,
})
// Spy on the underlying WAAPI animation's commitStyles
const commitStylesSpy = jest.spyOn(animation['animation'], 'commitStyles')
animation.stop()
expect(commitStylesSpy).toHaveBeenCalled()
document.body.removeChild(element)
})2. Optional Chaining on elementSeverity: Low The code uses 3. Missing Edge Case: Element Connected but Not in DocumentSeverity: Low An element can have
The current test only covers scenario #2. Consider adding a test for scenario #1: test("should not throw when element never added to DOM", () => {
const element = document.createElement("div")
// Note: element NOT added to document.body
const animation = new NativeAnimation({
element,
name: "opacity",
keyframes: [0, 1],
duration: 1000,
})
expect(() => animation.stop()).not.toThrow()
})4. Test Cleanup ConsistencySeverity: Low In the second test (lines 25-43), you clean up the element after the test. In the first test (lines 5-23), the element is already removed so no cleanup is needed. This is fine, but consider using describe("commitStyles", () => {
let element: HTMLDivElement
beforeEach(() => {
element = document.createElement("div")
})
afterEach(() => {
if (element.isConnected) {
document.body.removeChild(element)
}
})
// ... tests
})🎯 Best Practices & Code Quality✅ Follows Repository Conventions
✅ Security ConsiderationsNo security concerns. This is purely defensive error handling. ✅ Performance ConsiderationsThe 📋 Final RecommendationsRequired Before Merge:
Recommended Before Merge:
Optional Enhancements:
✅ Overall AssessmentVerdict: Approve with minor suggestions This is a solid bug fix that:
The suggestions above are primarily about test coverage completeness rather than functional issues. The core fix is sound and ready to merge with the minor test improvements. Great work! 🎉 |
…cellation Check if element is still connected to the DOM before calling commitStyles() in NativeAnimation. This prevents the WAAPI commitStyles() method from throwing when the animation is stopped after the element has been removed from the DOM (e.g., in disconnectedCallback). Added Playwright E2E test to verify the fix. Fixes #3509 https://claude.ai/code/session_01Cp6uaRWywEQbL3zyMCNkfN
9517b26 to
c84f4cf
Compare
Code ReviewSummaryThis PR fixes a crash in ✅ Positive Aspects
🔍 Code Quality Observationspackages/motion-dom/src/animation/NativeAnimation.ts:181-184 protected commitStyles() {
const element = this.options?.element
if (!this.isPseudoElement && element?.isConnected) {
this.animation.commitStyles?.()
}
}The logic is sound and follows defensive programming practices. The conditions are properly ordered:
🧪 Test Coverage AssessmentStrengths:
Minor observation: 🚀 Performance Considerations
🔒 Security ConsiderationsNo security concerns. This is a defensive fix that prevents a method from being called on disconnected elements. 🎯 RecommendationsThe PR is well-implemented and ready to merge. No changes required. Optional enhancement (future consideration): ✨ ConclusionApproval Status: ✅ Approved This is a clean, well-tested bug fix that solves the reported issue without introducing complexity or side effects. The fix aligns with the repository's code style guidelines and includes appropriate test coverage. Great work on the thorough test coverage and minimal implementation! 🎉 |
Check if element is still connected to the DOM before calling commitStyles()
in NativeAnimation. This prevents the WAAPI commitStyles() method from throwing
when the animation is stopped after the element has been removed from the DOM
(e.g., in disconnectedCallback).
Fixes #3509