Repository navigation
[scroll area] Fix thumb-drag divide-by-zero and per-scroll re-render - #5099
Conversation
The thumb is floored at MIN_THUMB_SIZE, so a short or heavily padded scrollbar can drive `maxThumbOffset` to zero or negative. The drag and track-click handlers divided by it, producing a non-finite or inverted scroll position. The browser then clamps that to an edge, so a tiny thumb drag or a track click teleports the scroll to the top/bottom instead of moving proportionally. Guard the divisor (`<= 0` -> ratio 0) on both axes in both handlers, matching the existing divide guard in the render path. Separately, `computeThumbPosition` (called on every scroll frame) called `setCornerSize` with a fresh object literal and no equality guard, unlike its sibling setters. A new reference is never `Object.is`-equal, so the root context rebuilt and every scroll-area part re-rendered on each scroll frame even when the corner size was unchanged. Bail when unchanged. Add regression tests: Chromium drag tests asserting a degenerate-track drag does not jump the scroll, and a Chromium test asserting parts do not re-render on scroll when the corner size is unchanged.
commit: |
Bundle size
PerformanceTotal duration: 1,096.30 ms +57.78 ms(+5.6%) | Renders: 78 (+0) No significant changes — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
This PR hardens the Scroll Area component against edge cases where the scrollbar track is too small (or heavily padded), preventing divide-by-zero/invalid scroll math during thumb drag and track clicks, and reduces unnecessary re-renders by stabilizing cornerSize state updates during scrolling.
Changes:
- Guard thumb-drag and track-click scroll calculations against non-positive thumb travel ranges.
- Avoid rebuilding the root context on every scroll frame by skipping
cornerSizestate updates when unchanged. - Add browser-only regression tests for non-positive thumb offsets and for context stability during scroll.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/react/src/scroll-area/viewport/ScrollAreaViewport.tsx | Prevents per-scroll re-renders by returning the previous cornerSize object when dimensions don’t change. |
| packages/react/src/scroll-area/scrollbar/ScrollAreaScrollbar.tsx | Adds non-positive travel-range guards to prevent invalid track-click scroll calculations. |
| packages/react/src/scroll-area/scrollbar/ScrollAreaScrollbar.test.tsx | Adds browser-only tests covering thumb/track interactions when the thumb cannot travel. |
| packages/react/src/scroll-area/root/ScrollAreaRoot.tsx | Prevents thumb-drag divide-by-zero/invalid ratios by guarding against non-positive travel ranges. |
| packages/react/src/scroll-area/root/ScrollAreaRoot.test.tsx | Adds a browser-only test to ensure scrolling doesn’t re-render parts when corner size is stable. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
Fixes two Scroll Area issues: