Skip to content

[scroll area] Fix thumb-drag divide-by-zero and per-scroll re-render - #5099

Merged
atomiks merged 4 commits into
mui:masterfrom
atomiks:claude/jolly-bohr-525c5c
Jun 29, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
atomiks:claude/jolly-bohr-525c5c

Conversation

@atomiks

@atomiks atomiks commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes two Scroll Area issues:

  • Prevent thumb drag and track click math from dividing by a non-positive thumb travel range on very small or heavily padded scrollbars.
  • Avoid re-rendering Scroll Area parts on every scroll frame when the corner size has not changed.

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.
@atomiks atomiks added type: bug It doesn't behave as expected. component: scroll area Changes related to the scroll area component. labels Jun 22, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5099
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5099
    

commit: d411d2d

@code-infra-dashboard

code-infra-dashboard Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+123B(+0.03%) 🔺+31B(+0.02%)

Details of bundle changes

Performance

Total 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.

@netlify

netlify Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit d411d2d
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a41f3323ebe000008af2192
😎 Deploy Preview https://deploy-preview-5099--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks marked this pull request as ready for review June 22, 2026 11:09
@atomiks
atomiks requested a review from Copilot June 29, 2026 04:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 cornerSize state 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.

@atomiks
atomiks merged commit 4e1648b into mui:master Jun 29, 2026
6 checks passed
@atomiks
atomiks deleted the claude/jolly-bohr-525c5c branch June 29, 2026 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: scroll area Changes related to the scroll area component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants