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
@@ -0,0 +1,176 @@
1
+ import { attachEvents } from "../eventAttacher";
2
+ import { Vector2 } from "../types";
3
+ import { clamp } from "../math";
4
+ import { withDefault } from "../withDefault";
5
+ import { Gesture } from "./Gesture";
6
+
7
+ export class DragGesture extends Gesture {
8
+ movementStart: Vector2 = withDefault(0, 0);
9
+ initialMovement: Vector2 = withDefault(0, 0);
10
+ movement: Vector2 = withDefault(0, 0);
11
+ previousMovement: Vector2 = withDefault(0, 0);
12
+ translation: Vector2 = withDefault(0, 0);
13
+ offset: Vector2 = withDefault(0, 0);
14
+ velocity: Vector2 = withDefault(0, 0);
15
+
16
+ // @override
17
+ // initialize the events
18
+ _initEvents() {
19
+ if (this.targetElement || this.targetElements.length > 0) {
20
+ this._subscribe = attachEvents(
21
+ [window],
22
+ [
23
+ ["mousedown", this.pointerDown.bind(this)],
24
+ ["mousemove", this.pointerMove.bind(this)],
25
+ ["mouseup", this.pointerUp.bind(this)],
26
+ ["touchstart", this.pointerDown.bind(this), { passive: false }],
27
+ ["touchmove", this.pointerMove.bind(this), { passive: false }],
28
+ ["touchend", this.pointerUp.bind(this)],
29
+ ]
30
+ );
31
+ }
32
+ }
33
+
34
+ // @override - cancel events
35
+ // we only canceled down and move events because mouse up
36
+ // will not be triggered
37
+ _cancelEvents() {
38
+ if (this._subscribe) {
39
+ this._subscribe(["mousedown", "mousemove", "touchstart", "touchmove"]);
40
+ }
41
+ }
42
+
43
+ _handleCallback() {
44
+ if (this.callback) {
45
+ this.callback({
46
+ args: [this.currentIndex],
47
+ down: this.isActive,
48
+ movementX: this.movement.x,
49
+ movementY: this.movement.y,
50
+ offsetX: this.translation.x,
51
+ offsetY: this.translation.y,
52
+ velocityX: this.velocity.x,
53
+ velocityY: this.velocity.y,
54
+ distanceX: Math.abs(this.movement.x),
55
+ distanceY: Math.abs(this.movement.y),
56
+ directionX: Math.sign(this.movement.x),
57
+ directionY: Math.sign(this.movement.y),
58
+ cancel: () => {
59
+ this._cancelEvents();
60
+ },
61
+ });
62
+ }
63
+ }
64
+
65
+ pointerDown(e: any) {
66
+ if (e.type === "touchstart") {
67
+ this.movementStart = {
68
+ x: e.touches[0].clientX,
69
+ y: e.touches[0].clientY,
70
+ };
71
+ } else {
72
+ this.movementStart = { x: e.clientX, y: e.clientY };
73
+ }
74
+
75
+ this.movement = { x: 0, y: 0 };
76
+ this.offset = { x: this.translation.x, y: this.translation.y };
77
+ this.previousMovement = { x: 0, y: 0 };
78
+ this.velocity = { x: 0, y: 0 };
79
+
80
+ // find current selected element
81
+ const currElem = this.targetElements.find((elem: any) => elem === e.target);
82
+
83
+ if (e.target === this.targetElement || currElem) {
84
+ this.isActive = true;
85
+ e.preventDefault();
86
+
87
+ // set args
88
+ if (currElem) {
89
+ this.currentIndex = this.targetElements.indexOf(currElem);
90
+ }
91
+
92
+ // if initial function is defined then call it to get initial movementX and movementY
93
+ // if only select to bounded draggable element
94
+ const initial = this.config?.initial && this.config.initial();
95
+ const initialMovementX = initial?.movementX;
96
+ const initialMovementY = initial?.movementY;
97
+
98
+ this.initialMovement = {
99
+ x: initialMovementX ?? 0,
100
+ y: initialMovementY ?? 0,
101
+ };
102
+
103
+ this.movement = {
104
+ x: this.initialMovement.x,
105
+ y: this.initialMovement.y,
106
+ };
107
+
108
+ this.previousMovement = {
109
+ x: this.initialMovement.x,
110
+ y: this.initialMovement.y,
111
+ };
112
+
113
+ this._handleCallback();
114
+ }
115
+ }
116
+
117
+ pointerMove(e: any) {
118
+ if (this.isActive) {
119
+ e.preventDefault();
120
+ const now = Date.now();
121
+ const deltaTime = Math.min(now - this.lastTimeStamp, 64);
122
+ this.lastTimeStamp = now;
123
+
124
+ const t = deltaTime / 1000;
125
+
126
+ if (e.type === "touchmove") {
127
+ this.movement = {
128
+ x:
129
+ this.initialMovement.x +
130
+ (e.touches[0].clientX - this.movementStart.x),
131
+ y:
132
+ this.initialMovement.y +
133
+ (e.touches[0].clientY - this.movementStart.y),
134
+ };
135
+ } else {
136
+ this.movement = {
137
+ x: this.initialMovement.x + (e.clientX - this.movementStart.x),
138
+ y: this.initialMovement.y + (e.clientY - this.movementStart.y),
139
+ };
140
+ }
141
+
142
+ this.translation = {
143
+ x: this.offset.x + this.movement.x,
144
+ y: this.offset.y + this.movement.y,
145
+ };
146
+
147
+ this.velocity = {
148
+ x: clamp(
149
+ (this.movement.x - this.previousMovement.x) / t / 1000,
150
+ -1 * Gesture._VELOCITY_LIMIT,
151
+ Gesture._VELOCITY_LIMIT
152
+ ),
153
+ y: clamp(
154
+ (this.movement.y - this.previousMovement.y) / t / 1000,
155
+ -1 * Gesture._VELOCITY_LIMIT,
156
+ Gesture._VELOCITY_LIMIT
157
+ ),
158
+ };
159
+
160
+ this.previousMovement = {
161
+ x: this.movement.x,
162
+ y: this.movement.y,
163
+ };
164
+
165
+ this._handleCallback();
166
+ }
167
+ }
168
+
169
+ pointerUp() {
170
+ if (this.isActive) {
171
+ this.isActive = false;
172
+ this._handleCallback();
173
+ this._initEvents();
174
+ }
175
+ }
176
+ }
@@ -0,0 +1,54 @@
1
+ export class Gesture {
2
+ currentIndex?: number;
3
+ lastTimeStamp: number = Date.now();
4
+ isActive: boolean = false;
5
+ targetElement?: HTMLElement; // represents the bounded element
6
+ targetElements: Array<HTMLElement> = []; // represents the bounded elements
7
+ config?: any;
8
+ callback?: <T>(event: T) => void;
9
+ _subscribe?: (eventKeys?: Array<string>) => void;
10
+ static _VELOCITY_LIMIT: number = 20;
11
+
12
+ // it must be overridden by other child classes
13
+ _initEvents() {}
14
+
15
+ // cancel events
16
+ // we only canceled down and move events because mouse up
17
+ // will not be triggered
18
+ _cancelEvents() {
19
+ if (this._subscribe) {
20
+ this._subscribe();
21
+ }
22
+ }
23
+
24
+ // re-apply new callback
25
+ applyCallback(callback: <T>(event: T) => void) {
26
+ this.callback = callback;
27
+ }
28
+
29
+ // apply gesture
30
+ applyGesture({
31
+ targetElement,
32
+ targetElements,
33
+ callback,
34
+ config,
35
+ }: {
36
+ targetElement?: any;
37
+ targetElements?: any;
38
+ callback: <T>(event: T) => void;
39
+ config?: any;
40
+ }) {
41
+ this.targetElement = targetElement;
42
+ this.targetElements = targetElements.map(
43
+ (element: { current: any }) => element.current
44
+ );
45
+ this.callback = callback;
46
+ this.config = config;
47
+
48
+ // initialize events
49
+ this._initEvents();
50
+
51
+ // unbind
52
+ return () => this._subscribe && this._subscribe();
53
+ }
54
+ }
@@ -0,0 +1,111 @@
1
+ import { attachEvents } from "../eventAttacher";
2
+ import { Vector2 } from "../types";
3
+ import { clamp } from "../math";
4
+ import { withDefault } from "../withDefault";
5
+ import { Gesture } from "./Gesture";
6
+
7
+ export class MouseMoveGesture extends Gesture {
8
+ event?: MouseEvent;
9
+ isActiveID?: any;
10
+ movement: Vector2 = withDefault(0, 0);
11
+ previousMovement: Vector2 = withDefault(0, 0);
12
+ velocity: Vector2 = withDefault(0, 0);
13
+ direction: Vector2 = withDefault(0, 0);
14
+
15
+ // @override
16
+ // initialize the events
17
+ _initEvents() {
18
+ if (this.targetElement) {
19
+ this._subscribe = attachEvents(
20
+ [this.targetElement],
21
+ [["mousemove", this.onMouseMove.bind(this)]]
22
+ );
23
+ } else if (this.targetElements.length > 0) {
24
+ this._subscribe = attachEvents(this.targetElements, [
25
+ ["mousemove", this.onMouseMove.bind(this)],
26
+ ]);
27
+ } else {
28
+ this._subscribe = attachEvents(
29
+ [window],
30
+ [["mousemove", this.onMouseMove.bind(this)]]
31
+ );
32
+ }
33
+ }
34
+
35
+ _handleCallback() {
36
+ if (this.callback) {
37
+ this.callback({
38
+ args: [this.currentIndex],
39
+ event: this.event,
40
+ isMoving: this.isActive,
41
+ target: this.event?.target,
42
+ mouseX: this.movement.x,
43
+ mouseY: this.movement.y,
44
+ velocityX: this.velocity.x,
45
+ velocityY: this.velocity.y,
46
+ directionX: this.direction.x,
47
+ directionY: this.direction.y,
48
+ });
49
+ }
50
+ }
51
+
52
+ onMouseMove(e: MouseEvent) {
53
+ // find current selected element
54
+ const currElem = this.targetElements.find((elem: any) => elem === e.target);
55
+
56
+ // set args
57
+ if (currElem) {
58
+ this.currentIndex = this.targetElements.indexOf(currElem);
59
+ }
60
+
61
+ this.event = e;
62
+
63
+ const now: number = Date.now();
64
+ const deltaTime = Math.min(now - this.lastTimeStamp, 64);
65
+ this.lastTimeStamp = now;
66
+ const t = deltaTime / 1000; // seconds
67
+
68
+ const x = e.clientX;
69
+ const y = e.clientY;
70
+
71
+ this.movement = { x, y };
72
+
73
+ if (this.isActiveID !== -1) {
74
+ this.isActive = true;
75
+ clearTimeout(this.isActiveID);
76
+ }
77
+
78
+ this.isActiveID = setTimeout(() => {
79
+ this.isActive = false;
80
+ this.direction = { x: 0, y: 0 };
81
+ this.velocity = { x: 0, y: 0 };
82
+
83
+ this._handleCallback();
84
+ }, 250); // Debounce 250 milliseconds
85
+
86
+ const diffX = this.movement.x - this.previousMovement.x;
87
+ const diffY = this.movement.y - this.previousMovement.y;
88
+
89
+ this.direction = {
90
+ x: Math.sign(diffX),
91
+ y: Math.sign(diffY),
92
+ };
93
+
94
+ this.velocity = {
95
+ x: clamp(
96
+ diffX / t / 1000,
97
+ -1 * Gesture._VELOCITY_LIMIT,
98
+ Gesture._VELOCITY_LIMIT
99
+ ),
100
+ y: clamp(
101
+ diffY / t / 1000,
102
+ -1 * Gesture._VELOCITY_LIMIT,
103
+ Gesture._VELOCITY_LIMIT
104
+ ),
105
+ };
106
+
107
+ this.previousMovement = { x: this.movement.x, y: this.movement.y };
108
+
109
+ this._handleCallback();
110
+ }
111
+ }
@@ -0,0 +1,107 @@
1
+ import { attachEvents } from "../eventAttacher";
2
+ import { Vector2 } from "../types";
3
+ import { clamp } from "../math";
4
+ import { withDefault } from "../withDefault";
5
+ import { Gesture } from "./Gesture";
6
+
7
+ export class ScrollGesture extends Gesture {
8
+ isActiveID?: any;
9
+ movement: Vector2 = withDefault(0, 0);
10
+ previousMovement: Vector2 = withDefault(0, 0);
11
+ direction: Vector2 = withDefault(0, 0);
12
+ velocity: Vector2 = withDefault(0, 0);
13
+
14
+ // @override
15
+ // initialize the events
16
+ _initEvents() {
17
+ if (this.targetElement) {
18
+ this._subscribe = attachEvents(
19
+ [this.targetElement],
20
+ [["scroll", this.scrollElementListener.bind(this)]]
21
+ );
22
+ } else {
23
+ this._subscribe = attachEvents(
24
+ [window],
25
+ [["scroll", this.scrollListener.bind(this)]]
26
+ );
27
+ }
28
+ }
29
+
30
+ _handleCallback() {
31
+ if (this.callback) {
32
+ this.callback({
33
+ isScrolling: this.isActive,
34
+ scrollX: this.movement.x,
35
+ scrollY: this.movement.y,
36
+ velocityX: this.velocity.x,
37
+ velocityY: this.velocity.y,
38
+ directionX: this.direction.x,
39
+ directionY: this.direction.y,
40
+ });
41
+ }
42
+ }
43
+
44
+ onScroll({ x, y }: Vector2) {
45
+ const now: number = Date.now();
46
+ const deltaTime = Math.min(now - this.lastTimeStamp, 64);
47
+ this.lastTimeStamp = now;
48
+ const t = deltaTime / 1000; // seconds
49
+
50
+ this.movement = { x, y };
51
+
52
+ // Clear if scrolling
53
+ if (this.isActiveID !== -1) {
54
+ this.isActive = true;
55
+ clearTimeout(this.isActiveID);
56
+ }
57
+
58
+ this.isActiveID = setTimeout(() => {
59
+ this.isActive = false;
60
+ this.direction = { x: 0, y: 0 };
61
+
62
+ // Reset Velocity
63
+ this.velocity = { x: 0, y: 0 };
64
+
65
+ this._handleCallback(); // Debounce 250milliseconds
66
+ }, 250);
67
+
68
+ const diffX = this.movement.x - this.previousMovement.x;
69
+ const diffY = this.movement.y - this.previousMovement.y;
70
+
71
+ this.direction = {
72
+ x: Math.sign(diffX),
73
+ y: Math.sign(diffY),
74
+ };
75
+
76
+ this.velocity = {
77
+ x: clamp(
78
+ diffX / t / 1000,
79
+ -1 * Gesture._VELOCITY_LIMIT,
80
+ Gesture._VELOCITY_LIMIT
81
+ ),
82
+ y: clamp(
83
+ diffY / t / 1000,
84
+ -1 * Gesture._VELOCITY_LIMIT,
85
+ Gesture._VELOCITY_LIMIT
86
+ ),
87
+ };
88
+
89
+ this.previousMovement = {
90
+ x: this.movement.x,
91
+ y: this.movement.y,
92
+ };
93
+
94
+ this._handleCallback();
95
+ }
96
+
97
+ scrollListener() {
98
+ const { pageYOffset: y, pageXOffset: x } = window;
99
+ this.onScroll({ x, y });
100
+ }
101
+
102
+ scrollElementListener() {
103
+ const x = this.targetElement?.scrollLeft || 0;
104
+ const y = this.targetElement?.scrollTop || 0;
105
+ this.onScroll({ x, y });
106
+ }
107
+ }
@@ -0,0 +1,123 @@
1
+ import { attachEvents } from "../eventAttacher";
2
+ import { Vector2 } from "../types";
3
+ import { clamp } from "../math";
4
+ import { withDefault } from "../withDefault";
5
+ import { Gesture } from "./Gesture";
6
+
7
+ const LINE_HEIGHT = 40;
8
+ const PAGE_HEIGHT = 800;
9
+
10
+ export class WheelGesture extends Gesture {
11
+ isActiveID?: any;
12
+ movement: Vector2 = withDefault(0, 0);
13
+ previousMovement: Vector2 = withDefault(0, 0);
14
+ direction: Vector2 = withDefault(0, 0);
15
+ velocity: Vector2 = withDefault(0, 0);
16
+ delta: Vector2 = withDefault(0, 0);
17
+
18
+ // Holds offsets
19
+ offset: Vector2 = withDefault(0, 0);
20
+ translation: Vector2 = withDefault(0, 0);
21
+
22
+ // @override
23
+ // initialize the events
24
+ _initEvents() {
25
+ if (this.targetElement) {
26
+ this._subscribe = attachEvents(
27
+ [this.targetElement],
28
+ [["wheel", this.onWheel.bind(this)]]
29
+ );
30
+ }
31
+ }
32
+
33
+ _handleCallback() {
34
+ if (this.callback) {
35
+ this.callback({
36
+ target: this.targetElement,
37
+ isWheeling: this.isActive,
38
+ deltaX: this.delta.x,
39
+ deltaY: this.delta.y,
40
+ directionX: this.direction.x,
41
+ directionY: this.direction.y,
42
+ movementX: this.movement.x,
43
+ movementY: this.movement.y,
44
+ offsetX: this.offset.x,
45
+ offsetY: this.offset.y,
46
+ velocityX: this.velocity.x,
47
+ velocityY: this.velocity.y,
48
+ });
49
+ }
50
+ }
51
+
52
+ onWheel(event: WheelEvent) {
53
+ let { deltaX, deltaY, deltaMode } = event;
54
+
55
+ const now: number = Date.now();
56
+ const deltaTime = Math.min(now - this.lastTimeStamp, 64);
57
+ this.lastTimeStamp = now;
58
+ const t = deltaTime / 1000; // seconds
59
+
60
+ this.isActive = true;
61
+
62
+ if (this.isActiveID !== -1) {
63
+ this.isActive = true;
64
+ clearTimeout(this.isActiveID);
65
+ }
66
+
67
+ this.isActiveID = setTimeout(() => {
68
+ this.isActive = false;
69
+ this.translation = { x: this.offset.x, y: this.offset.y };
70
+ this._handleCallback();
71
+
72
+ this.velocity = { x: 0, y: 0 }; // Reset Velocity
73
+ this.movement = { x: 0, y: 0 };
74
+ }, 200);
75
+
76
+ // normalize wheel values, especially for Firefox
77
+ if (deltaMode === 1) {
78
+ deltaX *= LINE_HEIGHT;
79
+ deltaY *= LINE_HEIGHT;
80
+ } else if (deltaMode === 2) {
81
+ deltaX *= PAGE_HEIGHT;
82
+ deltaY *= PAGE_HEIGHT;
83
+ }
84
+
85
+ this.delta = { x: deltaX, y: deltaY };
86
+ this.movement = {
87
+ x: this.movement.x + deltaX,
88
+ y: this.movement.y + deltaY,
89
+ };
90
+ this.offset = {
91
+ x: this.translation.x + this.movement.x,
92
+ y: this.translation.y + this.movement.y,
93
+ };
94
+
95
+ const diffX = this.movement.x - this.previousMovement.x;
96
+ const diffY = this.movement.y - this.previousMovement.y;
97
+
98
+ this.direction = {
99
+ x: Math.sign(diffX),
100
+ y: Math.sign(diffY),
101
+ };
102
+
103
+ this.velocity = {
104
+ x: clamp(
105
+ diffX / t / 1000,
106
+ -1 * Gesture._VELOCITY_LIMIT,
107
+ Gesture._VELOCITY_LIMIT
108
+ ),
109
+ y: clamp(
110
+ diffY / t / 1000,
111
+ -1 * Gesture._VELOCITY_LIMIT,
112
+ Gesture._VELOCITY_LIMIT
113
+ ),
114
+ };
115
+
116
+ this.previousMovement = {
117
+ x: this.movement.x,
118
+ y: this.movement.y,
119
+ };
120
+
121
+ this._handleCallback();
122
+ }
123
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./DragGesture";
2
+ export * from "./MouseMoveGesture";
3
+ export * from "./ScrollGesture";
4
+ export * from "./WheelGesture";
@@ -0,0 +1,67 @@
1
+ type MouseEventType =
2
+ | "click"
3
+ | "dblclick"
4
+ | "mousedown"
5
+ | "mousemove"
6
+ | "mouseup"
7
+ | "touchstart"
8
+ | "touchmove"
9
+ | "touchend"
10
+ | "mouseenter"
11
+ | "mouseleave"
12
+ | "mouseout"
13
+ | "mouseover"
14
+ | "scroll"
15
+ | "wheel"
16
+ | "contextmenu";
17
+
18
+ type DomTargetTypes = Array<Window | Document | HTMLElement>;
19
+
20
+ /**
21
+ * Attach single document / window event / HTMLElement
22
+ */
23
+ function attachEvent(
24
+ domTargets: DomTargetTypes,
25
+ event: MouseEventType,
26
+ callback: (e: any) => void,
27
+ capture: any = false
28
+ ) {
29
+ domTargets.forEach((target) => {
30
+ target.addEventListener(event, callback, capture);
31
+ });
32
+
33
+ return function () {
34
+ domTargets.forEach((target) => {
35
+ target.removeEventListener(event, callback, capture);
36
+ });
37
+ };
38
+ }
39
+
40
+ /**
41
+ * Attach multiple document / window event / HTMLElement
42
+ */
43
+ export function attachEvents(
44
+ domTargets: DomTargetTypes,
45
+ events: Array<
46
+ [event: MouseEventType, callback: (e: any) => void, capture?: any]
47
+ >
48
+ ) {
49
+ const subscribers = new Map();
50
+
51
+ events.forEach(function ([event, callback, capture = false]) {
52
+ subscribers.set(event, attachEvent(domTargets, event, callback, capture));
53
+ });
54
+
55
+ return function (eventKeys?: Array<string>) {
56
+ for (const [eventKey, subscriber] of subscribers.entries()) {
57
+ if (!eventKeys) {
58
+ subscriber();
59
+ return;
60
+ }
61
+
62
+ if (eventKeys.indexOf(eventKey) !== -1) {
63
+ subscriber();
64
+ }
65
+ }
66
+ };
67
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./useDrag";
2
+ export * from "./useMouseMove";
3
+ export * from "./useScroll";
4
+ export * from "./useWheel";
5
+ export * from "./useGesture";
@@ -0,0 +1,14 @@
1
+ import * as React from "react";
2
+
3
+ import { DragEventType, UseDragConfig } from "../types";
4
+ import { DragGesture } from "../controllers";
5
+ import { useRecognizer } from "./useRecognizer";
6
+
7
+ export function useDrag(
8
+ callback: (event: DragEventType) => void,
9
+ config?: UseDragConfig
10
+ ) {
11
+ const gesture = React.useRef(new DragGesture()).current;
12
+
13
+ return useRecognizer([["drag", gesture, callback, config]]);
14
+ }
@@ -0,0 +1,38 @@
1
+ import * as React from "react";
2
+ import {
3
+ DragGesture,
4
+ MouseMoveGesture,
5
+ ScrollGesture,
6
+ WheelGesture,
7
+ } from "../controllers";
8
+ import {
9
+ DragEventType,
10
+ WheelEventType,
11
+ ScrollEventType,
12
+ MouseMoveEventType,
13
+ } from "../types";
14
+ import { useRecognizer } from "./useRecognizer";
15
+
16
+ export function useGesture({
17
+ onDrag,
18
+ onWheel,
19
+ onScroll,
20
+ onMouseMove,
21
+ }: {
22
+ onDrag?: (event: DragEventType) => void;
23
+ onWheel?: (event: WheelEventType) => void;
24
+ onScroll?: (event: ScrollEventType) => void;
25
+ onMouseMove?: (event: MouseMoveEventType) => void;
26
+ }) {
27
+ const dragGesture = React.useRef(new DragGesture()).current;
28
+ const wheelGesture = React.useRef(new WheelGesture()).current;
29
+ const scrollGesture = React.useRef(new ScrollGesture()).current;
30
+ const mouseMoveGesture = React.useRef(new MouseMoveGesture()).current;
31
+
32
+ return useRecognizer([
33
+ ["drag", dragGesture, onDrag],
34
+ ["wheel", wheelGesture, onWheel],
35
+ ["scroll", scrollGesture, onScroll],
36
+ ["move", mouseMoveGesture, onMouseMove],
37
+ ]);
38
+ }