We are experiencing intermittent issues with scroll calculation in our Timeline component. The problem manifests as incorrect scroll positioning, sometimes resulting in unexpected lateral movement instead of the intended vertical scroll.
Observed Behavior
Scroll values are sometimes calculated incorrectly, causing the viewport to shift horizontally instead of vertically.
Log entries show sudden jumps in scroll values, indicating potential calculation errors.
The issue appears to be more prevalent during rapid scrolling or when crossing certain thresholds in the timeline.
In these log entries, we can see abrupt changes in the second and third values, which represent scroll positions. This inconsistency likely contributes to the incorrect scroll behavior.
Current Implementation
The current implementation uses a handleWheel function to process scroll events:
It calculates a new scale based on the wheel delta.
It determines the new scroll position using the current scroll, mouse position, and new scale.
The scroll position is updated using a deltaScrollX function, which applies limits to the scroll range.
Potential Causes
Rounding errors or type conversion issues in scroll calculations.
Asynchronous state updates leading to inconsistent values.
Performance issues related to frequent calls to getBoundingClientRect().
Lack of smoothing or interpolation for scroll values, causing jumps.
Proposed Solutions
Implement a singleton ScrollManager class to centralize scroll state management.
Add checks for maximum allowed scroll change per event to prevent large jumps.
Use requestAnimationFrame for smoother scroll updates.
Cache and update getBoundingClientRect() values more efficiently.
Optimize the handleWheel function using useCallback.
Next Steps
Implement and test the proposed solutions.
Add more comprehensive logging to track scroll behavior over time.
Consider adding unit tests to verify correct scroll calculations.
Scroll Calculation Problem in Timeline Component
Issue Description
We are experiencing intermittent issues with scroll calculation in our Timeline component. The problem manifests as incorrect scroll positioning, sometimes resulting in unexpected lateral movement instead of the intended vertical scroll.
Observed Behavior
Example Log Output
In these log entries, we can see abrupt changes in the second and third values, which represent scroll positions. This inconsistency likely contributes to the incorrect scroll behavior.
Current Implementation
The current implementation uses a
handleWheel
function to process scroll events:deltaScrollX
function, which applies limits to the scroll range.Potential Causes
getBoundingClientRect()
.Proposed Solutions
requestAnimationFrame
for smoother scroll updates.getBoundingClientRect()
values more efficiently.handleWheel
function usinguseCallback
.Next Steps
Additional Information