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
package/dist/index.js
ADDED
|
@@ -0,0 +1,1189 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var reMotion = require('@raidipesh78/re-motion');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* interpolate function maps input range to output range
|
|
8
|
+
* @param value - number | TransitionValue
|
|
9
|
+
* @param inputRange - Array<number>
|
|
10
|
+
* @param outputRange - Array<string | number>
|
|
11
|
+
* @param extrapolateConfig - "clamp" | "identity" | "extend"
|
|
12
|
+
* @returns - number | TransitionValue
|
|
13
|
+
*/
|
|
14
|
+
function interpolate(value, inputRange, outputRange, extrapolateConfig) {
|
|
15
|
+
if (typeof value === "object" && reMotion.isSubscriber(value)) {
|
|
16
|
+
return reMotion.interpolateTransitionValue(value, inputRange, outputRange, extrapolateConfig);
|
|
17
|
+
}
|
|
18
|
+
else if (typeof value === "number") {
|
|
19
|
+
return reMotion.interpolateNumbers(value, inputRange, outputRange, extrapolateConfig);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
throw new Error("Error! " + typeof value + " cannot be interpolated");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* bInterpolate functions maps input range [0, 1] to given [minOutput, maxOutput]
|
|
27
|
+
* sorthand function to interpolate input range [0, 1]
|
|
28
|
+
* @param value - number | TransitionValue
|
|
29
|
+
* @param minOutput - number | string
|
|
30
|
+
* @param maxOutput - number | string
|
|
31
|
+
* @param extrapolateConfig - "clamp" | "identity" | "extend"
|
|
32
|
+
* @returns - number | TransitionValue
|
|
33
|
+
*/
|
|
34
|
+
function bInterpolate(value, minOutput, maxOutput, extrapolateConfig) {
|
|
35
|
+
return interpolate(value, [0, 1], [minOutput, maxOutput], extrapolateConfig);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/*! *****************************************************************************
|
|
39
|
+
Copyright (c) Microsoft Corporation.
|
|
40
|
+
|
|
41
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
42
|
+
purpose with or without fee is hereby granted.
|
|
43
|
+
|
|
44
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
45
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
46
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
47
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
48
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
49
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
50
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
51
|
+
***************************************************************************** */
|
|
52
|
+
/* global Reflect, Promise */
|
|
53
|
+
|
|
54
|
+
var extendStatics = function(d, b) {
|
|
55
|
+
extendStatics = Object.setPrototypeOf ||
|
|
56
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
57
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
58
|
+
return extendStatics(d, b);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function __extends(d, b) {
|
|
62
|
+
extendStatics(d, b);
|
|
63
|
+
function __() { this.constructor = d; }
|
|
64
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
var __assign = function() {
|
|
68
|
+
__assign = Object.assign || function __assign(t) {
|
|
69
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
70
|
+
s = arguments[i];
|
|
71
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
72
|
+
}
|
|
73
|
+
return t;
|
|
74
|
+
};
|
|
75
|
+
return __assign.apply(this, arguments);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
function __values(o) {
|
|
79
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
80
|
+
if (m) return m.call(o);
|
|
81
|
+
if (o && typeof o.length === "number") return {
|
|
82
|
+
next: function () {
|
|
83
|
+
if (o && i >= o.length) o = void 0;
|
|
84
|
+
return { value: o && o[i++], done: !o };
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function __read(o, n) {
|
|
91
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
92
|
+
if (!m) return o;
|
|
93
|
+
var i = m.call(o), r, ar = [], e;
|
|
94
|
+
try {
|
|
95
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
96
|
+
}
|
|
97
|
+
catch (error) { e = { error: error }; }
|
|
98
|
+
finally {
|
|
99
|
+
try {
|
|
100
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
101
|
+
}
|
|
102
|
+
finally { if (e) throw e.error; }
|
|
103
|
+
}
|
|
104
|
+
return ar;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function __spread() {
|
|
108
|
+
for (var ar = [], i = 0; i < arguments.length; i++)
|
|
109
|
+
ar = ar.concat(__read(arguments[i]));
|
|
110
|
+
return ar;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
var getInitialConfig = function (animationType) {
|
|
114
|
+
switch (animationType) {
|
|
115
|
+
case "elastic":
|
|
116
|
+
return { mass: 1, friction: 18, tension: 250 };
|
|
117
|
+
case "stiff":
|
|
118
|
+
return { mass: 1, friction: 18, tension: 350 };
|
|
119
|
+
case "wooble":
|
|
120
|
+
return { mass: 1, friction: 8, tension: 250 };
|
|
121
|
+
case "bounce":
|
|
122
|
+
return { duration: 500, easing: reMotion.Easing.bounce };
|
|
123
|
+
case "ease":
|
|
124
|
+
default:
|
|
125
|
+
return { mass: 1, friction: 26, tension: 170 };
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* getValue checks for type of initialValue and throws error
|
|
131
|
+
* for type other than AnimatedValueType
|
|
132
|
+
*/
|
|
133
|
+
var getValue = function (value) {
|
|
134
|
+
if (typeof value === "number" || typeof value === "string") {
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
throw new Error("Invalid Value! Animated value only accepts string or number.");
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* useAnimatedValue for animated transitions
|
|
143
|
+
*/
|
|
144
|
+
function useAnimatedValue(initialValue, config) {
|
|
145
|
+
var _a;
|
|
146
|
+
var _isInitial = React.useRef(true);
|
|
147
|
+
var _initialValue = getValue(initialValue);
|
|
148
|
+
var animationType = (_a = config === null || config === void 0 ? void 0 : config.animationType) !== null && _a !== void 0 ? _a : "ease"; // Defines default animation
|
|
149
|
+
var onAnimationEnd = config === null || config === void 0 ? void 0 : config.onAnimationEnd;
|
|
150
|
+
var listener = config === null || config === void 0 ? void 0 : config.listener;
|
|
151
|
+
var _b = __read(reMotion.useTransition(_initialValue, __assign(__assign(__assign({}, getInitialConfig(animationType)), config), { onRest: function (result) {
|
|
152
|
+
if (result.finished) {
|
|
153
|
+
onAnimationEnd && onAnimationEnd(result.value);
|
|
154
|
+
}
|
|
155
|
+
}, onChange: function (value) {
|
|
156
|
+
listener && listener(value);
|
|
157
|
+
} })), 2), animation = _b[0], setAnimation = _b[1];
|
|
158
|
+
// doesn't fire on initial render
|
|
159
|
+
React.useEffect(function () {
|
|
160
|
+
if (!_isInitial.current) {
|
|
161
|
+
setAnimation({ toValue: _initialValue });
|
|
162
|
+
}
|
|
163
|
+
_isInitial.current = false;
|
|
164
|
+
}, [_initialValue]);
|
|
165
|
+
var targetObject = {
|
|
166
|
+
value: animation,
|
|
167
|
+
currentValue: animation.get(),
|
|
168
|
+
};
|
|
169
|
+
return new Proxy(targetObject, {
|
|
170
|
+
set: function (_, key, value) {
|
|
171
|
+
if (key === "value") {
|
|
172
|
+
if (typeof value === "number" || typeof value === "string") {
|
|
173
|
+
setAnimation({ toValue: value });
|
|
174
|
+
}
|
|
175
|
+
else if (typeof value === "object") {
|
|
176
|
+
setAnimation({ toValue: value.toValue, immediate: value === null || value === void 0 ? void 0 : value.immediate });
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
throw new Error("You cannot set any other property to animation node.");
|
|
181
|
+
},
|
|
182
|
+
get: function (_, key) {
|
|
183
|
+
if (key === "value") {
|
|
184
|
+
return animation;
|
|
185
|
+
}
|
|
186
|
+
if (key === "currentValue") {
|
|
187
|
+
return animation.get();
|
|
188
|
+
}
|
|
189
|
+
throw new Error("You cannot access any other property from animation node.");
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* useMountedValue handles mounting and unmounting of a component
|
|
196
|
+
* @param state - boolean
|
|
197
|
+
* @param config - useTransitionConfig
|
|
198
|
+
* @returns mountedValueFunction with a callback with argument ( animationNode, mounted )
|
|
199
|
+
*/
|
|
200
|
+
function useMountedValue(state, config) {
|
|
201
|
+
var _a, _b, _c, _d, _e, _f;
|
|
202
|
+
var _g = __read(React.useState(true), 2), initial = _g[0], setInitial = _g[1];
|
|
203
|
+
var _h = __read(React.useState(state), 2), mounted = _h[0], setMounted = _h[1];
|
|
204
|
+
var _j = React.useRef(config).current, from = _j.from, enter = _j.enter, exit = _j.exit, _config = _j.config;
|
|
205
|
+
var _k = __read(reMotion.useTransition(from, _config), 2), animation = _k[0], setAnimation = _k[1];
|
|
206
|
+
var enterDuration = (_b = (_a = config.config) === null || _a === void 0 ? void 0 : _a.enterDuration) !== null && _b !== void 0 ? _b : (_c = config.config) === null || _c === void 0 ? void 0 : _c.duration;
|
|
207
|
+
var exitDuration = (_e = (_d = config.config) === null || _d === void 0 ? void 0 : _d.exitDuration) !== null && _e !== void 0 ? _e : (_f = config.config) === null || _f === void 0 ? void 0 : _f.exitDuration;
|
|
208
|
+
React.useEffect(function () {
|
|
209
|
+
if (state) {
|
|
210
|
+
setInitial(true);
|
|
211
|
+
setMounted(true);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
setInitial(false);
|
|
215
|
+
setAnimation({
|
|
216
|
+
toValue: exit,
|
|
217
|
+
duration: exitDuration,
|
|
218
|
+
}, function (_a) {
|
|
219
|
+
var finished = _a.finished;
|
|
220
|
+
if (finished) {
|
|
221
|
+
setMounted(false);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}, [state]);
|
|
226
|
+
React.useEffect(function () {
|
|
227
|
+
if (mounted && initial) {
|
|
228
|
+
setAnimation({
|
|
229
|
+
toValue: enter,
|
|
230
|
+
duration: enterDuration,
|
|
231
|
+
}, function () {
|
|
232
|
+
return;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}, [mounted, initial]);
|
|
236
|
+
return function (callback) {
|
|
237
|
+
return callback({ value: animation }, mounted);
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Make any component animatable
|
|
243
|
+
*/
|
|
244
|
+
function makeAnimatedComponent(WrappedComponent) {
|
|
245
|
+
return reMotion.makeAnimatedComponent(WrappedComponent);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* AnimatedBlock : Animated Div
|
|
249
|
+
*/
|
|
250
|
+
var AnimatedBlock = reMotion.makeAnimatedComponent("div");
|
|
251
|
+
/**
|
|
252
|
+
* AnimatedInline : Animated Span
|
|
253
|
+
*/
|
|
254
|
+
var AnimatedInline = reMotion.makeAnimatedComponent("span");
|
|
255
|
+
/**
|
|
256
|
+
* AnimatedImage : Animated Image
|
|
257
|
+
*/
|
|
258
|
+
var AnimatedImage = reMotion.makeAnimatedComponent("img");
|
|
259
|
+
/**
|
|
260
|
+
* ScrollableBlock
|
|
261
|
+
* Used to animate element when enter into viewport
|
|
262
|
+
* Render props pattern with children accepts animation node
|
|
263
|
+
* animated value goes from 0 to 1 when appear on viewport & vice versa.
|
|
264
|
+
*/
|
|
265
|
+
var ScrollableBlock = function (props) {
|
|
266
|
+
var children = props.children, _a = props.direction, direction = _a === void 0 ? "single" : _a, animationConfig = props.animationConfig, _b = props.threshold, threshold = _b === void 0 ? 0.2 : _b;
|
|
267
|
+
var scrollableBlockRef = React.useRef(null);
|
|
268
|
+
var animation = useAnimatedValue(0, animationConfig); // 0: not intersecting | 1: intersecting
|
|
269
|
+
React.useEffect(function () {
|
|
270
|
+
var _scrollableBlock = scrollableBlockRef.current;
|
|
271
|
+
var observer = new IntersectionObserver(function (_a) {
|
|
272
|
+
var _b = __read(_a, 1), entry = _b[0];
|
|
273
|
+
var isIntersecting = entry.isIntersecting;
|
|
274
|
+
if (isIntersecting) {
|
|
275
|
+
animation.value = 1;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
if (direction === "both")
|
|
279
|
+
animation.value = 0;
|
|
280
|
+
}
|
|
281
|
+
}, {
|
|
282
|
+
root: null,
|
|
283
|
+
threshold: threshold,
|
|
284
|
+
});
|
|
285
|
+
if (_scrollableBlock) {
|
|
286
|
+
observer.observe(_scrollableBlock);
|
|
287
|
+
}
|
|
288
|
+
return function () {
|
|
289
|
+
if (_scrollableBlock) {
|
|
290
|
+
observer.unobserve(_scrollableBlock);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}, []);
|
|
294
|
+
return React.createElement("div", { ref: scrollableBlockRef }, children && children(animation));
|
|
295
|
+
};
|
|
296
|
+
/**
|
|
297
|
+
* MountedBlock handles mounting and unmounting of a component
|
|
298
|
+
* @props - state: boolean, config: InnerUseMountedValueConfig
|
|
299
|
+
* @children - (animation: { value: TransitionValue }) => React.ReactNode
|
|
300
|
+
*/
|
|
301
|
+
var MountedBlock = function (_a) {
|
|
302
|
+
var state = _a.state, children = _a.children, config = _a.config;
|
|
303
|
+
var open = useMountedValue(state, { from: 0, enter: 1, exit: 0, config: config });
|
|
304
|
+
return React.createElement(React.Fragment, null, open(function (animation, mounted) { return mounted && children(animation); }));
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Attach single document / window event / HTMLElement
|
|
309
|
+
*/
|
|
310
|
+
function attachEvent(domTargets, event, callback, capture) {
|
|
311
|
+
if (capture === void 0) { capture = false; }
|
|
312
|
+
domTargets.forEach(function (target) {
|
|
313
|
+
target.addEventListener(event, callback, capture);
|
|
314
|
+
});
|
|
315
|
+
return function () {
|
|
316
|
+
domTargets.forEach(function (target) {
|
|
317
|
+
target.removeEventListener(event, callback, capture);
|
|
318
|
+
});
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Attach multiple document / window event / HTMLElement
|
|
323
|
+
*/
|
|
324
|
+
function attachEvents(domTargets, events) {
|
|
325
|
+
var subscribers = new Map();
|
|
326
|
+
events.forEach(function (_a) {
|
|
327
|
+
var _b = __read(_a, 3), event = _b[0], callback = _b[1], _c = _b[2], capture = _c === void 0 ? false : _c;
|
|
328
|
+
subscribers.set(event, attachEvent(domTargets, event, callback, capture));
|
|
329
|
+
});
|
|
330
|
+
return function (eventKeys) {
|
|
331
|
+
var e_1, _a;
|
|
332
|
+
try {
|
|
333
|
+
for (var _b = __values(subscribers.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
334
|
+
var _d = __read(_c.value, 2), eventKey = _d[0], subscriber = _d[1];
|
|
335
|
+
if (!eventKeys) {
|
|
336
|
+
subscriber();
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (eventKeys.indexOf(eventKey) !== -1) {
|
|
340
|
+
subscriber();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
345
|
+
finally {
|
|
346
|
+
try {
|
|
347
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
348
|
+
}
|
|
349
|
+
finally { if (e_1) throw e_1.error; }
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* bin(booleanValue)
|
|
356
|
+
* returns 1 if booleanValue == true and 0 if booleanValue == false
|
|
357
|
+
*/
|
|
358
|
+
function bin(bool) {
|
|
359
|
+
return bool ? 1 : 0;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* mix(progress, a, b)
|
|
363
|
+
* linear interpolation between a and b
|
|
364
|
+
*/
|
|
365
|
+
function mix(perc, val1, val2) {
|
|
366
|
+
return val1 * (1 - perc) + val2 * perc;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* clamp(value, min, max)
|
|
370
|
+
* clamps value for min and max bounds
|
|
371
|
+
*/
|
|
372
|
+
function clamp(value, lowerbound, upperbound) {
|
|
373
|
+
return Math.min(Math.max(value, lowerbound), upperbound);
|
|
374
|
+
}
|
|
375
|
+
function rubber2(distanceFromEdge, constant) {
|
|
376
|
+
return Math.pow(distanceFromEdge, constant * 5);
|
|
377
|
+
}
|
|
378
|
+
function rubber(distanceFromEdge, dimension, constant) {
|
|
379
|
+
if (dimension === 0 || Math.abs(dimension) === Infinity)
|
|
380
|
+
return rubber2(distanceFromEdge, constant);
|
|
381
|
+
return ((distanceFromEdge * dimension * constant) /
|
|
382
|
+
(dimension + constant * distanceFromEdge));
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* rubberClamp(value, min, max, constant?)
|
|
386
|
+
* constant is optional : default 0.15
|
|
387
|
+
* clamps the value for min and max value and
|
|
388
|
+
* extends beyond min and max values with constant
|
|
389
|
+
* factor to create elastic rubber band effect
|
|
390
|
+
*/
|
|
391
|
+
function rubberClamp(value, lowerbound, upperbound, constant) {
|
|
392
|
+
if (constant === void 0) { constant = 0.15; }
|
|
393
|
+
if (constant === 0)
|
|
394
|
+
return clamp(value, lowerbound, upperbound);
|
|
395
|
+
if (value < lowerbound) {
|
|
396
|
+
return (-rubber(lowerbound - value, upperbound - lowerbound, constant) +
|
|
397
|
+
lowerbound);
|
|
398
|
+
}
|
|
399
|
+
if (value > upperbound) {
|
|
400
|
+
return (+rubber(value - upperbound, upperbound - lowerbound, constant) +
|
|
401
|
+
upperbound);
|
|
402
|
+
}
|
|
403
|
+
return value;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* snapTo(value, velocity, snapPoints[])
|
|
407
|
+
* Calculates the final snapPoint according to given current value,
|
|
408
|
+
* velocity and snapPoints array
|
|
409
|
+
*/
|
|
410
|
+
function snapTo(value, velocity, snapPoints) {
|
|
411
|
+
var finalValue = value + velocity * 0.2;
|
|
412
|
+
var getDiff = function (point) { return Math.abs(point - finalValue); };
|
|
413
|
+
var deltas = snapPoints.map(getDiff);
|
|
414
|
+
var minDelta = Math.min.apply(Math, __spread(deltas));
|
|
415
|
+
return snapPoints.reduce(function (acc, point) {
|
|
416
|
+
if (getDiff(point) === minDelta) {
|
|
417
|
+
return point;
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
return acc;
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* move(array, moveIndex, toIndex)
|
|
426
|
+
* move array item from moveIndex to toIndex without array modification
|
|
427
|
+
*/
|
|
428
|
+
function move(array, moveIndex, toIndex) {
|
|
429
|
+
var item = array[moveIndex];
|
|
430
|
+
var length = array.length;
|
|
431
|
+
var diff = moveIndex - toIndex;
|
|
432
|
+
if (diff > 0) {
|
|
433
|
+
return __spread(array.slice(0, toIndex), [
|
|
434
|
+
item
|
|
435
|
+
], array.slice(toIndex, moveIndex), array.slice(moveIndex + 1, length));
|
|
436
|
+
}
|
|
437
|
+
else if (diff < 0) {
|
|
438
|
+
var targetIndex = toIndex + 1;
|
|
439
|
+
return __spread(array.slice(0, moveIndex), array.slice(moveIndex + 1, targetIndex), [
|
|
440
|
+
item
|
|
441
|
+
], array.slice(targetIndex, length));
|
|
442
|
+
}
|
|
443
|
+
return array;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
var withDefault = function (x, y) {
|
|
447
|
+
return { x: x, y: y };
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
var Gesture = /** @class */ (function () {
|
|
451
|
+
function Gesture() {
|
|
452
|
+
this.lastTimeStamp = Date.now();
|
|
453
|
+
this.isActive = false;
|
|
454
|
+
this.targetElements = []; // represents the bounded elements
|
|
455
|
+
}
|
|
456
|
+
// it must be overridden by other child classes
|
|
457
|
+
Gesture.prototype._initEvents = function () { };
|
|
458
|
+
// cancel events
|
|
459
|
+
// we only canceled down and move events because mouse up
|
|
460
|
+
// will not be triggered
|
|
461
|
+
Gesture.prototype._cancelEvents = function () {
|
|
462
|
+
if (this._subscribe) {
|
|
463
|
+
this._subscribe();
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
// re-apply new callback
|
|
467
|
+
Gesture.prototype.applyCallback = function (callback) {
|
|
468
|
+
this.callback = callback;
|
|
469
|
+
};
|
|
470
|
+
// apply gesture
|
|
471
|
+
Gesture.prototype.applyGesture = function (_a) {
|
|
472
|
+
var _this = this;
|
|
473
|
+
var targetElement = _a.targetElement, targetElements = _a.targetElements, callback = _a.callback, config = _a.config;
|
|
474
|
+
this.targetElement = targetElement;
|
|
475
|
+
this.targetElements = targetElements.map(function (element) { return element.current; });
|
|
476
|
+
this.callback = callback;
|
|
477
|
+
this.config = config;
|
|
478
|
+
// initialize events
|
|
479
|
+
this._initEvents();
|
|
480
|
+
// unbind
|
|
481
|
+
return function () { return _this._subscribe && _this._subscribe(); };
|
|
482
|
+
};
|
|
483
|
+
Gesture._VELOCITY_LIMIT = 20;
|
|
484
|
+
return Gesture;
|
|
485
|
+
}());
|
|
486
|
+
|
|
487
|
+
var DragGesture = /** @class */ (function (_super) {
|
|
488
|
+
__extends(DragGesture, _super);
|
|
489
|
+
function DragGesture() {
|
|
490
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
491
|
+
_this.movementStart = withDefault(0, 0);
|
|
492
|
+
_this.initialMovement = withDefault(0, 0);
|
|
493
|
+
_this.movement = withDefault(0, 0);
|
|
494
|
+
_this.previousMovement = withDefault(0, 0);
|
|
495
|
+
_this.translation = withDefault(0, 0);
|
|
496
|
+
_this.offset = withDefault(0, 0);
|
|
497
|
+
_this.velocity = withDefault(0, 0);
|
|
498
|
+
return _this;
|
|
499
|
+
}
|
|
500
|
+
// @override
|
|
501
|
+
// initialize the events
|
|
502
|
+
DragGesture.prototype._initEvents = function () {
|
|
503
|
+
if (this.targetElement || this.targetElements.length > 0) {
|
|
504
|
+
this._subscribe = attachEvents([window], [
|
|
505
|
+
["mousedown", this.pointerDown.bind(this)],
|
|
506
|
+
["mousemove", this.pointerMove.bind(this)],
|
|
507
|
+
["mouseup", this.pointerUp.bind(this)],
|
|
508
|
+
["touchstart", this.pointerDown.bind(this), { passive: false }],
|
|
509
|
+
["touchmove", this.pointerMove.bind(this), { passive: false }],
|
|
510
|
+
["touchend", this.pointerUp.bind(this)],
|
|
511
|
+
]);
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
// @override - cancel events
|
|
515
|
+
// we only canceled down and move events because mouse up
|
|
516
|
+
// will not be triggered
|
|
517
|
+
DragGesture.prototype._cancelEvents = function () {
|
|
518
|
+
if (this._subscribe) {
|
|
519
|
+
this._subscribe(["mousedown", "mousemove", "touchstart", "touchmove"]);
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
DragGesture.prototype._handleCallback = function () {
|
|
523
|
+
var _this = this;
|
|
524
|
+
if (this.callback) {
|
|
525
|
+
this.callback({
|
|
526
|
+
args: [this.currentIndex],
|
|
527
|
+
down: this.isActive,
|
|
528
|
+
movementX: this.movement.x,
|
|
529
|
+
movementY: this.movement.y,
|
|
530
|
+
offsetX: this.translation.x,
|
|
531
|
+
offsetY: this.translation.y,
|
|
532
|
+
velocityX: this.velocity.x,
|
|
533
|
+
velocityY: this.velocity.y,
|
|
534
|
+
distanceX: Math.abs(this.movement.x),
|
|
535
|
+
distanceY: Math.abs(this.movement.y),
|
|
536
|
+
directionX: Math.sign(this.movement.x),
|
|
537
|
+
directionY: Math.sign(this.movement.y),
|
|
538
|
+
cancel: function () {
|
|
539
|
+
_this._cancelEvents();
|
|
540
|
+
},
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
DragGesture.prototype.pointerDown = function (e) {
|
|
545
|
+
var _a;
|
|
546
|
+
if (e.type === "touchstart") {
|
|
547
|
+
this.movementStart = {
|
|
548
|
+
x: e.touches[0].clientX,
|
|
549
|
+
y: e.touches[0].clientY,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
else {
|
|
553
|
+
this.movementStart = { x: e.clientX, y: e.clientY };
|
|
554
|
+
}
|
|
555
|
+
this.movement = { x: 0, y: 0 };
|
|
556
|
+
this.offset = { x: this.translation.x, y: this.translation.y };
|
|
557
|
+
this.previousMovement = { x: 0, y: 0 };
|
|
558
|
+
this.velocity = { x: 0, y: 0 };
|
|
559
|
+
// find current selected element
|
|
560
|
+
var currElem = this.targetElements.find(function (elem) { return elem === e.target; });
|
|
561
|
+
if (e.target === this.targetElement || currElem) {
|
|
562
|
+
this.isActive = true;
|
|
563
|
+
e.preventDefault();
|
|
564
|
+
// set args
|
|
565
|
+
if (currElem) {
|
|
566
|
+
this.currentIndex = this.targetElements.indexOf(currElem);
|
|
567
|
+
}
|
|
568
|
+
// if initial function is defined then call it to get initial movementX and movementY
|
|
569
|
+
// if only select to bounded draggable element
|
|
570
|
+
var initial = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.initial) && this.config.initial();
|
|
571
|
+
var initialMovementX = initial === null || initial === void 0 ? void 0 : initial.movementX;
|
|
572
|
+
var initialMovementY = initial === null || initial === void 0 ? void 0 : initial.movementY;
|
|
573
|
+
this.initialMovement = {
|
|
574
|
+
x: initialMovementX !== null && initialMovementX !== void 0 ? initialMovementX : 0,
|
|
575
|
+
y: initialMovementY !== null && initialMovementY !== void 0 ? initialMovementY : 0,
|
|
576
|
+
};
|
|
577
|
+
this.movement = {
|
|
578
|
+
x: this.initialMovement.x,
|
|
579
|
+
y: this.initialMovement.y,
|
|
580
|
+
};
|
|
581
|
+
this.previousMovement = {
|
|
582
|
+
x: this.initialMovement.x,
|
|
583
|
+
y: this.initialMovement.y,
|
|
584
|
+
};
|
|
585
|
+
this._handleCallback();
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
DragGesture.prototype.pointerMove = function (e) {
|
|
589
|
+
if (this.isActive) {
|
|
590
|
+
e.preventDefault();
|
|
591
|
+
var now = Date.now();
|
|
592
|
+
var deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
593
|
+
this.lastTimeStamp = now;
|
|
594
|
+
var t = deltaTime / 1000;
|
|
595
|
+
if (e.type === "touchmove") {
|
|
596
|
+
this.movement = {
|
|
597
|
+
x: this.initialMovement.x +
|
|
598
|
+
(e.touches[0].clientX - this.movementStart.x),
|
|
599
|
+
y: this.initialMovement.y +
|
|
600
|
+
(e.touches[0].clientY - this.movementStart.y),
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
this.movement = {
|
|
605
|
+
x: this.initialMovement.x + (e.clientX - this.movementStart.x),
|
|
606
|
+
y: this.initialMovement.y + (e.clientY - this.movementStart.y),
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
this.translation = {
|
|
610
|
+
x: this.offset.x + this.movement.x,
|
|
611
|
+
y: this.offset.y + this.movement.y,
|
|
612
|
+
};
|
|
613
|
+
this.velocity = {
|
|
614
|
+
x: clamp((this.movement.x - this.previousMovement.x) / t / 1000, -1 * Gesture._VELOCITY_LIMIT, Gesture._VELOCITY_LIMIT),
|
|
615
|
+
y: clamp((this.movement.y - this.previousMovement.y) / t / 1000, -1 * Gesture._VELOCITY_LIMIT, Gesture._VELOCITY_LIMIT),
|
|
616
|
+
};
|
|
617
|
+
this.previousMovement = {
|
|
618
|
+
x: this.movement.x,
|
|
619
|
+
y: this.movement.y,
|
|
620
|
+
};
|
|
621
|
+
this._handleCallback();
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
DragGesture.prototype.pointerUp = function () {
|
|
625
|
+
if (this.isActive) {
|
|
626
|
+
this.isActive = false;
|
|
627
|
+
this._handleCallback();
|
|
628
|
+
this._initEvents();
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
return DragGesture;
|
|
632
|
+
}(Gesture));
|
|
633
|
+
|
|
634
|
+
var MouseMoveGesture = /** @class */ (function (_super) {
|
|
635
|
+
__extends(MouseMoveGesture, _super);
|
|
636
|
+
function MouseMoveGesture() {
|
|
637
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
638
|
+
_this.movement = withDefault(0, 0);
|
|
639
|
+
_this.previousMovement = withDefault(0, 0);
|
|
640
|
+
_this.velocity = withDefault(0, 0);
|
|
641
|
+
_this.direction = withDefault(0, 0);
|
|
642
|
+
return _this;
|
|
643
|
+
}
|
|
644
|
+
// @override
|
|
645
|
+
// initialize the events
|
|
646
|
+
MouseMoveGesture.prototype._initEvents = function () {
|
|
647
|
+
if (this.targetElement) {
|
|
648
|
+
this._subscribe = attachEvents([this.targetElement], [["mousemove", this.onMouseMove.bind(this)]]);
|
|
649
|
+
}
|
|
650
|
+
else if (this.targetElements.length > 0) {
|
|
651
|
+
this._subscribe = attachEvents(this.targetElements, [
|
|
652
|
+
["mousemove", this.onMouseMove.bind(this)],
|
|
653
|
+
]);
|
|
654
|
+
}
|
|
655
|
+
else {
|
|
656
|
+
this._subscribe = attachEvents([window], [["mousemove", this.onMouseMove.bind(this)]]);
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
MouseMoveGesture.prototype._handleCallback = function () {
|
|
660
|
+
var _a;
|
|
661
|
+
if (this.callback) {
|
|
662
|
+
this.callback({
|
|
663
|
+
args: [this.currentIndex],
|
|
664
|
+
event: this.event,
|
|
665
|
+
isMoving: this.isActive,
|
|
666
|
+
target: (_a = this.event) === null || _a === void 0 ? void 0 : _a.target,
|
|
667
|
+
mouseX: this.movement.x,
|
|
668
|
+
mouseY: this.movement.y,
|
|
669
|
+
velocityX: this.velocity.x,
|
|
670
|
+
velocityY: this.velocity.y,
|
|
671
|
+
directionX: this.direction.x,
|
|
672
|
+
directionY: this.direction.y,
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
MouseMoveGesture.prototype.onMouseMove = function (e) {
|
|
677
|
+
var _this = this;
|
|
678
|
+
// find current selected element
|
|
679
|
+
var currElem = this.targetElements.find(function (elem) { return elem === e.target; });
|
|
680
|
+
// set args
|
|
681
|
+
if (currElem) {
|
|
682
|
+
this.currentIndex = this.targetElements.indexOf(currElem);
|
|
683
|
+
}
|
|
684
|
+
this.event = e;
|
|
685
|
+
var now = Date.now();
|
|
686
|
+
var deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
687
|
+
this.lastTimeStamp = now;
|
|
688
|
+
var t = deltaTime / 1000; // seconds
|
|
689
|
+
var x = e.clientX;
|
|
690
|
+
var y = e.clientY;
|
|
691
|
+
this.movement = { x: x, y: y };
|
|
692
|
+
if (this.isActiveID !== -1) {
|
|
693
|
+
this.isActive = true;
|
|
694
|
+
clearTimeout(this.isActiveID);
|
|
695
|
+
}
|
|
696
|
+
this.isActiveID = setTimeout(function () {
|
|
697
|
+
_this.isActive = false;
|
|
698
|
+
_this.direction = { x: 0, y: 0 };
|
|
699
|
+
_this.velocity = { x: 0, y: 0 };
|
|
700
|
+
_this._handleCallback();
|
|
701
|
+
}, 250); // Debounce 250 milliseconds
|
|
702
|
+
var diffX = this.movement.x - this.previousMovement.x;
|
|
703
|
+
var diffY = this.movement.y - this.previousMovement.y;
|
|
704
|
+
this.direction = {
|
|
705
|
+
x: Math.sign(diffX),
|
|
706
|
+
y: Math.sign(diffY),
|
|
707
|
+
};
|
|
708
|
+
this.velocity = {
|
|
709
|
+
x: clamp(diffX / t / 1000, -1 * Gesture._VELOCITY_LIMIT, Gesture._VELOCITY_LIMIT),
|
|
710
|
+
y: clamp(diffY / t / 1000, -1 * Gesture._VELOCITY_LIMIT, Gesture._VELOCITY_LIMIT),
|
|
711
|
+
};
|
|
712
|
+
this.previousMovement = { x: this.movement.x, y: this.movement.y };
|
|
713
|
+
this._handleCallback();
|
|
714
|
+
};
|
|
715
|
+
return MouseMoveGesture;
|
|
716
|
+
}(Gesture));
|
|
717
|
+
|
|
718
|
+
var ScrollGesture = /** @class */ (function (_super) {
|
|
719
|
+
__extends(ScrollGesture, _super);
|
|
720
|
+
function ScrollGesture() {
|
|
721
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
722
|
+
_this.movement = withDefault(0, 0);
|
|
723
|
+
_this.previousMovement = withDefault(0, 0);
|
|
724
|
+
_this.direction = withDefault(0, 0);
|
|
725
|
+
_this.velocity = withDefault(0, 0);
|
|
726
|
+
return _this;
|
|
727
|
+
}
|
|
728
|
+
// @override
|
|
729
|
+
// initialize the events
|
|
730
|
+
ScrollGesture.prototype._initEvents = function () {
|
|
731
|
+
if (this.targetElement) {
|
|
732
|
+
this._subscribe = attachEvents([this.targetElement], [["scroll", this.scrollElementListener.bind(this)]]);
|
|
733
|
+
}
|
|
734
|
+
else {
|
|
735
|
+
this._subscribe = attachEvents([window], [["scroll", this.scrollListener.bind(this)]]);
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
ScrollGesture.prototype._handleCallback = function () {
|
|
739
|
+
if (this.callback) {
|
|
740
|
+
this.callback({
|
|
741
|
+
isScrolling: this.isActive,
|
|
742
|
+
scrollX: this.movement.x,
|
|
743
|
+
scrollY: this.movement.y,
|
|
744
|
+
velocityX: this.velocity.x,
|
|
745
|
+
velocityY: this.velocity.y,
|
|
746
|
+
directionX: this.direction.x,
|
|
747
|
+
directionY: this.direction.y,
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
};
|
|
751
|
+
ScrollGesture.prototype.onScroll = function (_a) {
|
|
752
|
+
var _this = this;
|
|
753
|
+
var x = _a.x, y = _a.y;
|
|
754
|
+
var now = Date.now();
|
|
755
|
+
var deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
756
|
+
this.lastTimeStamp = now;
|
|
757
|
+
var t = deltaTime / 1000; // seconds
|
|
758
|
+
this.movement = { x: x, y: y };
|
|
759
|
+
// Clear if scrolling
|
|
760
|
+
if (this.isActiveID !== -1) {
|
|
761
|
+
this.isActive = true;
|
|
762
|
+
clearTimeout(this.isActiveID);
|
|
763
|
+
}
|
|
764
|
+
this.isActiveID = setTimeout(function () {
|
|
765
|
+
_this.isActive = false;
|
|
766
|
+
_this.direction = { x: 0, y: 0 };
|
|
767
|
+
// Reset Velocity
|
|
768
|
+
_this.velocity = { x: 0, y: 0 };
|
|
769
|
+
_this._handleCallback(); // Debounce 250milliseconds
|
|
770
|
+
}, 250);
|
|
771
|
+
var diffX = this.movement.x - this.previousMovement.x;
|
|
772
|
+
var diffY = this.movement.y - this.previousMovement.y;
|
|
773
|
+
this.direction = {
|
|
774
|
+
x: Math.sign(diffX),
|
|
775
|
+
y: Math.sign(diffY),
|
|
776
|
+
};
|
|
777
|
+
this.velocity = {
|
|
778
|
+
x: clamp(diffX / t / 1000, -1 * Gesture._VELOCITY_LIMIT, Gesture._VELOCITY_LIMIT),
|
|
779
|
+
y: clamp(diffY / t / 1000, -1 * Gesture._VELOCITY_LIMIT, Gesture._VELOCITY_LIMIT),
|
|
780
|
+
};
|
|
781
|
+
this.previousMovement = {
|
|
782
|
+
x: this.movement.x,
|
|
783
|
+
y: this.movement.y,
|
|
784
|
+
};
|
|
785
|
+
this._handleCallback();
|
|
786
|
+
};
|
|
787
|
+
ScrollGesture.prototype.scrollListener = function () {
|
|
788
|
+
var y = window.pageYOffset, x = window.pageXOffset;
|
|
789
|
+
this.onScroll({ x: x, y: y });
|
|
790
|
+
};
|
|
791
|
+
ScrollGesture.prototype.scrollElementListener = function () {
|
|
792
|
+
var _a, _b;
|
|
793
|
+
var x = ((_a = this.targetElement) === null || _a === void 0 ? void 0 : _a.scrollLeft) || 0;
|
|
794
|
+
var y = ((_b = this.targetElement) === null || _b === void 0 ? void 0 : _b.scrollTop) || 0;
|
|
795
|
+
this.onScroll({ x: x, y: y });
|
|
796
|
+
};
|
|
797
|
+
return ScrollGesture;
|
|
798
|
+
}(Gesture));
|
|
799
|
+
|
|
800
|
+
var LINE_HEIGHT = 40;
|
|
801
|
+
var PAGE_HEIGHT = 800;
|
|
802
|
+
var WheelGesture = /** @class */ (function (_super) {
|
|
803
|
+
__extends(WheelGesture, _super);
|
|
804
|
+
function WheelGesture() {
|
|
805
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
806
|
+
_this.movement = withDefault(0, 0);
|
|
807
|
+
_this.previousMovement = withDefault(0, 0);
|
|
808
|
+
_this.direction = withDefault(0, 0);
|
|
809
|
+
_this.velocity = withDefault(0, 0);
|
|
810
|
+
_this.delta = withDefault(0, 0);
|
|
811
|
+
// Holds offsets
|
|
812
|
+
_this.offset = withDefault(0, 0);
|
|
813
|
+
_this.translation = withDefault(0, 0);
|
|
814
|
+
return _this;
|
|
815
|
+
}
|
|
816
|
+
// @override
|
|
817
|
+
// initialize the events
|
|
818
|
+
WheelGesture.prototype._initEvents = function () {
|
|
819
|
+
if (this.targetElement) {
|
|
820
|
+
this._subscribe = attachEvents([this.targetElement], [["wheel", this.onWheel.bind(this)]]);
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
WheelGesture.prototype._handleCallback = function () {
|
|
824
|
+
if (this.callback) {
|
|
825
|
+
this.callback({
|
|
826
|
+
target: this.targetElement,
|
|
827
|
+
isWheeling: this.isActive,
|
|
828
|
+
deltaX: this.delta.x,
|
|
829
|
+
deltaY: this.delta.y,
|
|
830
|
+
directionX: this.direction.x,
|
|
831
|
+
directionY: this.direction.y,
|
|
832
|
+
movementX: this.movement.x,
|
|
833
|
+
movementY: this.movement.y,
|
|
834
|
+
offsetX: this.offset.x,
|
|
835
|
+
offsetY: this.offset.y,
|
|
836
|
+
velocityX: this.velocity.x,
|
|
837
|
+
velocityY: this.velocity.y,
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
WheelGesture.prototype.onWheel = function (event) {
|
|
842
|
+
var _this = this;
|
|
843
|
+
var deltaX = event.deltaX, deltaY = event.deltaY, deltaMode = event.deltaMode;
|
|
844
|
+
var now = Date.now();
|
|
845
|
+
var deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
846
|
+
this.lastTimeStamp = now;
|
|
847
|
+
var t = deltaTime / 1000; // seconds
|
|
848
|
+
this.isActive = true;
|
|
849
|
+
if (this.isActiveID !== -1) {
|
|
850
|
+
this.isActive = true;
|
|
851
|
+
clearTimeout(this.isActiveID);
|
|
852
|
+
}
|
|
853
|
+
this.isActiveID = setTimeout(function () {
|
|
854
|
+
_this.isActive = false;
|
|
855
|
+
_this.translation = { x: _this.offset.x, y: _this.offset.y };
|
|
856
|
+
_this._handleCallback();
|
|
857
|
+
_this.velocity = { x: 0, y: 0 }; // Reset Velocity
|
|
858
|
+
_this.movement = { x: 0, y: 0 };
|
|
859
|
+
}, 200);
|
|
860
|
+
// normalize wheel values, especially for Firefox
|
|
861
|
+
if (deltaMode === 1) {
|
|
862
|
+
deltaX *= LINE_HEIGHT;
|
|
863
|
+
deltaY *= LINE_HEIGHT;
|
|
864
|
+
}
|
|
865
|
+
else if (deltaMode === 2) {
|
|
866
|
+
deltaX *= PAGE_HEIGHT;
|
|
867
|
+
deltaY *= PAGE_HEIGHT;
|
|
868
|
+
}
|
|
869
|
+
this.delta = { x: deltaX, y: deltaY };
|
|
870
|
+
this.movement = {
|
|
871
|
+
x: this.movement.x + deltaX,
|
|
872
|
+
y: this.movement.y + deltaY,
|
|
873
|
+
};
|
|
874
|
+
this.offset = {
|
|
875
|
+
x: this.translation.x + this.movement.x,
|
|
876
|
+
y: this.translation.y + this.movement.y,
|
|
877
|
+
};
|
|
878
|
+
var diffX = this.movement.x - this.previousMovement.x;
|
|
879
|
+
var diffY = this.movement.y - this.previousMovement.y;
|
|
880
|
+
this.direction = {
|
|
881
|
+
x: Math.sign(diffX),
|
|
882
|
+
y: Math.sign(diffY),
|
|
883
|
+
};
|
|
884
|
+
this.velocity = {
|
|
885
|
+
x: clamp(diffX / t / 1000, -1 * Gesture._VELOCITY_LIMIT, Gesture._VELOCITY_LIMIT),
|
|
886
|
+
y: clamp(diffY / t / 1000, -1 * Gesture._VELOCITY_LIMIT, Gesture._VELOCITY_LIMIT),
|
|
887
|
+
};
|
|
888
|
+
this.previousMovement = {
|
|
889
|
+
x: this.movement.x,
|
|
890
|
+
y: this.movement.y,
|
|
891
|
+
};
|
|
892
|
+
this._handleCallback();
|
|
893
|
+
};
|
|
894
|
+
return WheelGesture;
|
|
895
|
+
}(Gesture));
|
|
896
|
+
|
|
897
|
+
var useRecognizer = function (handlers) {
|
|
898
|
+
var ref = React.useRef();
|
|
899
|
+
var elementRefs = React.useRef([]);
|
|
900
|
+
var subscribers = React.useRef(new Map()).current;
|
|
901
|
+
// re-initiate callback on change
|
|
902
|
+
React.useEffect(function () {
|
|
903
|
+
var e_1, _a;
|
|
904
|
+
try {
|
|
905
|
+
for (var _b = __values(subscribers.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
906
|
+
var _d = __read(_c.value, 2), _e = _d[1], keyIndex = _e.keyIndex, gesture = _e.gesture;
|
|
907
|
+
var _f = __read(handlers[keyIndex], 3), callback = _f[2];
|
|
908
|
+
gesture.applyCallback(callback);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
912
|
+
finally {
|
|
913
|
+
try {
|
|
914
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
915
|
+
}
|
|
916
|
+
finally { if (e_1) throw e_1.error; }
|
|
917
|
+
}
|
|
918
|
+
}, [handlers]);
|
|
919
|
+
React.useEffect(function () {
|
|
920
|
+
handlers.forEach(function (_a, keyIndex) {
|
|
921
|
+
var _b = __read(_a, 4), key = _b[0], gesture = _b[1], callback = _b[2], config = _b[3];
|
|
922
|
+
subscribers.set(key, {
|
|
923
|
+
keyIndex: keyIndex,
|
|
924
|
+
gesture: gesture,
|
|
925
|
+
unsubscribe: gesture.applyGesture({
|
|
926
|
+
targetElement: ref.current,
|
|
927
|
+
targetElements: elementRefs.current,
|
|
928
|
+
callback: callback,
|
|
929
|
+
config: config,
|
|
930
|
+
}),
|
|
931
|
+
});
|
|
932
|
+
});
|
|
933
|
+
return function () {
|
|
934
|
+
var e_2, _a;
|
|
935
|
+
try {
|
|
936
|
+
for (var _b = __values(subscribers.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
937
|
+
var _d = __read(_c.value, 2), unsubscribe = _d[1].unsubscribe;
|
|
938
|
+
unsubscribe && unsubscribe();
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
942
|
+
finally {
|
|
943
|
+
try {
|
|
944
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
945
|
+
}
|
|
946
|
+
finally { if (e_2) throw e_2.error; }
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
}, []);
|
|
950
|
+
return function (index) {
|
|
951
|
+
if (index === null || index === undefined) {
|
|
952
|
+
return { ref: ref };
|
|
953
|
+
}
|
|
954
|
+
else {
|
|
955
|
+
elementRefs.current[index] =
|
|
956
|
+
elementRefs.current[index] || React.createRef();
|
|
957
|
+
return { ref: elementRefs.current[index] };
|
|
958
|
+
}
|
|
959
|
+
};
|
|
960
|
+
};
|
|
961
|
+
|
|
962
|
+
function useDrag(callback, config) {
|
|
963
|
+
var gesture = React.useRef(new DragGesture()).current;
|
|
964
|
+
return useRecognizer([["drag", gesture, callback, config]]);
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
function useMouseMove(callback) {
|
|
968
|
+
var gesture = React.useRef(new MouseMoveGesture()).current;
|
|
969
|
+
return useRecognizer([["move", gesture, callback]]);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
function useScroll(callback) {
|
|
973
|
+
var gesture = React.useRef(new ScrollGesture()).current;
|
|
974
|
+
return useRecognizer([["scroll", gesture, callback]]);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
function useWheel(callback) {
|
|
978
|
+
var gesture = React.useRef(new WheelGesture()).current;
|
|
979
|
+
return useRecognizer([["wheel", gesture, callback]]);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
function useGesture(_a) {
|
|
983
|
+
var onDrag = _a.onDrag, onWheel = _a.onWheel, onScroll = _a.onScroll, onMouseMove = _a.onMouseMove;
|
|
984
|
+
var dragGesture = React.useRef(new DragGesture()).current;
|
|
985
|
+
var wheelGesture = React.useRef(new WheelGesture()).current;
|
|
986
|
+
var scrollGesture = React.useRef(new ScrollGesture()).current;
|
|
987
|
+
var mouseMoveGesture = React.useRef(new MouseMoveGesture()).current;
|
|
988
|
+
return useRecognizer([
|
|
989
|
+
["drag", dragGesture, onDrag],
|
|
990
|
+
["wheel", wheelGesture, onWheel],
|
|
991
|
+
["scroll", scrollGesture, onScroll],
|
|
992
|
+
["move", mouseMoveGesture, onMouseMove],
|
|
993
|
+
]);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
function useOutsideClick(elementRef, callback, deps) {
|
|
997
|
+
var callbackRef = React.useRef();
|
|
998
|
+
if (!callbackRef.current) {
|
|
999
|
+
callbackRef.current = callback;
|
|
1000
|
+
}
|
|
1001
|
+
// Reinitiate callback when dependency change
|
|
1002
|
+
React.useEffect(function () {
|
|
1003
|
+
callbackRef.current = callback;
|
|
1004
|
+
return function () {
|
|
1005
|
+
callbackRef.current = function () { return false; };
|
|
1006
|
+
};
|
|
1007
|
+
}, deps);
|
|
1008
|
+
React.useEffect(function () {
|
|
1009
|
+
var handleOutsideClick = function (e) {
|
|
1010
|
+
var _a;
|
|
1011
|
+
if (!((_a = elementRef === null || elementRef === void 0 ? void 0 : elementRef.current) === null || _a === void 0 ? void 0 : _a.contains(e.target))) {
|
|
1012
|
+
callbackRef.current && callbackRef.current(e);
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
var subscribe = attachEvents([document], [["click", handleOutsideClick]]);
|
|
1016
|
+
return function () { return subscribe && subscribe(); };
|
|
1017
|
+
}, []);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
function useMeasure(callback, deps) {
|
|
1021
|
+
var ref = React.useRef(null);
|
|
1022
|
+
var elementRefs = React.useRef([]);
|
|
1023
|
+
var callbackRef = React.useRef(callback);
|
|
1024
|
+
// Reinitiate callback when dependency change
|
|
1025
|
+
React.useEffect(function () {
|
|
1026
|
+
callbackRef.current = callback;
|
|
1027
|
+
return function () {
|
|
1028
|
+
callbackRef.current = function () { return false; };
|
|
1029
|
+
};
|
|
1030
|
+
}, deps);
|
|
1031
|
+
React.useEffect(function () {
|
|
1032
|
+
var _refElement = ref.current || document.documentElement;
|
|
1033
|
+
var _refElementsMultiple = elementRefs.current;
|
|
1034
|
+
var resizeObserver = new ResizeObserver(function (_a) {
|
|
1035
|
+
var _b = __read(_a, 1), entry = _b[0];
|
|
1036
|
+
var _c = entry.target.getBoundingClientRect(), left = _c.left, top = _c.top, width = _c.width, height = _c.height;
|
|
1037
|
+
var pageXOffset = window.pageXOffset, pageYOffset = window.pageYOffset;
|
|
1038
|
+
if (callbackRef) {
|
|
1039
|
+
if (_refElement === document.documentElement) {
|
|
1040
|
+
return; // no-op for document
|
|
1041
|
+
}
|
|
1042
|
+
else {
|
|
1043
|
+
callbackRef.current({
|
|
1044
|
+
left: left + pageXOffset,
|
|
1045
|
+
top: top + pageYOffset,
|
|
1046
|
+
width: width,
|
|
1047
|
+
height: height,
|
|
1048
|
+
vLeft: left,
|
|
1049
|
+
vTop: top,
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
});
|
|
1054
|
+
var resizeObserverMultiple = new ResizeObserver(function (entries) {
|
|
1055
|
+
var left = [];
|
|
1056
|
+
var top = [];
|
|
1057
|
+
var width = [];
|
|
1058
|
+
var height = [];
|
|
1059
|
+
var vLeft = [];
|
|
1060
|
+
var vTop = [];
|
|
1061
|
+
entries.forEach(function (entry) {
|
|
1062
|
+
var _a = entry.target.getBoundingClientRect(), _left = _a.left, _top = _a.top, _width = _a.width, _height = _a.height;
|
|
1063
|
+
var pageXOffset = window.pageXOffset, pageYOffset = window.pageYOffset;
|
|
1064
|
+
var _pageLeft = _left + pageXOffset;
|
|
1065
|
+
var _pageTop = _top + pageYOffset;
|
|
1066
|
+
left.push(_pageLeft);
|
|
1067
|
+
top.push(_pageTop);
|
|
1068
|
+
width.push(_width);
|
|
1069
|
+
height.push(_height);
|
|
1070
|
+
vLeft.push(_left);
|
|
1071
|
+
vTop.push(_top);
|
|
1072
|
+
});
|
|
1073
|
+
if (callbackRef) {
|
|
1074
|
+
callbackRef.current({
|
|
1075
|
+
left: left,
|
|
1076
|
+
top: top,
|
|
1077
|
+
width: width,
|
|
1078
|
+
height: height,
|
|
1079
|
+
vLeft: vLeft,
|
|
1080
|
+
vTop: vTop,
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
});
|
|
1084
|
+
if (_refElement) {
|
|
1085
|
+
if (_refElement === document.documentElement &&
|
|
1086
|
+
_refElementsMultiple.length > 0) {
|
|
1087
|
+
_refElementsMultiple.forEach(function (element) {
|
|
1088
|
+
resizeObserverMultiple.observe(element.current);
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
else {
|
|
1092
|
+
resizeObserver.observe(_refElement);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
return function () {
|
|
1096
|
+
if (_refElement) {
|
|
1097
|
+
if (_refElement === document.documentElement &&
|
|
1098
|
+
_refElementsMultiple.length > 0) {
|
|
1099
|
+
_refElementsMultiple.forEach(function (element) {
|
|
1100
|
+
resizeObserverMultiple.unobserve(element.current);
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
else {
|
|
1104
|
+
resizeObserver.unobserve(_refElement);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
};
|
|
1108
|
+
}, []);
|
|
1109
|
+
return function (index) {
|
|
1110
|
+
if (index === null || index === undefined) {
|
|
1111
|
+
return { ref: ref };
|
|
1112
|
+
}
|
|
1113
|
+
else {
|
|
1114
|
+
elementRefs.current[index] =
|
|
1115
|
+
elementRefs.current[index] || React.createRef();
|
|
1116
|
+
return { ref: elementRefs.current[index] };
|
|
1117
|
+
}
|
|
1118
|
+
}; // ...bind() or ...bind(index) for multiple
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
function useWindowDimension(callback, deps) {
|
|
1122
|
+
var windowDimensionsRef = React.useRef({
|
|
1123
|
+
width: 0,
|
|
1124
|
+
height: 0,
|
|
1125
|
+
innerWidth: 0,
|
|
1126
|
+
innerHeight: 0,
|
|
1127
|
+
});
|
|
1128
|
+
var callbackRef = React.useRef(callback);
|
|
1129
|
+
var handleCallback = function () {
|
|
1130
|
+
if (callbackRef) {
|
|
1131
|
+
callbackRef.current(__assign({}, windowDimensionsRef.current));
|
|
1132
|
+
}
|
|
1133
|
+
};
|
|
1134
|
+
// Reinitiate callback when dependency change
|
|
1135
|
+
React.useEffect(function () {
|
|
1136
|
+
callbackRef.current = callback;
|
|
1137
|
+
return function () {
|
|
1138
|
+
callbackRef.current = function () { return false; };
|
|
1139
|
+
};
|
|
1140
|
+
}, deps);
|
|
1141
|
+
React.useEffect(function () {
|
|
1142
|
+
var resizeObserver = new ResizeObserver(function (_a) {
|
|
1143
|
+
var _b = __read(_a, 1), entry = _b[0];
|
|
1144
|
+
var _c = entry.target, clientWidth = _c.clientWidth, clientHeight = _c.clientHeight;
|
|
1145
|
+
var innerWidth = window.innerWidth, innerHeight = window.innerHeight;
|
|
1146
|
+
windowDimensionsRef.current = {
|
|
1147
|
+
width: clientWidth,
|
|
1148
|
+
height: clientHeight,
|
|
1149
|
+
innerWidth: innerWidth,
|
|
1150
|
+
innerHeight: innerHeight,
|
|
1151
|
+
};
|
|
1152
|
+
handleCallback();
|
|
1153
|
+
});
|
|
1154
|
+
resizeObserver.observe(document.documentElement);
|
|
1155
|
+
return function () { return resizeObserver.unobserve(document.documentElement); };
|
|
1156
|
+
}, []);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
Object.defineProperty(exports, 'Easing', {
|
|
1160
|
+
enumerable: true,
|
|
1161
|
+
get: function () {
|
|
1162
|
+
return reMotion.Easing;
|
|
1163
|
+
}
|
|
1164
|
+
});
|
|
1165
|
+
exports.AnimatedBlock = AnimatedBlock;
|
|
1166
|
+
exports.AnimatedImage = AnimatedImage;
|
|
1167
|
+
exports.AnimatedInline = AnimatedInline;
|
|
1168
|
+
exports.MountedBlock = MountedBlock;
|
|
1169
|
+
exports.ScrollableBlock = ScrollableBlock;
|
|
1170
|
+
exports.bInterpolate = bInterpolate;
|
|
1171
|
+
exports.bin = bin;
|
|
1172
|
+
exports.clamp = clamp;
|
|
1173
|
+
exports.interpolate = interpolate;
|
|
1174
|
+
exports.makeAnimatedComponent = makeAnimatedComponent;
|
|
1175
|
+
exports.mix = mix;
|
|
1176
|
+
exports.move = move;
|
|
1177
|
+
exports.rubberClamp = rubberClamp;
|
|
1178
|
+
exports.snapTo = snapTo;
|
|
1179
|
+
exports.useAnimatedValue = useAnimatedValue;
|
|
1180
|
+
exports.useDrag = useDrag;
|
|
1181
|
+
exports.useGesture = useGesture;
|
|
1182
|
+
exports.useMeasure = useMeasure;
|
|
1183
|
+
exports.useMountedValue = useMountedValue;
|
|
1184
|
+
exports.useMouseMove = useMouseMove;
|
|
1185
|
+
exports.useOutsideClick = useOutsideClick;
|
|
1186
|
+
exports.useScroll = useScroll;
|
|
1187
|
+
exports.useWheel = useWheel;
|
|
1188
|
+
exports.useWindowDimension = useWindowDimension;
|
|
1189
|
+
//# sourceMappingURL=index.js.map
|