Skip to content

[slider] Fix assorted issues - #5097

Merged
atomiks merged 5 commits into
mui:masterfrom
atomiks:claude/happy-keller-a81638
Jun 30, 2026
Merged

atomiks merged 5 commits into
mui:masterfrom
atomiks:claude/happy-keller-a81638

Conversation

@atomiks

@atomiks atomiks commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Fixes five runtime bugs in Slider, each covered by a regression test (added failing first, then confirmed passing).

Range values aren't clamped

SliderRoot's scalar path clamped to [min, max], but the range path only sorted. So defaultValue={[19, 41]} with min={20} max={40} rendered thumbs outside the track and produced out-of-range aria-valuenow. Each range item is now clamped before sorting.

Thumb onFocus/onBlur fire on the wrapper div

The internal focus/blur/keydown handlers live on the nested <input>, and onKeyDown is forwarded there, but onFocus/onBlur were attached to the wrapper <div> — so event.currentTarget was the div, inconsistent with onKeyDown (both are typed FocusEventHandler<HTMLInputElement>). They're now forwarded to the input. The synthetic blur/focus dispatched while restoring :focus-visible is still suppressed so user handlers don't observe those internal events.

Slider.Value keeps stale formatting

formattedValues was memoized on a stable ref whose .current only updates in a layout effect, so changing format (e.g. plain → currency) without changing the value/locale never recomputed. format is now threaded through context and the memo keys on it. This also makes Thumb's aria-valuetext react to format changes, which had the same latent staleness.

Range validation commits while focus moves between thumbs

The input onBlur unconditionally ran field blur logic (validation.commit, setFocused(false)) under validationMode="onBlur", so tabbing from one range thumb to its sibling committed validation mid-interaction. Guarded with a shadow-safe contains(controlRef.current, event.relatedTarget), mirroring RadioGroup.

Range pointer handling can emit a malformed array

getFingerState guarded the upper bound only for non-range sliders; the range branch checked thumbIndex < 0 only. If the pressed thumb index pointed past the end of a value array that shrank mid-drag, the collision/push helpers wrote past the array end and grew it. Added || thumbIndex >= values.length to the range guard.

pnpm test:jsdom Slider and pnpm test:chromium Slider pass; typescript, eslint, and prettier are clean.

@atomiks atomiks added component: slider Changes related to the slider component. type: bug It doesn't behave as expected. 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@5097
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5097
    

commit: b6e0e28

@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 🔺+124B(+0.03%) 🔺+49B(+0.03%)

Details of bundle changes

Performance

Total duration: 1,431.20 ms +80.23 ms(+5.9%) | Renders: 78 (+0)

Test Duration Renders
Menu mount (300 instances) 150.00 ms 🔺+32.93 ms(+28.1%) 2 (+0)
Scroll Area mount (300 instances) 112.85 ms 🔺+25.87 ms(+29.7%) 3 (+0)
Checkbox mount (500 instances) 95.76 ms 🔺+25.49 ms(+36.3%) 1 (+0)
Menu open (500 items) 76.78 ms 🔺+13.52 ms(+21.4%) 12 (+0)
Slider mount (300 instances) 161.34 ms ▼-40.65 ms(-20.1%) 3 (+0)

9 tests within noise — details

Metric alarms

Test Metric Change
Menu mount (300 instances) bench:paint 🔺 +43.26 ms
Scroll Area mount (300 instances) bench:paint 🔺 +26.42 ms
Checkbox mount (500 instances) bench:paint 🔺 +24.74 ms
Menu open (500 items) bench:paint 🔺 +24.75 ms
Menu open (500 items) bench:paint#menu-open 🔺 +24.75 ms

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 b6e0e28
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a438b05ac13cf0008b42e45
😎 Deploy Preview https://deploy-preview-5097--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 changed the title [slider] Fix range clamping, focus forwarding, and stale formatting [slider] Fix assorted issues Jun 22, 2026
@atomiks
atomiks marked this pull request as ready for review June 23, 2026 04:53

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PR review

This PR fixes five distinct Slider runtime bugs, each with a regression test. The changes are correct and well-targeted: range clamping, the getFingerState upper-bound guard, the field-blur contains guard, and the format-through-context fix all hold up under scrutiny, and the focus/blur forwarding interacts correctly with mergeProps (the user handlers run before the internal onFocus resets restoringFocusVisibleRef, so the synthetic restore-focus suppression still works). Nothing is merge-blocking. One informational note on a re-render tradeoff.

Bugs (0)

No findings.

Tests (0)

No findings.

Simplifications (0)

No findings.

Docs (0)

