react-ui-animate 2.0.0-rc.1 → 2.0.0-rc.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/.vscode/settings.json +3 -3
- package/LICENSE +21 -21
- package/README.md +115 -115
- package/dist/animation/animationType.d.ts +8 -0
- package/dist/animation/getInitialConfig.d.ts +2 -2
- package/dist/index.d.ts +5 -4
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -1
- package/package.json +49 -49
- package/rollup.config.js +18 -18
- package/src/animation/animationType.ts +17 -9
- package/src/animation/getInitialConfig.ts +61 -30
- package/src/animation/index.ts +9 -9
- package/src/animation/interpolation.ts +24 -24
- package/src/animation/modules.tsx +105 -105
- package/src/animation/useAnimatedValue.ts +62 -62
- package/src/animation/useMountedValue.ts +66 -66
- package/src/gestures/controllers/DragGesture.ts +177 -177
- package/src/gestures/controllers/Gesture.ts +54 -54
- package/src/gestures/controllers/MouseMoveGesture.ts +111 -111
- package/src/gestures/controllers/ScrollGesture.ts +107 -107
- package/src/gestures/controllers/WheelGesture.ts +123 -123
- package/src/gestures/controllers/index.ts +4 -4
- package/src/gestures/eventAttacher.ts +67 -67
- package/src/gestures/hooks/index.ts +5 -5
- package/src/gestures/hooks/useDrag.ts +14 -14
- package/src/gestures/hooks/useGesture.ts +38 -38
- package/src/gestures/hooks/useMouseMove.ts +11 -11
- package/src/gestures/hooks/useRecognizer.ts +59 -59
- package/src/gestures/hooks/useScroll.ts +11 -11
- package/src/gestures/hooks/useWheel.ts +11 -11
- package/src/gestures/index.ts +2 -2
- package/src/gestures/math.ts +120 -120
- package/src/gestures/types.ts +49 -49
- package/src/gestures/withDefault.ts +3 -3
- package/src/hooks/index.ts +3 -3
- package/src/hooks/useMeasure.ts +133 -133
- package/src/hooks/useOutsideClick.ts +36 -36
- package/src/hooks/useWindowDimension.ts +59 -59
- package/src/index.ts +5 -4
- package/src/utils/delay.ts +9 -9
- package/src/utils/index.ts +2 -2
- package/src/utils/isDefined.ts +4 -4
- package/tsconfig.json +25 -25
|
@@ -1,38 +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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,11 +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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,59 +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
|
-
};
|
|
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
|
+
};
|
|
@@ -1,11 +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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,11 +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
|
-
}
|
|
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
|
+
}
|
package/src/gestures/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./hooks";
|
|
2
|
-
export * from "./math";
|
|
1
|
+
export * from "./hooks";
|
|
2
|
+
export * from "./math";
|
package/src/gestures/math.ts
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* bin(booleanValue)
|
|
3
|
-
* returns 1 if booleanValue == true and 0 if booleanValue == false
|
|
4
|
-
*/
|
|
5
|
-
export function bin(bool: boolean) {
|
|
6
|
-
return bool ? 1 : 0;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* mix(progress, a, b)
|
|
11
|
-
* linear interpolation between a and b
|
|
12
|
-
*/
|
|
13
|
-
export function mix(perc: number, val1: number, val2: number) {
|
|
14
|
-
return val1 * (1 - perc) + val2 * perc;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* clamp(value, min, max)
|
|
19
|
-
* clamps value for min and max bounds
|
|
20
|
-
*/
|
|
21
|
-
export function clamp(value: number, lowerbound: number, upperbound: number) {
|
|
22
|
-
return Math.min(Math.max(value, lowerbound), upperbound);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function rubber2(distanceFromEdge: number, constant: number) {
|
|
26
|
-
return Math.pow(distanceFromEdge, constant * 5);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function rubber(distanceFromEdge: number, dimension: number, constant: number) {
|
|
30
|
-
if (dimension === 0 || Math.abs(dimension) === Infinity)
|
|
31
|
-
return rubber2(distanceFromEdge, constant);
|
|
32
|
-
return (
|
|
33
|
-
(distanceFromEdge * dimension * constant) /
|
|
34
|
-
(dimension + constant * distanceFromEdge)
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* rubberClamp(value, min, max, constant?)
|
|
40
|
-
* constant is optional : default 0.15
|
|
41
|
-
* clamps the value for min and max value and
|
|
42
|
-
* extends beyond min and max values with constant
|
|
43
|
-
* factor to create elastic rubber band effect
|
|
44
|
-
*/
|
|
45
|
-
export function rubberClamp(
|
|
46
|
-
value: number,
|
|
47
|
-
lowerbound: number,
|
|
48
|
-
upperbound: number,
|
|
49
|
-
constant: number = 0.15
|
|
50
|
-
) {
|
|
51
|
-
if (constant === 0) return clamp(value, lowerbound, upperbound);
|
|
52
|
-
|
|
53
|
-
if (value < lowerbound) {
|
|
54
|
-
return (
|
|
55
|
-
-rubber(lowerbound - value, upperbound - lowerbound, constant) +
|
|
56
|
-
lowerbound
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (value > upperbound) {
|
|
61
|
-
return (
|
|
62
|
-
+rubber(value - upperbound, upperbound - lowerbound, constant) +
|
|
63
|
-
upperbound
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* snapTo(value, velocity, snapPoints[])
|
|
72
|
-
* Calculates the final snapPoint according to given current value,
|
|
73
|
-
* velocity and snapPoints array
|
|
74
|
-
*/
|
|
75
|
-
export function snapTo(
|
|
76
|
-
value: number,
|
|
77
|
-
velocity: number,
|
|
78
|
-
snapPoints: Array<number>
|
|
79
|
-
): number {
|
|
80
|
-
const finalValue = value + velocity * 0.2;
|
|
81
|
-
const getDiff = (point: number) => Math.abs(point - finalValue);
|
|
82
|
-
const deltas = snapPoints.map(getDiff);
|
|
83
|
-
const minDelta = Math.min(...deltas);
|
|
84
|
-
|
|
85
|
-
return snapPoints.reduce(function (acc, point) {
|
|
86
|
-
if (getDiff(point) === minDelta) {
|
|
87
|
-
return point;
|
|
88
|
-
} else {
|
|
89
|
-
return acc;
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* move(array, moveIndex, toIndex)
|
|
96
|
-
* move array item from moveIndex to toIndex without array modification
|
|
97
|
-
*/
|
|
98
|
-
export function move(array: Array<any>, moveIndex: number, toIndex: number) {
|
|
99
|
-
const item = array[moveIndex];
|
|
100
|
-
const length = array.length;
|
|
101
|
-
const diff = moveIndex - toIndex;
|
|
102
|
-
|
|
103
|
-
if (diff > 0) {
|
|
104
|
-
return [
|
|
105
|
-
...array.slice(0, toIndex),
|
|
106
|
-
item,
|
|
107
|
-
...array.slice(toIndex, moveIndex),
|
|
108
|
-
...array.slice(moveIndex + 1, length),
|
|
109
|
-
];
|
|
110
|
-
} else if (diff < 0) {
|
|
111
|
-
const targetIndex = toIndex + 1;
|
|
112
|
-
return [
|
|
113
|
-
...array.slice(0, moveIndex),
|
|
114
|
-
...array.slice(moveIndex + 1, targetIndex),
|
|
115
|
-
item,
|
|
116
|
-
...array.slice(targetIndex, length),
|
|
117
|
-
];
|
|
118
|
-
}
|
|
119
|
-
return array;
|
|
120
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* bin(booleanValue)
|
|
3
|
+
* returns 1 if booleanValue == true and 0 if booleanValue == false
|
|
4
|
+
*/
|
|
5
|
+
export function bin(bool: boolean) {
|
|
6
|
+
return bool ? 1 : 0;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* mix(progress, a, b)
|
|
11
|
+
* linear interpolation between a and b
|
|
12
|
+
*/
|
|
13
|
+
export function mix(perc: number, val1: number, val2: number) {
|
|
14
|
+
return val1 * (1 - perc) + val2 * perc;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* clamp(value, min, max)
|
|
19
|
+
* clamps value for min and max bounds
|
|
20
|
+
*/
|
|
21
|
+
export function clamp(value: number, lowerbound: number, upperbound: number) {
|
|
22
|
+
return Math.min(Math.max(value, lowerbound), upperbound);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function rubber2(distanceFromEdge: number, constant: number) {
|
|
26
|
+
return Math.pow(distanceFromEdge, constant * 5);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function rubber(distanceFromEdge: number, dimension: number, constant: number) {
|
|
30
|
+
if (dimension === 0 || Math.abs(dimension) === Infinity)
|
|
31
|
+
return rubber2(distanceFromEdge, constant);
|
|
32
|
+
return (
|
|
33
|
+
(distanceFromEdge * dimension * constant) /
|
|
34
|
+
(dimension + constant * distanceFromEdge)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* rubberClamp(value, min, max, constant?)
|
|
40
|
+
* constant is optional : default 0.15
|
|
41
|
+
* clamps the value for min and max value and
|
|
42
|
+
* extends beyond min and max values with constant
|
|
43
|
+
* factor to create elastic rubber band effect
|
|
44
|
+
*/
|
|
45
|
+
export function rubberClamp(
|
|
46
|
+
value: number,
|
|
47
|
+
lowerbound: number,
|
|
48
|
+
upperbound: number,
|
|
49
|
+
constant: number = 0.15
|
|
50
|
+
) {
|
|
51
|
+
if (constant === 0) return clamp(value, lowerbound, upperbound);
|
|
52
|
+
|
|
53
|
+
if (value < lowerbound) {
|
|
54
|
+
return (
|
|
55
|
+
-rubber(lowerbound - value, upperbound - lowerbound, constant) +
|
|
56
|
+
lowerbound
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (value > upperbound) {
|
|
61
|
+
return (
|
|
62
|
+
+rubber(value - upperbound, upperbound - lowerbound, constant) +
|
|
63
|
+
upperbound
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* snapTo(value, velocity, snapPoints[])
|
|
72
|
+
* Calculates the final snapPoint according to given current value,
|
|
73
|
+
* velocity and snapPoints array
|
|
74
|
+
*/
|
|
75
|
+
export function snapTo(
|
|
76
|
+
value: number,
|
|
77
|
+
velocity: number,
|
|
78
|
+
snapPoints: Array<number>
|
|
79
|
+
): number {
|
|
80
|
+
const finalValue = value + velocity * 0.2;
|
|
81
|
+
const getDiff = (point: number) => Math.abs(point - finalValue);
|
|
82
|
+
const deltas = snapPoints.map(getDiff);
|
|
83
|
+
const minDelta = Math.min(...deltas);
|
|
84
|
+
|
|
85
|
+
return snapPoints.reduce(function (acc, point) {
|
|
86
|
+
if (getDiff(point) === minDelta) {
|
|
87
|
+
return point;
|
|
88
|
+
} else {
|
|
89
|
+
return acc;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* move(array, moveIndex, toIndex)
|
|
96
|
+
* move array item from moveIndex to toIndex without array modification
|
|
97
|
+
*/
|
|
98
|
+
export function move(array: Array<any>, moveIndex: number, toIndex: number) {
|
|
99
|
+
const item = array[moveIndex];
|
|
100
|
+
const length = array.length;
|
|
101
|
+
const diff = moveIndex - toIndex;
|
|
102
|
+
|
|
103
|
+
if (diff > 0) {
|
|
104
|
+
return [
|
|
105
|
+
...array.slice(0, toIndex),
|
|
106
|
+
item,
|
|
107
|
+
...array.slice(toIndex, moveIndex),
|
|
108
|
+
...array.slice(moveIndex + 1, length),
|
|
109
|
+
];
|
|
110
|
+
} else if (diff < 0) {
|
|
111
|
+
const targetIndex = toIndex + 1;
|
|
112
|
+
return [
|
|
113
|
+
...array.slice(0, moveIndex),
|
|
114
|
+
...array.slice(moveIndex + 1, targetIndex),
|
|
115
|
+
item,
|
|
116
|
+
...array.slice(targetIndex, length),
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
return array;
|
|
120
|
+
}
|
package/src/gestures/types.ts
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
type GenericEventType = {
|
|
2
|
-
velocityX: number;
|
|
3
|
-
velocityY: number;
|
|
4
|
-
directionX: number;
|
|
5
|
-
directionY: number;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export type DragEventType = {
|
|
9
|
-
args: Array<number | undefined>;
|
|
10
|
-
down: boolean;
|
|
11
|
-
movementX: number;
|
|
12
|
-
movementY: number;
|
|
13
|
-
offsetX: number;
|
|
14
|
-
offsetY: number;
|
|
15
|
-
distanceX: number;
|
|
16
|
-
distanceY: number;
|
|
17
|
-
cancel: () => void;
|
|
18
|
-
} & GenericEventType;
|
|
19
|
-
|
|
20
|
-
export type MouseMoveEventType = {
|
|
21
|
-
args: Array<number | undefined>;
|
|
22
|
-
event: MouseEvent;
|
|
23
|
-
target: EventTarget | undefined | null;
|
|
24
|
-
isMoving: boolean;
|
|
25
|
-
mouseX: number;
|
|
26
|
-
mouseY: number;
|
|
27
|
-
} & GenericEventType;
|
|
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
|
-
|
|
46
|
-
export type UseDragConfig = {
|
|
47
|
-
initial?: () => { movementX: number; movementY: number };
|
|
48
|
-
};
|
|
49
|
-
export type Vector2 = { x: number; y: number };
|
|
1
|
+
type GenericEventType = {
|
|
2
|
+
velocityX: number;
|
|
3
|
+
velocityY: number;
|
|
4
|
+
directionX: number;
|
|
5
|
+
directionY: number;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type DragEventType = {
|
|
9
|
+
args: Array<number | undefined>;
|
|
10
|
+
down: boolean;
|
|
11
|
+
movementX: number;
|
|
12
|
+
movementY: number;
|
|
13
|
+
offsetX: number;
|
|
14
|
+
offsetY: number;
|
|
15
|
+
distanceX: number;
|
|
16
|
+
distanceY: number;
|
|
17
|
+
cancel: () => void;
|
|
18
|
+
} & GenericEventType;
|
|
19
|
+
|
|
20
|
+
export type MouseMoveEventType = {
|
|
21
|
+
args: Array<number | undefined>;
|
|
22
|
+
event: MouseEvent;
|
|
23
|
+
target: EventTarget | undefined | null;
|
|
24
|
+
isMoving: boolean;
|
|
25
|
+
mouseX: number;
|
|
26
|
+
mouseY: number;
|
|
27
|
+
} & GenericEventType;
|
|
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
|
+
|
|
46
|
+
export type UseDragConfig = {
|
|
47
|
+
initial?: () => { movementX: number; movementY: number };
|
|
48
|
+
};
|
|
49
|
+
export type Vector2 = { x: number; y: number };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const withDefault = (x: number, y: number) => {
|
|
2
|
-
return { x, y };
|
|
3
|
-
};
|
|
1
|
+
export const withDefault = (x: number, y: number) => {
|
|
2
|
+
return { x, y };
|
|
3
|
+
};
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./useOutsideClick";
|
|
2
|
-
export * from "./useMeasure";
|
|
3
|
-
export * from "./useWindowDimension";
|
|
1
|
+
export * from "./useOutsideClick";
|
|
2
|
+
export * from "./useMeasure";
|
|
3
|
+
export * from "./useWindowDimension";
|