react-ui-animate 1.2.1 → 1.4.1

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.
Files changed (108) hide show
  1. package/dist/{animate → animation}/getInitialConfig.d.ts +0 -0
  2. package/dist/animation/index.d.ts +4 -0
  3. package/dist/{Interpolation.d.ts → animation/interpolation.d.ts} +0 -0
  4. package/dist/{Modules.d.ts → animation/modules.d.ts} +15 -0
  5. package/dist/{useAnimatedValue.d.ts → animation/useAnimatedValue.d.ts} +2 -2
  6. package/dist/{useMountedValue.d.ts → animation/useMountedValue.d.ts} +3 -3
  7. package/dist/gestures/controllers/DragGesture.d.ts +17 -0
  8. package/dist/gestures/controllers/Gesture.d.ts +20 -0
  9. package/dist/gestures/controllers/MouseMoveGesture.d.ts +13 -0
  10. package/dist/gestures/controllers/ScrollGesture.d.ts +14 -0
  11. package/dist/gestures/controllers/WheelGesture.d.ts +15 -0
  12. package/dist/gestures/controllers/index.d.ts +4 -0
  13. package/dist/gestures/eventAttacher.d.ts +11 -0
  14. package/dist/{animate → gestures}/hooks/index.d.ts +2 -4
  15. package/dist/gestures/hooks/useDrag.d.ts +4 -0
  16. package/dist/gestures/hooks/useGesture.d.ts +9 -0
  17. package/dist/gestures/hooks/useMouseMove.d.ts +4 -0
  18. package/dist/gestures/hooks/useRecognizer.d.ts +10 -0
  19. package/dist/gestures/hooks/useScroll.d.ts +4 -0
  20. package/dist/gestures/hooks/useWheel.d.ts +4 -0
  21. package/dist/gestures/index.d.ts +2 -0
  22. package/dist/{Math.d.ts → gestures/math.d.ts} +0 -0
  23. package/dist/{animate/Types.d.ts → gestures/types.d.ts} +17 -30
  24. package/dist/gestures/withDefault.d.ts +4 -0
  25. package/dist/hooks/index.d.ts +0 -4
  26. package/dist/hooks/useMeasure.d.ts +10 -1
  27. package/dist/hooks/useWindowDimension.d.ts +7 -1
  28. package/dist/index.d.ts +2 -5
  29. package/dist/index.js +848 -707
  30. package/dist/index.js.map +1 -1
  31. package/dist/utils/index.d.ts +1 -0
  32. package/dist/{animate → utils}/isDefined.d.ts +0 -0
  33. package/package.json +6 -3
  34. package/src/{getInitialConfig.ts → animation/getInitialConfig.ts} +0 -0
  35. package/src/animation/index.ts +4 -0
  36. package/src/{Interpolation.ts → animation/interpolation.ts} +0 -0
  37. package/src/{Modules.tsx → animation/modules.tsx} +26 -1
  38. package/src/{useAnimatedValue.ts → animation/useAnimatedValue.ts} +5 -8
  39. package/src/{useMountedValue.ts → animation/useMountedValue.ts} +4 -7
  40. package/src/gestures/controllers/DragGesture.ts +176 -0
  41. package/src/gestures/controllers/Gesture.ts +54 -0
  42. package/src/gestures/controllers/MouseMoveGesture.ts +111 -0
  43. package/src/gestures/controllers/ScrollGesture.ts +107 -0
  44. package/src/gestures/controllers/WheelGesture.ts +123 -0
  45. package/src/gestures/controllers/index.ts +4 -0
  46. package/src/gestures/eventAttacher.ts +67 -0
  47. package/src/gestures/hooks/index.ts +5 -0
  48. package/src/gestures/hooks/useDrag.ts +14 -0
  49. package/src/gestures/hooks/useGesture.ts +38 -0
  50. package/src/gestures/hooks/useMouseMove.ts +11 -0
  51. package/src/gestures/hooks/useRecognizer.ts +59 -0
  52. package/src/gestures/hooks/useScroll.ts +11 -0
  53. package/src/gestures/hooks/useWheel.ts +11 -0
  54. package/src/gestures/index.ts +2 -0
  55. package/src/{Math.ts → gestures/math.ts} +0 -0
  56. package/src/{Types.ts → gestures/types.ts} +19 -36
  57. package/src/gestures/withDefault.ts +3 -0
  58. package/src/hooks/index.ts +0 -4
  59. package/src/hooks/useMeasure.ts +11 -1
  60. package/src/hooks/useOutsideClick.ts +3 -2
  61. package/src/hooks/useWindowDimension.ts +7 -1
  62. package/src/index.ts +2 -5
  63. package/src/utils/index.ts +1 -0
  64. package/src/{isDefined.ts → utils/isDefined.ts} +0 -0
  65. package/tsconfig.json +1 -0
  66. package/dist/Types.d.ts +0 -64
  67. package/dist/animate/Interpolation.d.ts +0 -20
  68. package/dist/animate/Math.d.ts +0 -34
  69. package/dist/animate/Modules.d.ts +0 -32
  70. package/dist/animate/controllers/EventAttacher.d.ts +0 -8
  71. package/dist/animate/controllers/index.d.ts +0 -1
  72. package/dist/animate/hooks/useDrag.d.ts +0 -5
  73. package/dist/animate/hooks/useMeasure.d.ts +0 -5
  74. package/dist/animate/hooks/useMouseMove.d.ts +0 -5
  75. package/dist/animate/hooks/useOutsideClick.d.ts +0 -2
  76. package/dist/animate/hooks/useScroll.d.ts +0 -5
  77. package/dist/animate/hooks/useWheel.d.ts +0 -5
  78. package/dist/animate/hooks/useWindowDimension.d.ts +0 -3
  79. package/dist/animate/index.d.ts +0 -8
  80. package/dist/animate/useAnimatedValue.d.ts +0 -30
  81. package/dist/animate/useMountedValue.d.ts +0 -22
  82. package/dist/controllers/EventAttacher.d.ts +0 -8
  83. package/dist/controllers/index.d.ts +0 -1
  84. package/dist/core/Animation.d.ts +0 -13
  85. package/dist/core/Bezier.d.ts +0 -8
  86. package/dist/core/Colors.d.ts +0 -25
  87. package/dist/core/Easing.d.ts +0 -40
  88. package/dist/core/Interpolation.d.ts +0 -38
  89. package/dist/core/RequestAnimationFrame.d.ts +0 -8
  90. package/dist/core/SpringAnimation.d.ts +0 -39
  91. package/dist/core/Tags.d.ts +0 -2
  92. package/dist/core/TimingAnimation.d.ts +0 -34
  93. package/dist/core/TransformStyles.d.ts +0 -8
  94. package/dist/core/animated.d.ts +0 -9
  95. package/dist/core/index.d.ts +0 -7
  96. package/dist/core/useTransition.d.ts +0 -46
  97. package/dist/getInitialConfig.d.ts +0 -3
  98. package/dist/hooks/useDrag.d.ts +0 -5
  99. package/dist/hooks/useMouseMove.d.ts +0 -5
  100. package/dist/hooks/useScroll.d.ts +0 -5
  101. package/dist/hooks/useWheel.d.ts +0 -5
  102. package/dist/isDefined.d.ts +0 -1
  103. package/src/controllers/EventAttacher.ts +0 -35
  104. package/src/controllers/index.ts +0 -1
  105. package/src/hooks/useDrag.ts +0 -231
  106. package/src/hooks/useMouseMove.ts +0 -123
  107. package/src/hooks/useScroll.ts +0 -124
  108. package/src/hooks/useWheel.ts +0 -144
