Repository navigation
[slider] Fix assorted issues - #5097
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,431.20 ms +80.23 ms(+5.9%) | Renders: 78 (+0)
9 tests within noise — details Metric alarms
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. |
flaviendelangle
left a comment
There was a problem hiding this comment.
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
e3a969b to
f8efd63
Compare
- 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
f8efd63 to
a6118f2
Compare
There was a problem hiding this comment.
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/onBlurto the underlying<input>(matchingonKeyDownsemantics) and prevent prematureonBlurvalidation commits when moving focus between range thumbs. - Make formatted output (
Slider.Valueand thumbaria-valuetext) recompute whenformatchanges; 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.
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. SodefaultValue={[19, 41]}withmin={20} max={40}rendered thumbs outside the track and produced out-of-rangearia-valuenow. Each range item is now clamped before sorting.Thumb
onFocus/onBlurfire on the wrapper divThe internal focus/blur/keydown handlers live on the nested
<input>, andonKeyDownis forwarded there, butonFocus/onBlurwere attached to the wrapper<div>— soevent.currentTargetwas the div, inconsistent withonKeyDown(both are typedFocusEventHandler<HTMLInputElement>). They're now forwarded to the input. The synthetic blur/focus dispatched while restoring:focus-visibleis still suppressed so user handlers don't observe those internal events.Slider.Valuekeeps stale formattingformattedValueswas memoized on a stable ref whose.currentonly updates in a layout effect, so changingformat(e.g. plain → currency) without changing the value/locale never recomputed.formatis now threaded through context and the memo keys on it. This also makesThumb'saria-valuetextreact toformatchanges, which had the same latent staleness.Range validation commits while focus moves between thumbs
The input
onBlurunconditionally ran field blur logic (validation.commit,setFocused(false)) undervalidationMode="onBlur", so tabbing from one range thumb to its sibling committed validation mid-interaction. Guarded with a shadow-safecontains(controlRef.current, event.relatedTarget), mirroringRadioGroup.Range pointer handling can emit a malformed array
getFingerStateguarded the upper bound only for non-range sliders; the range branch checkedthumbIndex < 0only. 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.lengthto the range guard.pnpm test:jsdom Sliderandpnpm test:chromium Sliderpass;typescript,eslint, andprettierare clean.