Skip to content

[slider] Fix touch track tap not firing onValueCommitted - #5779

Merged
atomiks merged 11 commits into
mui:masterfrom
atomiks:claude/github-issue-5772-9173d5
Sep 24, 2026
Merged

atomiks merged 11 commits into
mui:masterfrom
atomiks:claude/github-issue-5772-9173d5

Conversation

@atomiks

@atomiks atomiks commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5772

This PR makes a touch tap on the slider track fire onValueCommitted, just like a mouse click. Controlled and uncontrolled sliders were both affected. This regressed in v1.6.0 (#4937).

A touch fires pointerdown followed by the compatibility touchstart, and both ran the press-start logic. The pointer handler set the value and remembered it as the value to commit. The touch handler then called startPressing(), which cleared it. Re-applying the same value is a no-op, so nothing was remembered and release skipped the commit. A drag only worked because the next move remembered a new value.

Changes

  • Once a touch or pen pointerdown has started a gesture, touchstart no longer restarts it or sets the value again. Pen is included because Apple Pencil also fires a compatibility touchstart. The touch handler still attaches its touch listeners: without touch-action: none, Chrome cancels the pointer partway through a drag, and those listeners keep the drag going until touchend commits it.
  • The flag is reset at the start of every pointerdown, and cleared by the touchstart that reads it and when listeners stop. A cancelled gesture, including a pen gesture with no touchstart, can't leave it set for the next press.
  • A touch or pen tap on the thumb no longer changes the value, which now matches mouse. Before, the touch handler set a value from the finger position even when the thumb was pressed, so tapping a thumb could snap it to the nearest step (for example 100 → 99 with step={3}).
  • Added regression tests. A Chromium test taps the track with real touch input through CDP, so the browser decides the event order. Synthetic tests fire the same order (pointerdown, touchstart, pointerup, touchend) for touch and pen. Others cover the drag continuing after pointercancel, a touch press after a cancelled mouse, pen, or touch gesture, and a touch or pen tap on the thumb.

@atomiks atomiks added component: slider Changes related to the slider component. type: bug It doesn't behave as expected. type: regression A bug, but worse, it used to behave as expected. and removed type: bug It doesn't behave as expected. labels Sep 23, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 23, 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@5779
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5779
    

commit: c28f4a1

@code-infra-dashboard

code-infra-dashboard Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+107B(+0.02%) 🔺+25B(+0.02%)

Details of bundle changes

Performance

Total duration: 1,224.33 ms +84.45 ms(+7.4%) | Renders: 76 (+0) | Paint: 2,008.97 ms +107.45 ms(+5.7%)

No significant changes — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit c28f4a1
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ab4e60a085aed000811fd27
😎 Deploy Preview https://deploy-preview-5779--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 September 23, 2026 09:05
@michaldudak

Copy link
Copy Markdown
Member

PR review

This fixes #5772. I confirmed it by running an independent set of regression tests, written against the bug before this PR existed, on this branch's source: a touch tap commits once on both controlled and uncontrolled sliders, and the tap test added here fails on the base commit. Nothing is merge-blocking. The findings are one narrow stale-state path, one test that passes without the fix, and an undisclosed behavior change that looks like an improvement but has no test.

The claim that this regressed in v1.6.0 via #4937 checks out: that commit introduced both the pointer-path track press and the reset that clears the recorded value, and it first appears in the v1.6.0 tag.

Bugs (1)

1. 🟡 The gesture flag is never cleared when a gesture is cancelled

Location: packages/react/src/slider/control/SliderControl.tsx:432

pressedValuesRef.current = null;
currentInteractionValueRef.current = null;
pointerGestureRef.current = false;

stopListening is the only place that clears the flag, and a cancelled gesture never reaches it, because nothing listens for pointercancel or touchcancel. That is the known leak behind #5637. After a gesture that ends in touchcancel with no touchend, the flag stays true, and the next press whose pointer handler bails out early gets no press setup at all, where before this PR the touch handler would have set it up.

Failure scenario: a drag interrupted by a system gesture, then a press on which the pointer handler returns early, does nothing at all: the thumb does not move. I demonstrated it by forcing a pointerdown whose target is a text node, which the base commit handles and this branch ignores. I could not build a fully realistic browser sequence for it, since real pointer events always target elements, so the remaining routes are a consumer that calls preventDefault on pointerdown for some presses only, or a WebKit touch-target quirk. The slider recovers on the next ordinary press.

Fix: clear the flag wherever a cancel is handled, which is worth doing together with #5637. Setting it only for event.pointerType === 'touch' would also narrow the window, since a leaked mouse gesture could otherwise poison a later touch.

Tests (3)

1. 🟡 The pointercancel test passes without the source change

Location: packages/react/src/slider/root/SliderRoot.test.tsx:878

I ran both new tests against the base commit. The tap test fails, correctly. This one passes, because on base the touchmove to 70 repopulates the interaction value and touchend commits it. It is still a worthwhile guard, since it would fail if handleTouchStart returned early instead of attaching its listeners, but its name reads as a regression test for a bug that base does not have.

Failure scenario: a future refactor could delete the fix and keep this test green, leaving only the first test as real coverage.

Fix: reword it as a guard for the touch listeners, or assert a case base fails, such as a controlled slider tapped, then pointerCancel, then touchend with no movement, expecting one commit.

2. ℹ️ The new tests never exercise the pointer path they describe

Location: packages/react/src/slider/root/SliderRoot.test.tsx:850

The file's beforeAll replaces window.PointerEvent with MouseEvent, which the comment at line 79 says costs pointerId and pointerType on every event. So the pointerType: 'touch' and pointerId: 1 in both new tests are dropped, pointer capture is never taken or released, and the tests cannot tell a touch pointer from a mouse. They still pin the real mechanism, since the pointer handler runs either way.

Failure scenario: anyone moving these tests next to the code they cover, in the control's own test file, loses that stub. Pointer capture then runs, and setPointerCapture throws an unhandled NotFoundError for any id other than 1, which is the only id the Chromium runner keeps active. I hit exactly that with id 2.

Fix: leave them here, or stub the three capture methods if they move.

3. ℹ️ A touch tap on the thumb no longer applies a value, with nothing pinning it

Location: packages/react/src/slider/control/SliderControl.tsx:403

Skipping the touch press setup also skips the touch path's unconditional setValueFromPointer, which the pointer path has always guarded with pressedOnAnyThumb. The behaviors now match, which is the right outcome, but it is a second behavior change the description does not mention and no test covers.

Failure scenario: with min=0, max=100, step=3 and a value of 100, tapping the thumb without moving used to snap to 99 and commit it. It now leaves the value alone. I verified both directions against base and this branch.

Fix: add a short test pinning that a touch tap on the thumb leaves the value unchanged, and mention the change in the description.

Verdict

Approve after nits - the fix is correct and verified against independent tests, and every finding is a narrow edge case or test-quality point rather than a defect in the shipped path.


🤖 Review generated with Claude Code

@atomiks
atomiks force-pushed the claude/github-issue-5772-9173d5 branch from ed9f377 to fffd7b5 Compare September 23, 2026 11:33
@atomiks

atomiks commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

PR review

The fix itself is sound: gating touchstart on a flag that pointerdown sets and touchstart consumes correctly stops the compatibility touchstart from wiping currentInteractionValueRef, and the flag can't survive a cancelled gesture because both the reader and stopListening clear it. Nothing here is merge-blocking. Two things are worth a look before merge: the flag is gated on pointerType === 'touch', so stylus input — which also produces compatibility touch events — keeps the exact bug the PR fixes; and the new tests fire pointercancel/touchcancel against a component that has no handler for either, which surfaces a pre-existing stuck-gesture state.

Bugs (2)

1. 🟠 Stylus taps keep missing onValueCommitted because the flag only covers pointerType === 'touch'

Location: packages/react/src/slider/control/SliderControl.tsx:526

moveCountRef.current = 0;
// Only touch pointers have a following `touchstart` to consume the flag.
pointerGestureRef.current = event.pointerType === 'touch';

Stylus input is not the only source of compatibility touch events. On Android (and any platform where the digitizer is routed through the touch pipeline), Chrome dispatches pointerdown with pointerType: 'pen' and the compatibility touchstart. That touchstart sees startedByPointer === false, so it takes the full branch: startPressing() resets currentInteractionValueRef to null, then setValueFromPointer re-applies the value pointerdown already applied, setValue short-circuits on areValuesEqual(newValue, valueUnwrapped) and returns false, and nothing is recorded to commit — the precise mechanism described in the PR body.

The second half of the fix regresses the same way: the pen touchstart still runs setValueFromPointer with pressedThumbCenterOffsetRef applied, so a stylus tap on the thumb can still snap the value (100 → 99 with step={3}).

The comment on the line above states the assumption that makes this wrong ("Only touch pointers have a following touchstart"), and the codebase already disagrees with it: isTouchLikePointerType in packages/react/src/internals/usePressAndHold.ts exists specifically because pen behaves like touch here.

Failure scenario: On an Android tablet with a stylus, tap the slider track. onValueChange fires with the new value, but onValueCommitted never fires — the same regression #5772 reports for finger taps.

Fix: Reuse the existing helper:

import { isTouchLikePointerType } from '../../internals/usePressAndHold';
// ...
pointerGestureRef.current = isTouchLikePointerType(event.pointerType);

A pen pointer with no following touchstart is safe: stopListening() already clears the flag when the gesture ends. Update the comment to say "touch-like pointers" as well.

2. 🟠 Neither pointercancel nor touchcancel ends the gesture, leaving data-dragging stuck

Location: packages/react/src/slider/control/SliderControl.tsx:528

const doc = ownerDocument(control);
doc.addEventListener('pointermove', handleTouchMove, { passive: true });
doc.addEventListener('pointerup', handleTouchEnd, { once: true });

handleTouchEnd — the only thing that calls setDragging(false), setActive(-1), stopListening() and onValueCommitted — is only wired to pointerup and touchend. A cancelled pointer never produces a pointerup, and a cancelled touch never produces a touchend, so neither path is reachable after a cancel.

This is pre-existing rather than introduced here, but the PR now depends on the behaviour and encodes it in tests: SliderRoot.test.tsx:985-988 fires pointerCancel (and touchCancel for the touch case) and the slider simply carries on with dragging === true, the document listeners still attached and the in-progress value never committed. For the mouse/pen parameters of that test there is no touch fallback at all, so the gesture is stuck until some unrelated later interaction happens to fire pointerup.

Failure scenario: On a phone, start dragging the slider and let the OS interrupt the gesture (edge/back gesture, notification shade, long-press context menu). The browser fires touchcancel instead of touchend: data-dragging="true" stays on the Root/Control/Track/Thumb — so any [data-dragging] styling stays applied — and onValueCommitted never fires for the drag the user just performed.

Fix: Register pointercancel alongside pointerup and touchcancel alongside touchend, routing them to handleTouchEnd (or to a cancel variant that runs the same teardown but skips onValueCommitted, which is arguably the correct semantics for a cancelled gesture). If that is out of scope for a regression fix, it deserves a follow-up issue rather than being silently baked into the new tests.

Tests (1)

1. 🟡 No test covers a realistic touch drag, only a realistic touch tap

Location: packages/react/src/slider/root/SliderRoot.test.tsx:910

fireEvent.pointerDown(sliderControl, { pointerType: 'touch', pointerId: 1, buttons: 1, clientX: 50 });
fireEvent.touchStart(sliderControl, createTouches([{ identifier: 1, clientX: 50, clientY: 0 }]));
// Without `touch-action: none`, the browser cancels the pointer once it starts panning.
fireEvent.pointerCancel(sliderControl, { pointerType: 'touch', pointerId: 1 });
fireEvent.touchMove(document.body, createTouches([{ identifier: 1, clientX: 70, clientY: 0 }]));

The PR correctly identifies that the pre-existing touch tests never caught the bug because they fire touch events on their own, and adds one test with the real press order. But every move-based test in the file (should round value to step precision, the rtl/vertical drags, should reach right edge value, should focus the slider when touching, the disabled-thumb touch drag, …) is left firing touchstart/touchmove/touchend with no pointer events, i.e. it exercises the !startedByPointer fallback that no real browser takes after this change. The one drag test that does start from pointerdown immediately cancels the pointer, so it too runs only on the touch listeners.

The consequence is that the normal touch-drag path this PR creates — pointerdown → touchstart (skipped) → a pointermove and a touchmove for every finger move, both dispatching handleTouchMove → pointerup → touchend — has no coverage at all. That path is where the interesting invariants live: moveCountRef advances twice per physical move so the INTENTIONAL_DRAG_COUNT_THRESHOLD gate trips at roughly half the intended movement, and handleTouchEnd must commit exactly once even though both a pointerup and a touchend arrive.

Failure scenario: A future change to handleTouchMove/handleTouchEnd that double-counts moves or double-commits on a touch drag passes the whole suite, because no test ever delivers a pointermove and a touchmove for the same finger movement.

Fix: Add one test that plays the full browser sequence — pointerDown + touchStart, then paired pointerMove/touchMove for each step, then pointerUp + touchEnd — asserting the final value, a single onValueCommitted call, and that data-dragging is cleared at the end.

Verdict

Approve after nits - the core fix and its regression tests are correct; the open items are the pointerType === 'touch' gate leaving stylus taps broken and the untested realistic touch-drag path, neither of which blocks the fix for finger taps.


🤖 Review generated with Claude Code · Opus 5 (High) · medium review depth · 27 turns · 7m52s · $2.04 · run

@atomiks
atomiks merged commit 0b7b639 into mui:master Sep 24, 2026
24 checks passed
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: regression A bug, but worse, it used to behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Slider] Touch track tap changes value but does not fire onValueCommitted

2 participants