@@ -1,38 +0,0 @@
1
- import { TransitionValue } from "./useTransition";
2
- declare type ExtrapolateType = "identity" | "extend" | "clamp";
3
- export interface ExtrapolateConfig {
4
- extrapolate?: ExtrapolateType;
5
- extrapolateRight?: ExtrapolateType;
6
- extrapolateLeft?: ExtrapolateType;
7
- }
8
- /**
9
- * interpolateNumbers to interpolate the numeric value
10
- * @param value - number
11
- * @param inputRange
12
- * @param outputRange
13
- * @param extrapolateConfig
14
- * @returns - number | string
15
- */
16
- export declare function interpolateNumbers(value: number, inputRange: Array<number>, outputRange: Array<number | string>, extrapolateConfig?: ExtrapolateConfig): any;
17
- /**
18
- * interpolateTransitionValue to interpolating TransitionValue type value
19
- * @param value
20
- * @param inputRange
21
- * @param outputRange
22
- * @param extrapolateConfig
23
- * @returns TransitionValue
24
- */
25
- export declare const interpolateTransitionValue: (value: TransitionValue, inputRange: Array<number>, outputRange: Array<number | string>, extrapolateConfig?: ExtrapolateConfig | undefined) => {
26
- isInterpolation: boolean;
27
- interpolationConfig: {
28
- inputRange: number[];
29
- outputRange: (string | number)[];
30
- extrapolateConfig: ExtrapolateConfig | undefined;
31
- };
32
- _subscribe: (onUpdate: import("./useTransition").SubscriptionValue) => void;
33
- _value: string | number;
34
- _currentValue: import("react").MutableRefObject<string | number>;
35
- get: () => string | number;
36
- _config?: import("./useTransition").UseTransitionConfig | undefined;
37
- };
38
- export {};
@@ -1,8 +0,0 @@
1
- export declare const RequestAnimationFrame: {
2
- current: (cb: any) => number;
3
- inject(injected: any): void;
4
- };
5
- export declare const CancelAnimationFrame: {
6
- current: (id: any) => void;
7
- inject(injected: any): void;
8
- };
@@ -1,39 +0,0 @@
1
- import { Animation, ResultType } from "./Animation";
2
- import { UseTransitionConfig } from "./useTransition";
3
- /**
4
- * SpringAnimation class implements physics based spring animation
5
- */
6
- export declare class SpringAnimation extends Animation {
7
- _overshootClamping: boolean;
8
- _restDisplacementThreshold: number;
9
- _restSpeedThreshold: number;
10
- _initialVelocity?: number;
11
- _lastVelocity: number;
12
- _startPosition: number;
13
- _lastPosition: number;
14
- _fromValue: number;
15
- _toValue: any;
16
- _mass: number;
17
- _tension: number;
18
- _friction: number;
19
- _lastTime: number;
20
- _onFrame: (value: number) => void;
21
- _animationFrame: any;
22
- _immediate: boolean;
23
- _delay: number;
24
- _onRest?: (value: ResultType) => void;
25
- constructor({ initialPosition, config, }: {
26
- initialPosition: number;
27
- config?: Omit<UseTransitionConfig, "duration" | "easing">;
28
- });
29
- onUpdate(): void;
30
- stop(): void;
31
- set(toValue: number): void;
32
- start({ toValue, onFrame, previousAnimation, onEnd, immediate, }: {
33
- toValue: number;
34
- onFrame: (value: number) => void;
35
- previousAnimation?: SpringAnimation;
36
- onEnd?: (result: ResultType) => void;
37
- immediate?: boolean;
38
- }): void;
39
- }
@@ -1,2 +0,0 @@
1
- export declare const tags: string[];
2
- export declare const unitlessStyleProps: string[];
@@ -1,34 +0,0 @@
1
- import { Animation, ResultType } from "./Animation";
2
- import { UseTransitionConfig } from "./useTransition";
3
- /**
4
- * TimingAnimation class implements duration based spring animation
5
- */
6
- export declare class TimingAnimation extends Animation {
7
- _startTime: number;
8
- _fromValue: number;
9
- _toValue: any;
10
- _duration: number;
11
- _easing: (value: number) => number;
12
- _onFrame: (value: number) => void;
13
- _animationFrame: any;
14
- _timeout: any;
15
- _position: number;
16
- _immediate: boolean;
17
- _delay: number;
18
- _tempDuration: number;
19
- _onRest?: (value: ResultType) => void;
20
- constructor({ initialPosition, config, }: {
21
- initialPosition: number;
22
- config: Omit<UseTransitionConfig, "mass" | "friction" | "tension">;
23
- });
24
- onUpdate(): void;
25
- stop(): void;
26
- set(toValue: number): void;
27
- start({ toValue, onFrame, onEnd, immediate, duration, }: {
28
- toValue: number;
29
- onFrame: (value: number) => void;
30
- onEnd?: (result: ResultType) => void;
31
- immediate?: boolean;
32
- duration?: number;
33
- }): void;
34
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * style keys which can be accepted by animated component
3
- */
4
- export declare const styleTrasformKeys: string[];
5
- /**
6
- * getTransform function returns transform string from style object
7
- */
8
- export declare function getTransform(style: any): any;
@@ -1,9 +0,0 @@
1
- import * as React from "react";
2
- /**
3
- * isSubscriber to check the value is TransitionValue or not
4
- * @param value - any
5
- * @returns - boolean
6
- */
7
- export declare const isSubscriber: (value: any) => any;
8
- export declare function makeAnimatedComponent(WrapperComponent: React.ComponentType | keyof JSX.IntrinsicElements): React.ForwardRefExoticComponent<Pick<any, string | number | symbol> & React.RefAttributes<unknown>>;
9
- export declare const animated: any;
@@ -1,7 +0,0 @@
1
- export * from "./animated";
2
- export * from "./useTransition";
3
- export * from "./Easing";
4
- export * from "./Animation";
5
- export * from "./Colors";
6
- export * from "./Bezier";
7
- export * from "./Interpolation";
@@ -1,46 +0,0 @@
1
- import * as React from "react";
2
- import { ResultType } from "./Animation";
3
- /**
4
- * UseTransitionConfig for useTransition config
5
- */
6
- export interface UseTransitionConfig {
7
- mass?: number;
8
- tension?: number;
9
- friction?: number;
10
- duration?: number;
11
- easing?: (t: number) => number;
12
- immediate?: boolean;
13
- delay?: number;
14
- onChange?: (value: number) => void;
15
- onRest?: (value: any) => void;
16
- }
17
- export declare type SubscriptionValue = (updatedValue: AssignValue, callback?: (result: ResultType) => void) => void;
18
- /**
19
- * useTransition returns TransitionValue object
20
- */
21
- export declare type TransitionValue = {
22
- _subscribe: (onUpdate: SubscriptionValue) => void;
23
- _value: number | string;
24
- _currentValue: React.MutableRefObject<number | string>;
25
- get: () => number | string;
26
- _config?: UseTransitionConfig;
27
- };
28
- /**
29
- * Assign value object to set animation
30
- */
31
- export declare type AssignValue = {
32
- toValue: number | string;
33
- immediate?: boolean;
34
- duration?: number;
35
- };
36
- /**
37
- * useTransition return type
38
- */
39
- export declare type UseTransitionReturn = [TransitionValue, SubscriptionValue];
40
- /**
41
- * useTransition() hook for time and spring based animations
42
- * @param initialValue numbers are animatable and strings are non-animatable
43
- * @param config
44
- * @returns [value, setValue]
45
- */
46
- export declare const useTransition: (initialValue: number | string, config?: UseTransitionConfig | undefined) => UseTransitionReturn;
@@ -1,3 +0,0 @@
1
- import { GenericAnimationConfig } from "./useAnimatedValue";
2
- export declare type InitialConfigType = "ease" | "elastic" | "stiff" | "wooble" | "bounce" | undefined;
3
- export declare const getInitialConfig: (animationType: InitialConfigType) => GenericAnimationConfig;
@@ -1,5 +0,0 @@
1
- import * as React from "react";
2
- import { DragEventType, UseDragConfig } from "../Types";
3
- export declare function useDrag(callback: (event: DragEventType) => void, config?: UseDragConfig, deps?: React.DependencyList): (index?: number | undefined) => {
4
- ref: any;
5
- };
@@ -1,5 +0,0 @@
1
- import * as React from "react";
2
- import { MouseMoveEventType } from "../Types";
3
- export declare function useMouseMove(callback: (event: MouseMoveEventType) => void, deps?: React.DependencyList): () => {
4
- ref: React.MutableRefObject<any>;
5
- };
@@ -1,5 +0,0 @@
1
- import * as React from "react";
2
- import { ScrollEventType } from "../Types";
3
- export declare function useScroll(callback: (event: ScrollEventType) => void, deps?: React.DependencyList): () => {
4
- ref: React.MutableRefObject<any>;
5
- };
@@ -1,5 +0,0 @@
1
- import * as React from "react";
2
- import { WheelEventType } from "../Types";
3
- export declare function useWheel(callback: (event: WheelEventType) => void, deps?: React.DependencyList): () => {
4
- ref: React.MutableRefObject<any>;
5
- };
@@ -1 +0,0 @@
1
- export declare const isDefined: <T>(value: T) => boolean;
@@ -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
- }
@@ -1 +0,0 @@
1
- export * from "./EventAttacher";
@@ -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
- }