/
useSlider.ts
150 lines (121 loc) Β· 3.82 KB
/
useSlider.ts
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
import { useEffect, useRef, RefObject, CSSProperties } from 'react';
import { isClient, off, on } from './util';
import useMountedState from './useMountedState';
import useSetState from './useSetState';
import useMeasureDirty from './useMeasureDirty';
export interface State {
isSliding: boolean;
value: number;
pos: number;
length: number;
}
export interface Options {
onScrub: (value: number) => void;
onScrubStart: () => void;
onScrubStop: () => void;
reverse: boolean;
styles: boolean | CSSProperties;
}
const noop = () => {};
const useSlider = (ref: RefObject<HTMLElement>, options: Partial<Options> = {}): State => {
const isMounted = useMountedState();
const isSliding = useRef(false);
const frame = useRef(0);
const [state, setState] = useSetState<State>({
isSliding: false,
value: 0,
pos: 0,
length: 0,
});
const { width } = useMeasureDirty(ref);
useEffect(() => {
if (isClient) {
const styles = options.styles === undefined ? true : options.styles;
const reverse = options.reverse === undefined ? false : options.reverse;
if (ref.current && styles) {
ref.current.style.userSelect = 'none';
}
const startScrubbing = () => {
if (!isSliding.current && isMounted()) {
(options.onScrubStart || noop)();
isSliding.current = true;
setState({ isSliding: true });
bindEvents();
}
};
const stopScrubbing = () => {
if (isSliding.current && isMounted()) {
(options.onScrubStop || noop)();
isSliding.current = false;
setState({ isSliding: false });
unbindEvents();
}
};
const onMouseDown = (event: MouseEvent) => {
startScrubbing();
onMouseMove(event);
};
const onMouseMove = (event: MouseEvent) => onScrub(event.clientX);
const onTouchStart = (event: TouchEvent) => {
startScrubbing();
onTouchMove(event);
};
const onTouchMove = (event: TouchEvent) => onScrub(event.changedTouches[0].clientX);
const bindEvents = () => {
on(document, 'mousemove', onMouseMove);
on(document, 'mouseup', stopScrubbing);
on(document, 'touchmove', onTouchMove);
on(document, 'touchend', stopScrubbing);
};
const unbindEvents = () => {
off(document, 'mousemove', onMouseMove);
off(document, 'mouseup', stopScrubbing);
off(document, 'touchmove', onTouchMove);
off(document, 'touchend', stopScrubbing);
};
const onScrub = (clientX: number) => {
cancelAnimationFrame(frame.current);
frame.current = requestAnimationFrame(() => {
if (isMounted() && ref.current) {
const { left: pos, width: length } = ref.current.getBoundingClientRect();
// Prevent returning 0 when element is hidden by CSS
if (!length) {
return;
}
let value = (clientX - pos) / length;
if (value > 1) {
value = 1;
} else if (value < 0) {
value = 0;
}
if (reverse) {
value = 1 - value;
}
setState({
value,
pos: clientX - pos,
length,
});
(options.onScrub || noop)(value);
}
});
};
on(ref.current, 'mousedown', onMouseDown);
on(ref.current, 'touchstart', onTouchStart);
return () => {
off(ref.current, 'mousedown', onMouseDown);
off(ref.current, 'touchstart', onTouchStart);
};
} else {
return undefined;
}
}, [ref]);
useEffect(() => {
setState(prevState => ({
pos: Math.round(prevState.value * width),
length: width,
}));
}, [width]);
return state;
};
export default useSlider;