react-ui-animate 1.2.2 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Interpolation.d.ts +20 -0
- package/dist/Math.d.ts +34 -0
- package/dist/Modules.d.ts +32 -0
- package/dist/Types.d.ts +64 -0
- package/dist/animation/getInitialConfig.d.ts +3 -0
- package/dist/animation/index.d.ts +4 -0
- package/dist/animation/interpolation.d.ts +20 -0
- package/dist/animation/modules/AnimatedBlock.d.ts +5 -0
- package/dist/animation/modules/AnimatedImage.d.ts +5 -0
- package/dist/animation/modules/AnimatedInline.d.ts +5 -0
- package/dist/animation/modules/MountedBlock.d.ts +17 -0
- package/dist/animation/modules/ScrollableBlock.d.ts +16 -0
- package/dist/animation/modules/index.d.ts +6 -0
- package/dist/animation/modules/makeAnimatedComponent.d.ts +4 -0
- package/dist/animation/modules.d.ts +47 -0
- package/dist/animation/useAnimatedValue.d.ts +30 -0
- package/dist/animation/useMountedValue.d.ts +22 -0
- package/dist/controllers/EventAttacher.d.ts +8 -0
- package/dist/controllers/index.d.ts +1 -0
- package/dist/gestures/controllers/DragGesture.d.ts +17 -0
- package/dist/gestures/controllers/Gesture.d.ts +20 -0
- package/dist/gestures/controllers/MouseMoveGesture.d.ts +13 -0
- package/dist/gestures/controllers/ScrollGesture.d.ts +14 -0
- package/dist/gestures/controllers/WheelGesture.d.ts +15 -0
- package/dist/gestures/controllers/index.d.ts +4 -0
- package/dist/gestures/eventAttacher.d.ts +11 -0
- package/dist/gestures/hooks/index.d.ts +5 -0
- package/dist/gestures/hooks/useDrag.d.ts +4 -0
- package/dist/gestures/hooks/useGesture.d.ts +9 -0
- package/dist/gestures/hooks/useMouseMove.d.ts +4 -0
- package/dist/gestures/hooks/useRecognizer.d.ts +10 -0
- package/dist/gestures/hooks/useScroll.d.ts +4 -0
- package/dist/gestures/hooks/useWheel.d.ts +4 -0
- package/dist/gestures/index.d.ts +2 -0
- package/dist/gestures/math.d.ts +34 -0
- package/dist/gestures/types.d.ts +51 -0
- package/dist/gestures/withDefault.d.ts +4 -0
- package/dist/getInitialConfig.d.ts +3 -0
- package/dist/hooks/index.d.ts +3 -0
- package/dist/hooks/useDrag.d.ts +5 -0
- package/dist/hooks/useMeasure.d.ts +14 -0
- package/dist/hooks/useMouseMove.d.ts +5 -0
- package/dist/hooks/useOutsideClick.d.ts +2 -0
- package/dist/hooks/useScroll.d.ts +5 -0
- package/dist/hooks/useWheel.d.ts +5 -0
- package/dist/hooks/useWindowDimension.d.ts +9 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1189 -0
- package/dist/index.js.map +1 -0
- package/dist/useAnimatedValue.d.ts +30 -0
- package/dist/useMountedValue.d.ts +22 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/isDefined.d.ts +1 -0
- package/package.json +6 -3
- package/src/{getInitialConfig.ts → animation/getInitialConfig.ts} +0 -0
- package/src/animation/index.ts +4 -0
- package/src/{Interpolation.ts → animation/interpolation.ts} +0 -0
- package/src/{Modules.tsx → animation/modules.tsx} +26 -1
- package/src/{useAnimatedValue.ts → animation/useAnimatedValue.ts} +8 -9
- package/src/{useMountedValue.ts → animation/useMountedValue.ts} +8 -9
- package/src/gestures/controllers/DragGesture.ts +176 -0
- package/src/gestures/controllers/Gesture.ts +54 -0
- package/src/gestures/controllers/MouseMoveGesture.ts +111 -0
- package/src/gestures/controllers/ScrollGesture.ts +107 -0
- package/src/gestures/controllers/WheelGesture.ts +123 -0
- package/src/gestures/controllers/index.ts +4 -0
- package/src/gestures/eventAttacher.ts +67 -0
- package/src/gestures/hooks/index.ts +5 -0
- package/src/gestures/hooks/useDrag.ts +14 -0
- package/src/gestures/hooks/useGesture.ts +38 -0
- package/src/gestures/hooks/useMouseMove.ts +11 -0
- package/src/gestures/hooks/useRecognizer.ts +59 -0
- package/src/gestures/hooks/useScroll.ts +11 -0
- package/src/gestures/hooks/useWheel.ts +11 -0
- package/src/gestures/index.ts +2 -0
- package/src/{Math.ts → gestures/math.ts} +0 -0
- package/src/{Types.ts → gestures/types.ts} +19 -36
- package/src/gestures/withDefault.ts +3 -0
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useMeasure.ts +11 -1
- package/src/hooks/useOutsideClick.ts +3 -2
- package/src/hooks/useWindowDimension.ts +7 -1
- package/src/index.ts +2 -5
- package/src/utils/index.ts +1 -0
- package/src/{isDefined.ts → utils/isDefined.ts} +0 -0
- package/tsconfig.json +1 -0
- package/src/controllers/EventAttacher.ts +0 -35
- package/src/controllers/index.ts +0 -1
- package/src/hooks/useDrag.ts +0 -231
- package/src/hooks/useMouseMove.ts +0 -123
- package/src/hooks/useScroll.ts +0 -124
- package/src/hooks/useWheel.ts +0 -144
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import { attachEvents } from "../gestures/eventAttacher";
|
|
3
4
|
|
|
4
5
|
export function useOutsideClick(
|
|
5
6
|
elementRef: React.RefObject<HTMLElement>,
|
|
@@ -28,7 +29,7 @@ export function useOutsideClick(
|
|
|
28
29
|
}
|
|
29
30
|
};
|
|
30
31
|
|
|
31
|
-
const subscribe = attachEvents(document, [["click", handleOutsideClick]]);
|
|
32
|
+
const subscribe = attachEvents([document], [["click", handleOutsideClick]]);
|
|
32
33
|
|
|
33
34
|
return () => subscribe && subscribe();
|
|
34
35
|
}, []);
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
type WindowDimensionType = {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
innerWidth: number;
|
|
7
|
+
innerHeight: number;
|
|
8
|
+
};
|
|
3
9
|
|
|
4
10
|
export function useWindowDimension(
|
|
5
11
|
callback: (event: WindowDimensionType) => void,
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
export { Easing } from "@raidipesh78/re-motion";
|
|
2
|
-
export
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./Interpolation";
|
|
5
|
-
export * from "./Modules";
|
|
6
|
-
export * from "./Math";
|
|
2
|
+
export * from "./animation";
|
|
3
|
+
export * from "./gestures";
|
|
7
4
|
export * from "./hooks";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./isDefined";
|
|
File without changes
|
package/tsconfig.json
CHANGED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Attach single document / window event / HTMLElement
|
|
3
|
-
*/
|
|
4
|
-
export function attachEvent(
|
|
5
|
-
domTarget: Window | Document | HTMLElement,
|
|
6
|
-
event: string,
|
|
7
|
-
callback: (e: any) => void,
|
|
8
|
-
capture: any = false
|
|
9
|
-
) {
|
|
10
|
-
domTarget.addEventListener(event, callback, capture);
|
|
11
|
-
|
|
12
|
-
return function () {
|
|
13
|
-
domTarget.removeEventListener(event, callback, capture);
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Attach multiple document / window event / HTMLElement
|
|
19
|
-
*/
|
|
20
|
-
export function attachEvents(
|
|
21
|
-
domTarget: Window | Document | HTMLElement,
|
|
22
|
-
events: Array<[event: string, callback: (e: any) => void, capture?: any]>
|
|
23
|
-
) {
|
|
24
|
-
const subscribers: Array<() => void> = [];
|
|
25
|
-
|
|
26
|
-
events.forEach(function ([event, callback, capture = false]) {
|
|
27
|
-
subscribers.push(attachEvent(domTarget, event, callback, capture));
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
return function () {
|
|
31
|
-
subscribers.forEach(function (subscriber) {
|
|
32
|
-
subscriber();
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
}
|
package/src/controllers/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./EventAttacher";
|
package/src/hooks/useDrag.ts
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { DragEventType, Vector2, UseDragConfig } from "../Types";
|
|
3
|
-
import { clamp } from "../Math";
|
|
4
|
-
import { attachEvents } from "../controllers";
|
|
5
|
-
|
|
6
|
-
export function useDrag(
|
|
7
|
-
callback: (event: DragEventType) => void,
|
|
8
|
-
config?: UseDragConfig,
|
|
9
|
-
deps?: React.DependencyList
|
|
10
|
-
) {
|
|
11
|
-
const _VELOCITY_LIMIT = 20;
|
|
12
|
-
|
|
13
|
-
const currentIndex = React.useRef<number | undefined>(undefined);
|
|
14
|
-
const ref = React.useRef<any>(null);
|
|
15
|
-
const elementRefs = React.useRef<Array<any>>([]);
|
|
16
|
-
|
|
17
|
-
const callbackRef = React.useRef<(event: DragEventType) => void>(callback);
|
|
18
|
-
|
|
19
|
-
const cancelRef = React.useRef<() => void>();
|
|
20
|
-
|
|
21
|
-
const isGestureActive = React.useRef<boolean>(false);
|
|
22
|
-
|
|
23
|
-
// Holds only movement - always starts from 0
|
|
24
|
-
const movement = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
25
|
-
const movementStart = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
26
|
-
const previousMovement = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
27
|
-
const initialMovement = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
28
|
-
|
|
29
|
-
// Holds offsets
|
|
30
|
-
const translation = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
31
|
-
const offset = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
32
|
-
|
|
33
|
-
const lastTimeStamp = React.useRef<number>(0);
|
|
34
|
-
const velocity = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
35
|
-
|
|
36
|
-
const handleCallback = () => {
|
|
37
|
-
if (callbackRef) {
|
|
38
|
-
callbackRef.current({
|
|
39
|
-
args: [currentIndex.current],
|
|
40
|
-
down: isGestureActive.current,
|
|
41
|
-
movementX: movement.current.x,
|
|
42
|
-
movementY: movement.current.y,
|
|
43
|
-
offsetX: translation.current.x,
|
|
44
|
-
offsetY: translation.current.y,
|
|
45
|
-
velocityX: velocity.current.x,
|
|
46
|
-
velocityY: velocity.current.y,
|
|
47
|
-
distanceX: Math.abs(movement.current.x),
|
|
48
|
-
distanceY: Math.abs(movement.current.y),
|
|
49
|
-
directionX: Math.sign(movement.current.x),
|
|
50
|
-
directionY: Math.sign(movement.current.y),
|
|
51
|
-
cancel: function () {
|
|
52
|
-
cancelRef.current && cancelRef.current();
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// Reinitiate callback when dependency change
|
|
59
|
-
React.useEffect(() => {
|
|
60
|
-
callbackRef.current = callback;
|
|
61
|
-
|
|
62
|
-
return () => {
|
|
63
|
-
callbackRef.current = () => false;
|
|
64
|
-
};
|
|
65
|
-
}, deps);
|
|
66
|
-
|
|
67
|
-
React.useEffect(() => {
|
|
68
|
-
const _elemRef = ref.current;
|
|
69
|
-
const _refElementsMultiple = elementRefs.current;
|
|
70
|
-
|
|
71
|
-
var reSubscribe: any;
|
|
72
|
-
const _initEvents = () => {
|
|
73
|
-
if (_elemRef || _refElementsMultiple.length > 0) {
|
|
74
|
-
reSubscribe = attachEvents(window, [
|
|
75
|
-
["mousedown", pointerDown],
|
|
76
|
-
["mousemove", pointerMove],
|
|
77
|
-
["touchstart", pointerDown, { passive: false }],
|
|
78
|
-
["touchmove", pointerMove, { passive: false }],
|
|
79
|
-
]);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const pointerDown = (e: any) => {
|
|
84
|
-
if (e.type === "touchstart") {
|
|
85
|
-
movementStart.current = {
|
|
86
|
-
x: e.touches[0].clientX,
|
|
87
|
-
y: e.touches[0].clientY,
|
|
88
|
-
};
|
|
89
|
-
} else {
|
|
90
|
-
movementStart.current = { x: e.clientX, y: e.clientY };
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
movement.current = { x: 0, y: 0 };
|
|
94
|
-
offset.current = { x: translation.current.x, y: translation.current.y };
|
|
95
|
-
previousMovement.current = { x: 0, y: 0 };
|
|
96
|
-
velocity.current = { x: 0, y: 0 };
|
|
97
|
-
|
|
98
|
-
// find current selected element
|
|
99
|
-
const currElem = _refElementsMultiple.find(
|
|
100
|
-
(elem) => elem.current === e.target
|
|
101
|
-
);
|
|
102
|
-
|
|
103
|
-
if (e.target === _elemRef || currElem) {
|
|
104
|
-
isGestureActive.current = true;
|
|
105
|
-
e.preventDefault();
|
|
106
|
-
|
|
107
|
-
// set args
|
|
108
|
-
if (currElem) {
|
|
109
|
-
currentIndex.current = _refElementsMultiple.indexOf(currElem);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// if initial function is defined then call it to get initial movementX and movementY
|
|
113
|
-
// if only select to bounded draggable element
|
|
114
|
-
const initial = config?.initial && config.initial();
|
|
115
|
-
const initialMovementX = initial?.movementX;
|
|
116
|
-
const initialMovementY = initial?.movementY;
|
|
117
|
-
|
|
118
|
-
initialMovement.current = {
|
|
119
|
-
x: initialMovementX ?? 0,
|
|
120
|
-
y: initialMovementY ?? 0,
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
movement.current = {
|
|
124
|
-
x: initialMovement.current.x,
|
|
125
|
-
y: initialMovement.current.y,
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
previousMovement.current = {
|
|
129
|
-
x: initialMovement.current.x,
|
|
130
|
-
y: initialMovement.current.y,
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
handleCallback();
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
const pointerMove = (e: any) => {
|
|
138
|
-
if (isGestureActive.current) {
|
|
139
|
-
e.preventDefault();
|
|
140
|
-
const now = Date.now();
|
|
141
|
-
const deltaTime = Math.min(now - lastTimeStamp.current, 64);
|
|
142
|
-
lastTimeStamp.current = now;
|
|
143
|
-
|
|
144
|
-
const t = deltaTime / 1000;
|
|
145
|
-
|
|
146
|
-
if (e.type === "touchmove") {
|
|
147
|
-
movement.current = {
|
|
148
|
-
x:
|
|
149
|
-
initialMovement.current.x +
|
|
150
|
-
(e.touches[0].clientX - movementStart.current.x),
|
|
151
|
-
y:
|
|
152
|
-
initialMovement.current.y +
|
|
153
|
-
(e.touches[0].clientY - movementStart.current.y),
|
|
154
|
-
};
|
|
155
|
-
} else {
|
|
156
|
-
movement.current = {
|
|
157
|
-
x:
|
|
158
|
-
initialMovement.current.x + (e.clientX - movementStart.current.x),
|
|
159
|
-
y:
|
|
160
|
-
initialMovement.current.y + (e.clientY - movementStart.current.y),
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
translation.current = {
|
|
165
|
-
x: offset.current.x + movement.current.x,
|
|
166
|
-
y: offset.current.y + movement.current.y,
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
velocity.current = {
|
|
170
|
-
x: clamp(
|
|
171
|
-
(movement.current.x - previousMovement.current.x) / t / 1000,
|
|
172
|
-
-_VELOCITY_LIMIT,
|
|
173
|
-
_VELOCITY_LIMIT
|
|
174
|
-
),
|
|
175
|
-
y: clamp(
|
|
176
|
-
(movement.current.y - previousMovement.current.y) / t / 1000,
|
|
177
|
-
-_VELOCITY_LIMIT,
|
|
178
|
-
_VELOCITY_LIMIT
|
|
179
|
-
),
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
previousMovement.current = {
|
|
183
|
-
x: movement.current.x,
|
|
184
|
-
y: movement.current.y,
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
handleCallback();
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
const pointerUp = () => {
|
|
192
|
-
if (isGestureActive.current) {
|
|
193
|
-
isGestureActive.current = false;
|
|
194
|
-
handleCallback();
|
|
195
|
-
_initEvents();
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
var subscribe: any;
|
|
200
|
-
if (_elemRef || _refElementsMultiple.length > 0) {
|
|
201
|
-
subscribe = attachEvents(window, [
|
|
202
|
-
["mousedown", pointerDown],
|
|
203
|
-
["mousemove", pointerMove],
|
|
204
|
-
["mouseup", pointerUp],
|
|
205
|
-
["touchstart", pointerDown, { passive: false }],
|
|
206
|
-
["touchmove", pointerMove, { passive: false }],
|
|
207
|
-
["touchend", pointerUp],
|
|
208
|
-
]);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const _cancelEvents = () => {
|
|
212
|
-
_initEvents(); // When cancel is called for first time reSubscription is undefined, so initializing reSubscribe and calling it fixes the issue
|
|
213
|
-
reSubscribe && reSubscribe();
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
cancelRef.current = _cancelEvents;
|
|
217
|
-
|
|
218
|
-
return () => subscribe && subscribe();
|
|
219
|
-
}, []);
|
|
220
|
-
|
|
221
|
-
return (index?: number) => {
|
|
222
|
-
if (index === null || index === undefined) {
|
|
223
|
-
return { ref };
|
|
224
|
-
} else {
|
|
225
|
-
elementRefs.current[index] =
|
|
226
|
-
elementRefs.current[index] || React.createRef();
|
|
227
|
-
|
|
228
|
-
return { ref: elementRefs.current[index] };
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { MouseMoveEventType, Vector2 } from "../Types";
|
|
3
|
-
import { clamp } from "../Math";
|
|
4
|
-
import { attachEvents } from "../controllers";
|
|
5
|
-
|
|
6
|
-
export function useMouseMove(
|
|
7
|
-
callback: (event: MouseMoveEventType) => void,
|
|
8
|
-
deps?: React.DependencyList
|
|
9
|
-
) {
|
|
10
|
-
const _VELOCITY_LIMIT = 20;
|
|
11
|
-
|
|
12
|
-
const ref = React.useRef<any>(null);
|
|
13
|
-
|
|
14
|
-
const callbackRef =
|
|
15
|
-
React.useRef<(event: MouseMoveEventType) => void>(callback);
|
|
16
|
-
const isMoving = React.useRef<boolean>(false);
|
|
17
|
-
const _isMoving = React.useRef<any>(-1);
|
|
18
|
-
const mouseXY = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
19
|
-
const prevMouseXY = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
20
|
-
const directionXY = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
21
|
-
const velocityXY = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
22
|
-
const currentEvent = React.useRef<MouseEvent>();
|
|
23
|
-
const lastTimeStamp = React.useRef<number>(0);
|
|
24
|
-
|
|
25
|
-
const handleCallback = () => {
|
|
26
|
-
if (callbackRef) {
|
|
27
|
-
callbackRef.current({
|
|
28
|
-
target: currentEvent.current?.target,
|
|
29
|
-
isMoving: isMoving.current,
|
|
30
|
-
mouseX: mouseXY.current.x,
|
|
31
|
-
mouseY: mouseXY.current.y,
|
|
32
|
-
velocityX: velocityXY.current.x,
|
|
33
|
-
velocityY: velocityXY.current.y,
|
|
34
|
-
directionX: directionXY.current.x,
|
|
35
|
-
directionY: directionXY.current.y,
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// Reinitiate callback when dependency change
|
|
41
|
-
React.useEffect(() => {
|
|
42
|
-
callbackRef.current = callback;
|
|
43
|
-
|
|
44
|
-
return () => {
|
|
45
|
-
callbackRef.current = () => false;
|
|
46
|
-
};
|
|
47
|
-
}, deps);
|
|
48
|
-
|
|
49
|
-
React.useEffect(() => {
|
|
50
|
-
const _refElement = ref.current;
|
|
51
|
-
|
|
52
|
-
const mouseMove = ({ e, x, y }: { e: MouseEvent } & Vector2) => {
|
|
53
|
-
const now: number = Date.now();
|
|
54
|
-
const deltaTime = Math.min(now - lastTimeStamp.current, 64);
|
|
55
|
-
lastTimeStamp.current = now;
|
|
56
|
-
const t = deltaTime / 1000; // seconds
|
|
57
|
-
|
|
58
|
-
mouseXY.current = { x, y };
|
|
59
|
-
currentEvent.current = e;
|
|
60
|
-
|
|
61
|
-
if (_isMoving.current !== -1) {
|
|
62
|
-
isMoving.current = true;
|
|
63
|
-
clearTimeout(_isMoving.current);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
_isMoving.current = setTimeout(() => {
|
|
67
|
-
isMoving.current = false;
|
|
68
|
-
directionXY.current = { x: 0, y: 0 };
|
|
69
|
-
velocityXY.current = { x: 0, y: 0 };
|
|
70
|
-
|
|
71
|
-
handleCallback();
|
|
72
|
-
}, 250); // Debounce 250 milliseconds
|
|
73
|
-
|
|
74
|
-
const diffX = mouseXY.current.x - prevMouseXY.current.x;
|
|
75
|
-
const diffY = mouseXY.current.y - prevMouseXY.current.y;
|
|
76
|
-
|
|
77
|
-
directionXY.current = {
|
|
78
|
-
x: Math.sign(diffX),
|
|
79
|
-
y: Math.sign(diffY),
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
velocityXY.current = {
|
|
83
|
-
x: clamp(diffX / t / 1000, -_VELOCITY_LIMIT, _VELOCITY_LIMIT),
|
|
84
|
-
y: clamp(diffY / t / 1000, -_VELOCITY_LIMIT, _VELOCITY_LIMIT),
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
prevMouseXY.current = { x: mouseXY.current.x, y: mouseXY.current.y };
|
|
88
|
-
|
|
89
|
-
handleCallback();
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const mouseMoveElementListener = (e: MouseEvent) => {
|
|
93
|
-
/** Is offsetLeft / offsetTop needed here ???
|
|
94
|
-
* const elementOffsetLeft = _refElement?.offsetLeft || 0;
|
|
95
|
-
* const elementOffsetTop = _refElement?.offsetTop || 0;
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
mouseMove({
|
|
99
|
-
e,
|
|
100
|
-
x: e.clientX,
|
|
101
|
-
y: e.clientY,
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const mouseMoveListener = (e: MouseEvent) => {
|
|
106
|
-
mouseMove({ e, x: e.clientX, y: e.clientY });
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
var subscribe: any;
|
|
110
|
-
|
|
111
|
-
if (_refElement) {
|
|
112
|
-
subscribe = attachEvents(_refElement, [
|
|
113
|
-
["mousemove", mouseMoveElementListener],
|
|
114
|
-
]);
|
|
115
|
-
} else {
|
|
116
|
-
subscribe = attachEvents(window, [["mousemove", mouseMoveListener]]);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return () => subscribe && subscribe();
|
|
120
|
-
}, []);
|
|
121
|
-
|
|
122
|
-
return () => ({ ref }); // ...bind()
|
|
123
|
-
}
|
package/src/hooks/useScroll.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { ScrollEventType, Vector2 } from "../Types";
|
|
3
|
-
import { clamp } from "../Math";
|
|
4
|
-
import { attachEvents } from "../controllers";
|
|
5
|
-
|
|
6
|
-
export function useScroll(
|
|
7
|
-
callback: (event: ScrollEventType) => void,
|
|
8
|
-
deps?: React.DependencyList
|
|
9
|
-
) {
|
|
10
|
-
const ref = React.useRef<any>(null);
|
|
11
|
-
|
|
12
|
-
const scrollXY = React.useRef<Vector2>({
|
|
13
|
-
x: 0,
|
|
14
|
-
y: 0,
|
|
15
|
-
});
|
|
16
|
-
const previousScrollXY = React.useRef<Vector2>({
|
|
17
|
-
x: 0,
|
|
18
|
-
y: 0,
|
|
19
|
-
});
|
|
20
|
-
const isScrolling = React.useRef<boolean>(false);
|
|
21
|
-
const scrollDirection = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
22
|
-
const _isScrolling = React.useRef<any>(-1); // For checking scrolling and add throttle
|
|
23
|
-
|
|
24
|
-
const lastTimeStamp = React.useRef<number>(0);
|
|
25
|
-
const velocity = React.useRef<Vector2>({ x: 0, y: 0 });
|
|
26
|
-
|
|
27
|
-
const callbackRef = React.useRef<(event: ScrollEventType) => void>(callback);
|
|
28
|
-
|
|
29
|
-
const handleCallback: () => void = () => {
|
|
30
|
-
if (callbackRef) {
|
|
31
|
-
callbackRef.current({
|
|
32
|
-
isScrolling: isScrolling.current,
|
|
33
|
-
scrollX: scrollXY.current.x,
|
|
34
|
-
scrollY: scrollXY.current.y,
|
|
35
|
-
velocityX: velocity.current.x,
|
|
36
|
-
velocityY: velocity.current.y,
|
|
37
|
-
directionX: scrollDirection.current.x,
|
|
38
|
-
directionY: scrollDirection.current.y,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// Reinitiate callback when dependency change
|
|
44
|
-
React.useEffect(() => {
|
|
45
|
-
callbackRef.current = callback;
|
|
46
|
-
|
|
47
|
-
return () => {
|
|
48
|
-
callbackRef.current = () => false;
|
|
49
|
-
};
|
|
50
|
-
}, deps);
|
|
51
|
-
|
|
52
|
-
React.useEffect(() => {
|
|
53
|
-
const _refElement = ref.current;
|
|
54
|
-
|
|
55
|
-
const scrollCallback = ({ x, y }: Vector2) => {
|
|
56
|
-
const now: number = Date.now();
|
|
57
|
-
const deltaTime = Math.min(now - lastTimeStamp.current, 64);
|
|
58
|
-
lastTimeStamp.current = now;
|
|
59
|
-
const t = deltaTime / 1000; // seconds
|
|
60
|
-
|
|
61
|
-
scrollXY.current = { x, y };
|
|
62
|
-
|
|
63
|
-
// Clear if scrolling
|
|
64
|
-
if (_isScrolling.current !== -1) {
|
|
65
|
-
isScrolling.current = true;
|
|
66
|
-
clearTimeout(_isScrolling.current);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
_isScrolling.current = setTimeout(() => {
|
|
70
|
-
isScrolling.current = false;
|
|
71
|
-
scrollDirection.current = { x: 0, y: 0 };
|
|
72
|
-
|
|
73
|
-
// Reset Velocity
|
|
74
|
-
velocity.current = { x: 0, y: 0 };
|
|
75
|
-
|
|
76
|
-
handleCallback(); // Debounce 250milliseconds
|
|
77
|
-
}, 250);
|
|
78
|
-
|
|
79
|
-
const diffX = scrollXY.current.x - previousScrollXY.current.x;
|
|
80
|
-
const diffY = scrollXY.current.y - previousScrollXY.current.y;
|
|
81
|
-
|
|
82
|
-
scrollDirection.current = {
|
|
83
|
-
x: Math.sign(diffX),
|
|
84
|
-
y: Math.sign(diffY),
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
velocity.current = {
|
|
88
|
-
x: clamp(diffX / t / 1000, -5, 5),
|
|
89
|
-
y: clamp(diffY / t / 1000, -5, 5),
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
previousScrollXY.current = {
|
|
93
|
-
x: scrollXY.current.x,
|
|
94
|
-
y: scrollXY.current.y,
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
handleCallback();
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const scrollListener: () => void = () => {
|
|
101
|
-
const { pageYOffset: y, pageXOffset: x } = window;
|
|
102
|
-
scrollCallback({ x, y });
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const scrollElementListener: () => void = () => {
|
|
106
|
-
const x = _refElement?.scrollLeft || 0;
|
|
107
|
-
const y = _refElement?.scrollTop || 0;
|
|
108
|
-
scrollCallback({ x, y });
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
var subscribe: any;
|
|
112
|
-
if (_refElement) {
|
|
113
|
-
subscribe = attachEvents(_refElement, [
|
|
114
|
-
["scroll", scrollElementListener],
|
|
115
|
-
]);
|
|
116
|
-
} else {
|
|
117
|
-
subscribe = attachEvents(window, [["scroll", scrollListener]]);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return () => subscribe && subscribe();
|
|
121
|
-
}, []);
|
|
122
|
-
|
|
123
|
-
return () => ({ ref }); // ...bind()
|
|
124
|
-
}
|