No findings.

Notes

ℹ️ Threading format through context defeats the context memo for inline format objects

Location: packages/react/src/slider/root/SliderRoot.tsx:318,361

format,            // now a memo dependency of contextValue (and `state` consumers downstream)

Previously formatOptionsRef was a stable ref, so the slider contextValue memo never invalidated on format identity changes. Now format is a direct dependency. This is necessary for the staleness fix — but it means the idiomatic usage <Slider.Root format={{ style: 'currency', currency: 'USD' }}> (a fresh object each parent render) now recreates contextValue on every render, re-rendering Thumb/Value/Control. SliderValue's formattedValues memo ([format, locale, values]) likewise recomputes each render.

Failure scenario: A slider whose parent re-renders frequently (e.g. on each drag tick elsewhere) and passes an inline format object now does extra reconciliation work per render that the stable-ref version avoided. Note this also diverges from NumberField/Progress, which still use the formatOptionsRef pattern.

Fix: Acceptable as-is given the correctness win; no action required. If the extra renders matter, the doc/JSDoc could advise memoizing format, or format could be excluded from the memo and read via a ref that's also tracked by a content-based signal — but that reintroduces complexity the PR deliberately removed.

Verdict

Approve — five real bugs fixed with matching regression tests, no correctness or contract issues found; the only note is a minor, justified re-render tradeoff.


🤖 Review generated with Claude Code

@atomiks
atomiks requested a review from aarongarciah as a code owner June 30, 2026 08:38
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jun 30, 2026
@atomiks
atomiks force-pushed the claude/happy-keller-a81638 branch from e3a969b to f8efd63 Compare June 30, 2026 08:44
atomiks added 4 commits June 30, 2026 18:46
- Clamp each range value to [min, max] before sorting so thumbs and
  aria-valuenow stay within bounds
- Forward Thumb onFocus/onBlur to the nested input so event.currentTarget
  matches onKeyDown, while still suppressing the synthetic focus-visible
  restoration events
- Thread `format` through context so Slider.Value (and Thumb
  aria-valuetext) recompute when the format option changes
- Skip field validation commit when focus moves between thumbs of the same
  range slider
- Guard range pointer handling against a pressed thumb index past the end
  of a shrunken value array
@atomiks
atomiks force-pushed the claude/happy-keller-a81638 branch from f8efd63 to a6118f2 Compare June 30, 2026 08:53
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jun 30, 2026

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 fixes several runtime edge cases in the Slider component (range clamping, event forwarding consistency, formatting reactivity, validation timing, and pointer/drag robustness) and adds targeted regression tests to prevent reintroductions.

Changes:

  • Clamp range values to [min, max] (before sorting) and ensure form-submitted values reflect clamped state.
  • Forward onFocus/onBlur to the underlying <input> (matching onKeyDown semantics) and prevent premature onBlur validation commits when moving focus between range thumbs.
  • Make formatted output (Slider.Value and thumb aria-valuetext) recompute when format changes; harden range drag handling against array length changes mid-interaction.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/react/src/slider/value/SliderValue.tsx Recompute formatted output when format changes by depending on format directly from context.
packages/react/src/slider/value/SliderValue.test.tsx Adds regression test ensuring formatted output updates when format prop changes.
packages/react/src/slider/thumb/SliderThumb.tsx Moves focus/blur forwarding to the input, updates aria formatting to use format, and guards blur validation when focus stays within the slider.
packages/react/src/slider/thumb/SliderThumb.test.tsx Adds regression tests for focus/blur currentTarget correctness and validation behavior when tabbing between range thumbs.
packages/react/src/slider/root/SliderRootContext.ts Updates context surface to provide format (instead of a ref) for reactive formatting.
packages/react/src/slider/root/SliderRoot.tsx Clamps range values; registers field control and dirtiness tracking against the clamped field value; threads format through context.
packages/react/src/slider/root/SliderRoot.test.tsx Adds regression tests for range clamping, aria text recomputation on format changes, and clamped form submission.
packages/react/src/slider/control/SliderControl.tsx Hardens pointer interaction logic against out-of-range pressed indices and stale/mismatched cached interaction arrays.
packages/react/src/slider/control/SliderControl.test.tsx Adds browser-only regression tests for mid-drag range shrink scenarios to prevent malformed/stale commits.
docs/src/app/(docs)/react/components/slider/types.md Updates format prop description to reflect value formatting behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@atomiks
atomiks merged commit 79a0755 into mui:master Jun 30, 2026
6 checks passed
@atomiks
atomiks deleted the claude/happy-keller-a81638 branch June 30, 2026 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants