Skip to content

Commit 65eae8d

Browse files
committed
Fix overlapping slider thumbs stuck at max
1 parent cfd2827 commit 65eae8d

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

packages/react/src/slider/root/useSliderRoot.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,21 @@ function areValuesEqual(
3737
return false;
3838
}
3939

40-
function findClosest(values: readonly number[], currentValue: number) {
41-
const { index: closestIndex } =
42-
values.reduce<{ distance: number; index: number } | null>(
43-
(acc, value: number, index: number) => {
44-
const distance = Math.abs(currentValue - value);
45-
46-
if (acc === null || distance < acc.distance || distance === acc.distance) {
47-
return {
48-
distance,
49-
index,
50-
};
51-
}
40+
function getClosestThumbIndex(values: readonly number[], currentValue: number, min: number) {
41+
let closestIndex;
42+
let minDistance;
43+
for (let i = 0; i < values.length; i += 1) {
44+
const distance = Math.abs(currentValue - values[i]);
45+
if (
46+
minDistance === undefined ||
47+
distance < minDistance ||
48+
(values[i] === min && distance === minDistance)
49+
) {
50+
closestIndex = i;
51+
minDistance = distance;
52+
}
53+
}
5254

53-
return acc;
54-
},
55-
null,
56-
) ?? {};
5755
return closestIndex;
5856
}
5957

@@ -371,7 +369,7 @@ export function useSliderRoot(parameters: useSliderRoot.Parameters): useSliderRo
371369
}
372370

373371
if (shouldCaptureThumbIndex) {
374-
closestThumbIndexRef.current = findClosest(values, newValue) ?? 0;
372+
closestThumbIndexRef.current = getClosestThumbIndex(values, newValue, min) ?? 0;
375373
}
376374

377375
const closestThumbIndex = closestThumbIndexRef.current ?? 0;

packages/react/src/slider/thumb/useSliderThumb.ts

-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ export function useSliderThumb(parameters: useSliderThumb.Parameters): useSlider
129129
}[orientation]]: `${percent}%`,
130130
[isVertical ? 'left' : 'top']: '50%',
131131
transform: `translate(${(isVertical || !isRtl ? -1 : 1) * 50}%, ${(isVertical ? 1 : -1) * 50}%)`,
132-
// So the non active thumb doesn't show its label on hover.
133-
pointerEvents: activeIndex !== -1 && activeIndex !== index ? 'none' : undefined,
134132
zIndex: activeIndex === index ? 1 : undefined,
135133
} satisfies React.CSSProperties;
136134
}, [activeIndex, isRtl, orientation, percent, index]);

0 commit comments

Comments
 (0)