-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathuseSliderState.tsx
261 lines (225 loc) · 8.07 KB
/
useSliderState.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import * as React from 'react';
import { useId, useControllableState, useMount } from '@fluentui/react-utilities';
import { SliderSlots, SliderState, SliderCommon } from './Slider.types';
/**
* Validates that the `value` is a number and falls between the min and max.
*
* @param value - the value to be clamped
* @param min - the lowest valid value
* @param max - the highest valid value
*/
const clamp = (value: number, min: number, max: number): number => Math.max(min, Math.min(max, value || 0));
/**
* Gets the current percent of specified value between a min and max
*
* @param value - the value to find the percent
* @param min - the lowest valid value
* @param max - the highest valid value
*/
const getPercent = (value: number, min: number, max: number) => {
return max === min ? 0 : ((value - min) / (max - min)) * 100;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const on = (element: Element, eventName: string, callback: (ev: any) => void) => {
element.addEventListener(eventName, callback);
return () => element.removeEventListener(eventName, callback);
};
export const useSliderState = (state: Pick<SliderState, keyof SliderCommon | keyof SliderSlots | 'as' | 'ref'>) => {
const {
as = 'div',
value,
defaultValue = 0,
min = 0,
max = 10,
step = 1,
disabled = false,
ariaValueText,
onChange,
vertical = false,
origin,
onPointerDown: onPointerDownCallback,
onKeyDown: onKeyDownCallback,
} = state;
const [currentValue, setCurrentValue] = useControllableState({
state: value && clamp(value, min, max),
defaultState: clamp(defaultValue, min, max),
initialState: 0,
});
const railRef = React.useRef<HTMLDivElement>(null);
const thumbRef = React.useRef<HTMLDivElement>(null);
const disposables = React.useRef<(() => void)[]>([]);
const onChangeCallback = React.useRef(onChange);
const id = useId('slider-', state.id);
/**
* Updates the `currentValue` to the new `incomingValue` and clamps it.
*
* @param ev
* @param incomingValue
*/
const updateValue = React.useCallback(
(incomingValue: number, ev): void => {
const clampedValue = clamp(incomingValue, min, max);
if (clampedValue !== min && clampedValue !== max) {
ev.stopPropagation();
if (ev.type === 'keydown') {
ev.preventDefault();
}
}
if (onChange && onChangeCallback.current) {
onChangeCallback.current(clampedValue, ev);
}
setCurrentValue(clampedValue);
},
[max, min, onChange, setCurrentValue],
);
/**
* Calculates the `step` position based off of a `Mouse` or `Touch` event.
*/
const calculateSteps = React.useCallback(
(ev: React.PointerEvent<HTMLDivElement>): number => {
const currentBounds = railRef?.current?.getBoundingClientRect();
const size = vertical ? currentBounds?.height || 0 : currentBounds?.width || 0;
const position = vertical ? currentBounds?.bottom || 0 : currentBounds?.left || 0;
const totalSteps = (max - min) / step;
const stepLength = size / totalSteps;
const thumbPosition = vertical ? ev.clientY : ev.clientX;
const distance = vertical ? position - thumbPosition : thumbPosition - position;
return distance / stepLength;
},
[max, min, step, vertical],
);
const onPointerMove = React.useCallback(
(ev: React.PointerEvent<HTMLDivElement>): void => {
if (step !== 1) {
updateValue(Math.round((min + step * calculateSteps(ev)) / step) * step, ev);
} else {
updateValue(min + step * calculateSteps(ev), ev);
}
},
[calculateSteps, min, step, updateValue],
);
const onPointerUp = (): void => {
disposables.current.forEach(dispose => dispose());
disposables.current = [];
thumbRef.current!.focus();
};
const onPointerDown = React.useCallback(
(ev: React.PointerEvent<HTMLDivElement>): void => {
const { pointerId } = ev;
const target = ev.target as HTMLElement;
if (target.setPointerCapture) {
target.setPointerCapture(pointerId);
}
onPointerDownCallback?.(ev);
disposables.current.push(on(target, 'pointermove', onPointerMove), on(target, 'pointerup', onPointerUp), () => {
target.releasePointerCapture(pointerId);
});
onPointerMove(ev);
},
[onPointerMove, onPointerDownCallback],
);
const onKeyDown = React.useCallback(
(ev: React.KeyboardEvent<HTMLDivElement>): void => {
onKeyDownCallback?.(ev);
if (ev.shiftKey) {
if (ev.key === 'ArrowDown' || ev.key === 'ArrowLeft') {
updateValue(currentValue! - step * 10, ev);
return;
} else if (ev.key === 'ArrowUp' || ev.key === 'ArrowRight') {
updateValue(currentValue! + step * 10, ev);
return;
}
} else if (ev.key === 'ArrowDown' || ev.key === 'ArrowLeft') {
updateValue(currentValue! - step, ev);
return;
} else if (ev.key === 'ArrowUp' || ev.key === 'ArrowRight') {
updateValue(currentValue! + step, ev);
return;
} else {
switch (ev.key) {
case 'PageDown':
updateValue(currentValue! - step * 10, ev);
break;
case 'PageUp':
updateValue(currentValue! + step * 10, ev);
break;
case 'Home':
updateValue(min, ev);
break;
case 'End':
updateValue(max, ev);
break;
}
}
},
[currentValue, max, min, onKeyDownCallback, step, updateValue],
);
React.useEffect(() => {
if (state.ref && state.ref.current) {
state.ref.current.value = currentValue;
state.ref.current.focus = () => thumbRef?.current?.focus();
}
}, [currentValue, state.ref]);
useMount(() => {
// If the user passes an out of bounds controlled value, clamp and update their value onMount.
if (value !== undefined && (value < min || value > max) && onChange && onChangeCallback.current) {
onChangeCallback.current(clamp(value, min, max));
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.warn('It appears that a controlled Slider has received an unclamped value outside of the min/max.');
}
}
});
const valuePercent = getPercent(currentValue!, min, max);
const originPercent = origin ? getPercent(origin, min, max) : 0;
const thumbStyles = {
transform: vertical ? `translateY(${valuePercent}%)` : `translateX(${valuePercent}%)`,
...state.thumb.style,
};
const trackStyles = vertical
? {
top: origin ? `${Math.min(valuePercent, originPercent)}%` : 0,
height: origin
? `${Math.max(originPercent - valuePercent, valuePercent - originPercent)}%`
: `${valuePercent}%`,
...state.track.style,
}
: {
left: origin ? `${Math.min(valuePercent, originPercent)}%` : 0,
width: origin ? `${Math.max(originPercent - valuePercent, valuePercent - originPercent)}%` : `${valuePercent}%`,
...state.track.style,
};
// Root props
state.as = as;
state.id = id;
if (!disabled) {
state.onPointerDown = onPointerDown;
state.onKeyDown = onKeyDown;
}
// Rail Props
state.rail.children = null;
// Track Props
state.trackWrapper.children = null;
// Track Props
state.track.className = 'ms-Slider-track';
state.track.style = trackStyles;
state.track.children = null;
// Thumb Wrapper Props
state.thumbWrapper.style = thumbStyles;
state.thumbWrapper.children = null;
// Thumb Props
state.thumb.className = 'ms-Slider-thumb';
state.thumb.ref = thumbRef;
state.thumb.tabIndex = disabled ? undefined : 0;
state.thumb.role = 'slider';
state.thumb['aria-valuemin'] = min;
state.thumb['aria-valuemax'] = max;
state.thumb['aria-valuenow'] = currentValue;
state.thumb['aria-valuetext'] = ariaValueText ? ariaValueText(currentValue!) : currentValue!.toString();
disabled && (state.thumb['aria-disabled'] = true);
state.thumb.children = null;
// Active Rail Props
state.activeRail.ref = railRef;
state.activeRail.children = null;
return state;
};