react-ui-animate 2.0.0-rc.2 → 2.0.0-rc.3
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/index.d.ts +1 -0
- package/dist/animation/modules/AnimatedBlock.d.ts +8 -0
- package/dist/animation/modules/AnimatedImage.d.ts +8 -0
- package/dist/animation/modules/AnimatedInline.d.ts +8 -0
- package/dist/animation/modules/MountedBlock.d.ts +18 -0
- package/dist/animation/modules/ScrollableBlock.d.ts +22 -0
- package/dist/animation/modules/TransitionBlock.d.ts +18 -0
- package/dist/animation/modules/index.d.ts +6 -0
- package/dist/animation/useAnimatedValue.d.ts +7 -3
- package/dist/animation/useMountedValue.d.ts +5 -4
- package/dist/gestures/controllers/MouseMoveGesture.d.ts +2 -2
- package/dist/gestures/controllers/ScrollGesture.d.ts +2 -2
- package/dist/gestures/controllers/WheelGesture.d.ts +2 -2
- package/dist/gestures/controllers/index.d.ts +4 -4
- package/dist/gestures/eventAttacher.d.ts +1 -1
- package/dist/gestures/hooks/index.d.ts +5 -5
- package/dist/gestures/hooks/useDrag.d.ts +1 -1
- package/dist/gestures/hooks/useGesture.d.ts +1 -1
- package/dist/gestures/hooks/useMouseMove.d.ts +1 -1
- package/dist/gestures/hooks/useRecognizer.d.ts +1 -1
- package/dist/gestures/hooks/useScroll.d.ts +1 -1
- package/dist/gestures/hooks/useWheel.d.ts +1 -1
- package/dist/gestures/index.d.ts +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +181 -150
- package/dist/index.js.map +1 -1
- package/package.json +49 -49
- package/rollup.config.js +18 -18
- package/src/animation/animationType.ts +17 -17
- package/src/animation/getInitialConfig.ts +61 -61
- package/src/animation/index.ts +10 -9
- package/src/animation/interpolation.ts +24 -24
- package/src/animation/modules/AnimatedBlock.ts +8 -0
- package/src/animation/modules/AnimatedImage.ts +8 -0
- package/src/animation/modules/AnimatedInline.ts +8 -0
- package/src/animation/modules/MountedBlock.tsx +25 -0
- package/src/animation/modules/ScrollableBlock.tsx +62 -0
- package/src/animation/modules/TransitionBlock.tsx +26 -0
- package/src/animation/modules/index.ts +6 -0
- package/src/animation/useAnimatedValue.ts +71 -62
- package/src/animation/useMountedValue.ts +67 -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 -5
- 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
- package/dist/animation/modules.d.ts +0 -55
- package/src/animation/modules.tsx +0 -105
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";
|
package/src/hooks/useMeasure.ts
CHANGED
|
@@ -1,133 +1,133 @@
|
|
|
1
|
-
import * as React from "react";
|
|
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
|
-
};
|
|
13
|
-
|
|
14
|
-
export function useMeasure(
|
|
15
|
-
callback: (event: MeasurementType) => void,
|
|
16
|
-
deps?: React.DependencyList
|
|
17
|
-
) {
|
|
18
|
-
const ref = React.useRef(null);
|
|
19
|
-
const elementRefs = React.useRef([]);
|
|
20
|
-
const callbackRef = React.useRef<(event: MeasurementType) => void>(callback);
|
|
21
|
-
|
|
22
|
-
// Reinitiate callback when dependency change
|
|
23
|
-
React.useEffect(() => {
|
|
24
|
-
callbackRef.current = callback;
|
|
25
|
-
|
|
26
|
-
return () => {
|
|
27
|
-
callbackRef.current = () => false;
|
|
28
|
-
};
|
|
29
|
-
}, deps);
|
|
30
|
-
|
|
31
|
-
React.useEffect(() => {
|
|
32
|
-
const _refElement = ref.current || document.documentElement;
|
|
33
|
-
const _refElementsMultiple = elementRefs.current;
|
|
34
|
-
|
|
35
|
-
const resizeObserver = new ResizeObserver(([entry]) => {
|
|
36
|
-
const { left, top, width, height } = entry.target.getBoundingClientRect();
|
|
37
|
-
const { pageXOffset, pageYOffset } = window;
|
|
38
|
-
|
|
39
|
-
if (callbackRef) {
|
|
40
|
-
if (_refElement === document.documentElement) {
|
|
41
|
-
return; // no-op for document
|
|
42
|
-
} else {
|
|
43
|
-
callbackRef.current({
|
|
44
|
-
left: left + pageXOffset,
|
|
45
|
-
top: top + pageYOffset,
|
|
46
|
-
width,
|
|
47
|
-
height,
|
|
48
|
-
vLeft: left,
|
|
49
|
-
vTop: top,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
const resizeObserverMultiple = new ResizeObserver((entries) => {
|
|
56
|
-
const left: Array<number> = [];
|
|
57
|
-
const top: Array<number> = [];
|
|
58
|
-
const width: Array<number> = [];
|
|
59
|
-
const height: Array<number> = [];
|
|
60
|
-
const vLeft: Array<number> = [];
|
|
61
|
-
const vTop: Array<number> = [];
|
|
62
|
-
|
|
63
|
-
entries.forEach((entry) => {
|
|
64
|
-
const {
|
|
65
|
-
left: _left,
|
|
66
|
-
top: _top,
|
|
67
|
-
width: _width,
|
|
68
|
-
height: _height,
|
|
69
|
-
} = entry.target.getBoundingClientRect();
|
|
70
|
-
const { pageXOffset, pageYOffset } = window;
|
|
71
|
-
const _pageLeft = _left + pageXOffset;
|
|
72
|
-
const _pageTop = _top + pageYOffset;
|
|
73
|
-
|
|
74
|
-
left.push(_pageLeft);
|
|
75
|
-
top.push(_pageTop);
|
|
76
|
-
width.push(_width);
|
|
77
|
-
height.push(_height);
|
|
78
|
-
vLeft.push(_left);
|
|
79
|
-
vTop.push(_top);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
if (callbackRef) {
|
|
83
|
-
callbackRef.current({
|
|
84
|
-
left,
|
|
85
|
-
top,
|
|
86
|
-
width,
|
|
87
|
-
height,
|
|
88
|
-
vLeft,
|
|
89
|
-
vTop,
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
if (_refElement) {
|
|
95
|
-
if (
|
|
96
|
-
_refElement === document.documentElement &&
|
|
97
|
-
_refElementsMultiple.length > 0
|
|
98
|
-
) {
|
|
99
|
-
_refElementsMultiple.forEach((element: any) => {
|
|
100
|
-
resizeObserverMultiple.observe(element.current);
|
|
101
|
-
});
|
|
102
|
-
} else {
|
|
103
|
-
resizeObserver.observe(_refElement);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return () => {
|
|
108
|
-
if (_refElement) {
|
|
109
|
-
if (
|
|
110
|
-
_refElement === document.documentElement &&
|
|
111
|
-
_refElementsMultiple.length > 0
|
|
112
|
-
) {
|
|
113
|
-
_refElementsMultiple.forEach((element: any) => {
|
|
114
|
-
resizeObserverMultiple.unobserve(element.current);
|
|
115
|
-
});
|
|
116
|
-
} else {
|
|
117
|
-
resizeObserver.unobserve(_refElement);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
}, []);
|
|
122
|
-
|
|
123
|
-
return (index?: number) => {
|
|
124
|
-
if (index === null || index === undefined) {
|
|
125
|
-
return { ref };
|
|
126
|
-
} else {
|
|
127
|
-
elementRefs.current[index] =
|
|
128
|
-
elementRefs.current[index] || React.createRef();
|
|
129
|
-
|
|
130
|
-
return { ref: elementRefs.current[index] };
|
|
131
|
-
}
|
|
132
|
-
}; // ...bind() or ...bind(index) for multiple
|
|
133
|
-
}
|
|
1
|
+
import * as React from "react";
|
|
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
|
+
};
|
|
13
|
+
|
|
14
|
+
export function useMeasure(
|
|
15
|
+
callback: (event: MeasurementType) => void,
|
|
16
|
+
deps?: React.DependencyList
|
|
17
|
+
) {
|
|
18
|
+
const ref = React.useRef(null);
|
|
19
|
+
const elementRefs = React.useRef([]);
|
|
20
|
+
const callbackRef = React.useRef<(event: MeasurementType) => void>(callback);
|
|
21
|
+
|
|
22
|
+
// Reinitiate callback when dependency change
|
|
23
|
+
React.useEffect(() => {
|
|
24
|
+
callbackRef.current = callback;
|
|
25
|
+
|
|
26
|
+
return () => {
|
|
27
|
+
callbackRef.current = () => false;
|
|
28
|
+
};
|
|
29
|
+
}, deps);
|
|
30
|
+
|
|
31
|
+
React.useEffect(() => {
|
|
32
|
+
const _refElement = ref.current || document.documentElement;
|
|
33
|
+
const _refElementsMultiple = elementRefs.current;
|
|
34
|
+
|
|
35
|
+
const resizeObserver = new ResizeObserver(([entry]) => {
|
|
36
|
+
const { left, top, width, height } = entry.target.getBoundingClientRect();
|
|
37
|
+
const { pageXOffset, pageYOffset } = window;
|
|
38
|
+
|
|
39
|
+
if (callbackRef) {
|
|
40
|
+
if (_refElement === document.documentElement) {
|
|
41
|
+
return; // no-op for document
|
|
42
|
+
} else {
|
|
43
|
+
callbackRef.current({
|
|
44
|
+
left: left + pageXOffset,
|
|
45
|
+
top: top + pageYOffset,
|
|
46
|
+
width,
|
|
47
|
+
height,
|
|
48
|
+
vLeft: left,
|
|
49
|
+
vTop: top,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const resizeObserverMultiple = new ResizeObserver((entries) => {
|
|
56
|
+
const left: Array<number> = [];
|
|
57
|
+
const top: Array<number> = [];
|
|
58
|
+
const width: Array<number> = [];
|
|
59
|
+
const height: Array<number> = [];
|
|
60
|
+
const vLeft: Array<number> = [];
|
|
61
|
+
const vTop: Array<number> = [];
|
|
62
|
+
|
|
63
|
+
entries.forEach((entry) => {
|
|
64
|
+
const {
|
|
65
|
+
left: _left,
|
|
66
|
+
top: _top,
|
|
67
|
+
width: _width,
|
|
68
|
+
height: _height,
|
|
69
|
+
} = entry.target.getBoundingClientRect();
|
|
70
|
+
const { pageXOffset, pageYOffset } = window;
|
|
71
|
+
const _pageLeft = _left + pageXOffset;
|
|
72
|
+
const _pageTop = _top + pageYOffset;
|
|
73
|
+
|
|
74
|
+
left.push(_pageLeft);
|
|
75
|
+
top.push(_pageTop);
|
|
76
|
+
width.push(_width);
|
|
77
|
+
height.push(_height);
|
|
78
|
+
vLeft.push(_left);
|
|
79
|
+
vTop.push(_top);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
if (callbackRef) {
|
|
83
|
+
callbackRef.current({
|
|
84
|
+
left,
|
|
85
|
+
top,
|
|
86
|
+
width,
|
|
87
|
+
height,
|
|
88
|
+
vLeft,
|
|
89
|
+
vTop,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
if (_refElement) {
|
|
95
|
+
if (
|
|
96
|
+
_refElement === document.documentElement &&
|
|
97
|
+
_refElementsMultiple.length > 0
|
|
98
|
+
) {
|
|
99
|
+
_refElementsMultiple.forEach((element: any) => {
|
|
100
|
+
resizeObserverMultiple.observe(element.current);
|
|
101
|
+
});
|
|
102
|
+
} else {
|
|
103
|
+
resizeObserver.observe(_refElement);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return () => {
|
|
108
|
+
if (_refElement) {
|
|
109
|
+
if (
|
|
110
|
+
_refElement === document.documentElement &&
|
|
111
|
+
_refElementsMultiple.length > 0
|
|
112
|
+
) {
|
|
113
|
+
_refElementsMultiple.forEach((element: any) => {
|
|
114
|
+
resizeObserverMultiple.unobserve(element.current);
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
resizeObserver.unobserve(_refElement);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}, []);
|
|
122
|
+
|
|
123
|
+
return (index?: number) => {
|
|
124
|
+
if (index === null || index === undefined) {
|
|
125
|
+
return { ref };
|
|
126
|
+
} else {
|
|
127
|
+
elementRefs.current[index] =
|
|
128
|
+
elementRefs.current[index] || React.createRef();
|
|
129
|
+
|
|
130
|
+
return { ref: elementRefs.current[index] };
|
|
131
|
+
}
|
|
132
|
+
}; // ...bind() or ...bind(index) for multiple
|
|
133
|
+
}
|