MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useSlider

Function useSlider

packages/react-aria/src/slider/useSlider.ts:54–254  ·  view source on GitHub ↗
(
  props: AriaSliderProps<T>,
  state: SliderState,
  trackRef: RefObject<Element | null>
)

Source from the content-addressed store, hash-verified

52 */
53// oxlint-disable-next-line react/react-compiler
54export function useSlider<T extends number | number[]>(
55 props: AriaSliderProps<T>,
56 state: SliderState,
57 trackRef: RefObject<Element | null>
58): SliderAria {
59 let {labelProps, fieldProps} = useLabel(props);
60
61 let isVertical = props.orientation === 'vertical';
62
63 // Attach id of the label to the state so it can be accessed by useSliderThumb.
64 sliderData.set(state, {
65 id: (labelProps.id ?? fieldProps.id)!,
66 'aria-describedby': props['aria-describedby'],
67 'aria-details': props['aria-details']
68 });
69
70 let {direction} = useLocale();
71
72 let {addGlobalListener, removeGlobalListener} = useGlobalListeners();
73
74 // When the user clicks or drags the track, we want the motion to set and drag the
75 // closest thumb. Hence we also need to install useMove() on the track element.
76 // Here, we keep track of which index is the "closest" to the drag start point.
77 // It is set onMouseDown/onTouchDown; see trackProps below.
78 const realTimeTrackDraggingIndex = useRef<number | null>(null);
79
80 const reverseX = direction === 'rtl';
81 const currentPosition = useRef<number | null>(null);
82 const {moveProps} = useMove({
83 onMoveStart() {
84 currentPosition.current = null;
85 },
86 onMove({deltaX, deltaY}) {
87 if (!trackRef.current) {
88 return;
89 }
90 let {height, width} = trackRef.current.getBoundingClientRect();
91 let size = isVertical ? height : width;
92
93 if (currentPosition.current == null && realTimeTrackDraggingIndex.current != null) {
94 currentPosition.current = state.getThumbPercent(realTimeTrackDraggingIndex.current) * size;
95 }
96
97 let delta = isVertical ? deltaY : deltaX;
98 if (isVertical || reverseX) {
99 delta = -delta;
100 }
101
102 currentPosition.current! += delta;
103
104 if (realTimeTrackDraggingIndex.current != null && trackRef.current) {
105 const percent = clamp(currentPosition.current! / size, 0, 1);
106 state.setThumbPercent(realTimeTrackDraggingIndex.current, percent);
107 }
108 },
109 onMoveEnd() {
110 if (realTimeTrackDraggingIndex.current != null) {
111 state.setThumbDragging(realTimeTrackDraggingIndex.current, false);

Callers 12

renderUseSliderFunction · 0.90
ExampleFunction · 0.90
RangeExampleFunction · 0.90
ExampleFunction · 0.90
useColorSliderFunction · 0.90
StoryRangeSliderFunction · 0.90
StorySliderFunction · 0.90
StoryMultiSliderFunction · 0.90
Slider.tsxFile · 0.90
SliderBase.tsxFile · 0.90
SliderFunction · 0.90

Calls 9

useLabelFunction · 0.90
useLocaleFunction · 0.90
useGlobalListenersFunction · 0.90
useMoveFunction · 0.90
getSliderThumbIdFunction · 0.90
setInteractionModalityFunction · 0.90
mergePropsFunction · 0.90
focusMethod · 0.80
setMethod · 0.45

Tested by 4

renderUseSliderFunction · 0.72
ExampleFunction · 0.72
RangeExampleFunction · 0.72
ExampleFunction · 0.72