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
|
@@ -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,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,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
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { MouseMoveEventType } from "../types";
|
|
4
|
+
import { MouseMoveGesture } from "../controllers";
|
|
5
|
+
import { useRecognizer } from "./useRecognizer";
|
|
6
|
+
|
|
7
|
+
export function useMouseMove(callback: (event: MouseMoveEventType) => void) {
|
|
8
|
+
const gesture = React.useRef(new MouseMoveGesture()).current;
|
|
9
|
+
|
|
10
|
+
return useRecognizer([["move", gesture, callback]]);
|
|
11
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/* eslint-disable react-hooks/exhaustive-deps */
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
|
|
4
|
+
type UseRecognizerHandlerType = Array<
|
|
5
|
+
[
|
|
6
|
+
key: "drag" | "wheel" | "move" | "scroll",
|
|
7
|
+
gesture: any,
|
|
8
|
+
callback: any,
|
|
9
|
+
config?: any
|
|
10
|
+
]
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
export const useRecognizer = (handlers: UseRecognizerHandlerType) => {
|
|
14
|
+
const ref = React.useRef<any>();
|
|
15
|
+
const elementRefs = React.useRef<Array<any>>([]);
|
|
16
|
+
const subscribers = React.useRef<
|
|
17
|
+
Map<string, { keyIndex: number; gesture: any; unsubscribe: any }>
|
|
18
|
+
>(new Map()).current;
|
|
19
|
+
|
|
20
|
+
// re-initiate callback on change
|
|
21
|
+
React.useEffect(() => {
|
|
22
|
+
for (let [, { keyIndex, gesture }] of subscribers.entries()) {
|
|
23
|
+
const [, , callback] = handlers[keyIndex];
|
|
24
|
+
gesture.applyCallback(callback);
|
|
25
|
+
}
|
|
26
|
+
}, [handlers]);
|
|
27
|
+
|
|
28
|
+
React.useEffect(() => {
|
|
29
|
+
handlers.forEach(([key, gesture, callback, config], keyIndex) => {
|
|
30
|
+
subscribers.set(key, {
|
|
31
|
+
keyIndex,
|
|
32
|
+
gesture,
|
|
33
|
+
unsubscribe: gesture.applyGesture({
|
|
34
|
+
targetElement: ref.current,
|
|
35
|
+
targetElements: elementRefs.current,
|
|
36
|
+
callback,
|
|
37
|
+
config,
|
|
38
|
+
}),
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return () => {
|
|
43
|
+
for (let [, { unsubscribe }] of subscribers.entries()) {
|
|
44
|
+
unsubscribe && unsubscribe();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
return (index?: number) => {
|
|
50
|
+
if (index === null || index === undefined) {
|
|
51
|
+
return { ref };
|
|
52
|
+
} else {
|
|
53
|
+
elementRefs.current[index] =
|
|
54
|
+
elementRefs.current[index] || React.createRef();
|
|
55
|
+
|
|
56
|
+
return { ref: elementRefs.current[index] };
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { ScrollEventType } from "../types";
|
|
4
|
+
import { ScrollGesture } from "../controllers";
|
|
5
|
+
import { useRecognizer } from "./useRecognizer";
|
|
6
|
+
|
|
7
|
+
export function useScroll(callback: (event: ScrollEventType) => void) {
|
|
8
|
+
const gesture = React.useRef(new ScrollGesture()).current;
|
|
9
|
+
|
|
10
|
+
return useRecognizer([["scroll", gesture, callback]]);
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { WheelEventType } from "../types";
|
|
4
|
+
import { WheelGesture } from "../controllers";
|
|
5
|
+
import { useRecognizer } from "./useRecognizer";
|
|
6
|
+
|
|
7
|
+
export function useWheel(callback: (event: WheelEventType) => void) {
|
|
8
|
+
const gesture = React.useRef(new WheelGesture()).current;
|
|
9
|
+
|
|
10
|
+
return useRecognizer([["wheel", gesture, callback]]);
|
|
11
|
+
}
|
|
File without changes
|
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
export type MeasurementValue = number | Array<number>;
|
|
2
|
-
|
|
3
|
-
export type MeasurementType = {
|
|
4
|
-
left: MeasurementValue;
|
|
5
|
-
top: MeasurementValue;
|
|
6
|
-
width: MeasurementValue;
|
|
7
|
-
height: MeasurementValue;
|
|
8
|
-
vLeft: MeasurementValue;
|
|
9
|
-
vTop: MeasurementValue;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export type WindowDimensionType = {
|
|
13
|
-
width: number;
|
|
14
|
-
height: number;
|
|
15
|
-
innerWidth: number;
|
|
16
|
-
innerHeight: number;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
1
|
type GenericEventType = {
|
|
20
2
|
velocityX: number;
|
|
21
3
|
velocityY: number;
|
|
@@ -23,23 +5,6 @@ type GenericEventType = {
|
|
|
23
5
|
directionY: number;
|
|
24
6
|
};
|
|
25
7
|
|
|
26
|
-
export type ScrollEventType = {
|
|
27
|
-
isScrolling: boolean;
|
|
28
|
-
scrollX: number;
|
|
29
|
-
scrollY: number;
|
|
30
|
-
} & GenericEventType;
|
|
31
|
-
|
|
32
|
-
export type WheelEventType = {
|
|
33
|
-
target: HTMLElement | undefined | null;
|
|
34
|
-
isWheeling: boolean;
|
|
35
|
-
movementX: number;
|
|
36
|
-
movementY: number;
|
|
37
|
-
offsetX: number;
|
|
38
|
-
offsetY: number;
|
|
39
|
-
deltaX: number;
|
|
40
|
-
deltaY: number;
|
|
41
|
-
} & GenericEventType;
|
|
42
|
-
|
|
43
8
|
export type DragEventType = {
|
|
44
9
|
args: Array<number | undefined>;
|
|
45
10
|
down: boolean;
|
|
@@ -53,14 +18,32 @@ export type DragEventType = {
|
|
|
53
18
|
} & GenericEventType;
|
|
54
19
|
|
|
55
20
|
export type MouseMoveEventType = {
|
|
21
|
+
args: Array<number | undefined>;
|
|
22
|
+
event: MouseEvent;
|
|
56
23
|
target: EventTarget | undefined | null;
|
|
57
24
|
isMoving: boolean;
|
|
58
25
|
mouseX: number;
|
|
59
26
|
mouseY: number;
|
|
60
27
|
} & GenericEventType;
|
|
61
28
|
|
|
29
|
+
export type ScrollEventType = {
|
|
30
|
+
isScrolling: boolean;
|
|
31
|
+
scrollX: number;
|
|
32
|
+
scrollY: number;
|
|
33
|
+
} & GenericEventType;
|
|
34
|
+
|
|
35
|
+
export type WheelEventType = {
|
|
36
|
+
target: HTMLElement | undefined | null;
|
|
37
|
+
isWheeling: boolean;
|
|
38
|
+
movementX: number;
|
|
39
|
+
movementY: number;
|
|
40
|
+
offsetX: number;
|
|
41
|
+
offsetY: number;
|
|
42
|
+
deltaX: number;
|
|
43
|
+
deltaY: number;
|
|
44
|
+
} & GenericEventType;
|
|
45
|
+
|
|
62
46
|
export type UseDragConfig = {
|
|
63
47
|
initial?: () => { movementX: number; movementY: number };
|
|
64
48
|
};
|
|
65
|
-
|
|
66
49
|
export type Vector2 = { x: number; y: number };
|
package/src/hooks/index.ts
CHANGED
package/src/hooks/useMeasure.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
type MeasurementValue = number | Array<number>;
|
|
4
|
+
|
|
5
|
+
type MeasurementType = {
|
|
6
|
+
left: MeasurementValue;
|
|
7
|
+
top: MeasurementValue;
|
|
8
|
+
width: MeasurementValue;
|
|
9
|
+
height: MeasurementValue;
|
|
10
|
+
vLeft: MeasurementValue;
|
|
11
|
+
vTop: MeasurementValue;
|
|
12
|
+
};
|
|
3
13
|
|
|
4
14
|
export function useMeasure(
|
|
5
15
|
callback: (event: MeasurementType) => void,
|