Skip to content

[Slider] Position thumb based on value instead of pointer location when dragging #1750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 23, 2025
4 changes: 2 additions & 2 deletions packages/react/src/slider/root/useSliderRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export function useSliderRoot(parameters: useSliderRoot.Parameters): useSliderRo
return {
value: newValue,
valueRescaled,
percentageValues: [valueRescaled * 100],
percentageValues: [valueToPercent(newValue, min, max)],
thumbIndex: 0,
};
}
Expand All @@ -388,7 +388,7 @@ export function useSliderRoot(parameters: useSliderRoot.Parameters): useSliderRo
);

const newPercentageValue = clamp(
valueRescaled * 100,
valueToPercent(newValue, min, max),
percentageValues[closestThumbIndex - 1] + minPercentageDifference || -Infinity,
percentageValues[closestThumbIndex + 1] - minPercentageDifference || Infinity,
);
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/slider/thumb/SliderThumb.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('<Slider.Thumb />', () => {
createTouches([{ identifier: 1, clientX: 199, clientY: 0 }]),
);

expect(thumbStyles.getPropertyValue('left')).to.equal('199px');
expect(thumbStyles.getPropertyValue('left')).to.equal('200px');
fireEvent.touchEnd(
document.body,
createTouches([{ identifier: 1, clientX: 0, clientY: 0 }]),
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('<Slider.Thumb />', () => {
createTouches([{ identifier: 1, clientX: 699, clientY: 0 }]),
);

expect(computedStyles.thumb2.getPropertyValue('left')).to.equal('699px');
expect(computedStyles.thumb2.getPropertyValue('left')).to.equal('700px');
fireEvent.touchEnd(
document.body,
createTouches([{ identifier: 1, clientX: 0, clientY: 0 }]),
Expand Down