MCPcopy Index your code
hub / github.com/adobe/react-spectrum / useSlider

Function useSlider

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

Source from the content-addressed store, hash-verified

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

Callers 11

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

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