Fix snapToCursor not working with initial coordinates#3445
Conversation
When using useDragControls with snapToCursor: true on a component with initial coordinates, the component would snap to the wrong position on the first drag. This was because the layout measurement includes the current transform value, but the snap calculation didn't account for it. The fix adds the current motion value to the snap calculation to ensure the element snaps to the correct cursor position regardless of its initial coordinates. Fixes #1607
Code ReviewSummaryThis PR fixes a bug where Code Quality ✅Strengths:
Minor suggestions:
Bug Fix Analysis ✅The fix is mathematically correct:
This explains why the second drag worked but the first didn't—after the first drag, the element was already offset incorrectly, but subsequent drags from that position appeared correct relative to where it landed. Test Coverage ✅Excellent test design:
Potential improvements:
Performance Considerations ✅
Security Concerns ✅No security issues identified. This is a pure calculation fix with no user input handling changes. Additional Observations
RecommendationsRequired before merge:
Optional enhancements:
ConclusionLGTM ✅ This is a well-crafted fix with excellent test coverage. The code is clean, well-documented, and solves the reported issue correctly. Great work! Review by Claude Code |
When using useDragControls with snapToCursor: true on a component with initial coordinates, the component would snap to the wrong position on the first drag. This was because the layout measurement includes the current transform value, but the snap calculation didn't account for it.
The fix adds the current motion value to the snap calculation to ensure the element snaps to the correct cursor position regardless of its initial coordinates.
Fixes #1607