/
use-move.ts
126 lines (105 loc) · 3.53 KB
/
use-move.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
/* eslint-disable @typescript-eslint/no-use-before-define */
import { useEffect, useState, useRef } from 'react';
import { clamp } from '../utils';
export interface UseMovePosition {
x: number;
y: number;
}
export const clampUseMovePosition = (position: UseMovePosition) => ({
x: clamp(position.x, 0, 1),
y: clamp(position.y, 0, 1),
});
interface useMoveHandlers {
onScrubStart?(): void;
onScrubEnd?(): void;
}
export function useMove<T extends HTMLElement = HTMLDivElement>(
onChange: (value: UseMovePosition) => void,
handlers?: useMoveHandlers,
dir: 'ltr' | 'rtl' = 'ltr'
) {
const ref = useRef<T>();
const mounted = useRef<boolean>(false);
const isSliding = useRef(false);
const frame = useRef(0);
const [active, setActive] = useState(false);
useEffect(() => {
mounted.current = true;
}, []);
useEffect(() => {
const onScrub = ({ x, y }: UseMovePosition) => {
cancelAnimationFrame(frame.current);
frame.current = requestAnimationFrame(() => {
if (mounted.current && ref.current) {
ref.current.style.userSelect = 'none';
const rect = ref.current.getBoundingClientRect();
if (rect.width && rect.height) {
const _x = clamp((x - rect.left) / rect.width, 0, 1);
onChange({
x: dir === 'ltr' ? _x : 1 - _x,
y: clamp((y - rect.top) / rect.height, 0, 1),
});
}
}
});
};
const bindEvents = () => {
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', stopScrubbing);
document.addEventListener('touchmove', onTouchMove);
document.addEventListener('touchend', stopScrubbing);
};
const unbindEvents = () => {
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', stopScrubbing);
document.removeEventListener('touchmove', onTouchMove);
document.removeEventListener('touchend', stopScrubbing);
};
const startScrubbing = () => {
if (!isSliding.current && mounted.current) {
isSliding.current = true;
typeof handlers?.onScrubStart === 'function' && handlers.onScrubStart();
setActive(true);
bindEvents();
}
};
const stopScrubbing = () => {
if (isSliding.current && mounted.current) {
isSliding.current = false;
setActive(false);
unbindEvents();
setTimeout(() => {
typeof handlers?.onScrubEnd === 'function' && handlers.onScrubEnd();
}, 0);
}
};
const onMouseDown = (event: MouseEvent) => {
startScrubbing();
event.preventDefault();
onMouseMove(event);
};
const onMouseMove = (event: MouseEvent) => onScrub({ x: event.clientX, y: event.clientY });
const onTouchStart = (event: TouchEvent) => {
if (event.cancelable) {
event.preventDefault();
}
startScrubbing();
onTouchMove(event);
};
const onTouchMove = (event: TouchEvent) => {
if (event.cancelable) {
event.preventDefault();
}
onScrub({ x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY });
};
ref.current.addEventListener('mousedown', onMouseDown);
ref.current.addEventListener('touchstart', onTouchStart, { passive: false });
return () => {
if (ref.current) {
ref.current.removeEventListener('mousedown', onMouseDown);
ref.current.removeEventListener('touchstart', onTouchStart);
}
};
}, [dir, onChange]);
return { ref, active };
}