react-ui-animate 1.4.5 → 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 -0
- package/LICENSE +21 -21
- package/README.md +115 -115
- package/dist/animation/animationType.d.ts +15 -0
- package/dist/animation/getInitialConfig.d.ts +3 -3
- package/dist/animation/index.d.ts +5 -4
- package/dist/animation/interpolation.d.ts +3 -11
- package/dist/animation/modules.d.ts +18 -10
- package/dist/animation/useAnimatedValue.d.ts +11 -23
- package/dist/animation/useMountedValue.d.ts +5 -14
- package/dist/gestures/controllers/DragGesture.d.ts +2 -2
- package/dist/index.d.ts +5 -4
- package/dist/index.js +102 -114
- package/dist/index.js.map +1 -1
- package/dist/utils/delay.d.ts +5 -0
- package/dist/utils/index.d.ts +2 -1
- package/package.json +49 -49
- package/rollup.config.js +18 -18
- package/src/animation/animationType.ts +17 -0
- package/src/animation/getInitialConfig.ts +61 -31
- package/src/animation/index.ts +9 -4
- package/src/animation/interpolation.ts +24 -57
- package/src/animation/modules.tsx +105 -105
- package/src/animation/useAnimatedValue.ts +62 -132
- package/src/animation/useMountedValue.ts +66 -82
- package/src/gestures/controllers/DragGesture.ts +177 -176
- 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 -0
- package/src/utils/index.ts +2 -1
- package/src/utils/isDefined.ts +4 -4
- package/tsconfig.json +25 -25
|
@@ -1,31 +1,61 @@
|
|
|
1
|
-
import { Easing } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
|
5
|
-
|
|
|
6
|
-
|
|
|
7
|
-
|
|
|
8
|
-
|
|
|
9
|
-
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
case
|
|
22
|
-
return { mass: 1, friction:
|
|
23
|
-
|
|
24
|
-
case
|
|
25
|
-
return {
|
|
26
|
-
|
|
27
|
-
case
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
};
|
|
1
|
+
import { Easing, UseTransitionConfig } from '@raidipesh78/re-motion';
|
|
2
|
+
export type InitialConfigType =
|
|
3
|
+
| 'linear'
|
|
4
|
+
| 'easein'
|
|
5
|
+
| 'easeout'
|
|
6
|
+
| 'easeinout'
|
|
7
|
+
| 'ease'
|
|
8
|
+
| 'power1'
|
|
9
|
+
| 'power2'
|
|
10
|
+
| 'power3'
|
|
11
|
+
| 'power4'
|
|
12
|
+
| 'elastic'
|
|
13
|
+
| 'stiff'
|
|
14
|
+
| 'wooble'
|
|
15
|
+
| 'bounce';
|
|
16
|
+
|
|
17
|
+
export const getInitialConfig = (
|
|
18
|
+
animationType?: InitialConfigType
|
|
19
|
+
): UseTransitionConfig => {
|
|
20
|
+
switch (animationType) {
|
|
21
|
+
case 'elastic':
|
|
22
|
+
return { mass: 1, friction: 18, tension: 250 };
|
|
23
|
+
|
|
24
|
+
case 'stiff':
|
|
25
|
+
return { mass: 1, friction: 18, tension: 350 };
|
|
26
|
+
|
|
27
|
+
case 'wooble':
|
|
28
|
+
return { mass: 1, friction: 8, tension: 250 };
|
|
29
|
+
|
|
30
|
+
case 'bounce':
|
|
31
|
+
return { duration: 500, easing: Easing.bounce };
|
|
32
|
+
|
|
33
|
+
case 'power1':
|
|
34
|
+
return { duration: 500, easing: Easing.bezier(0.17, 0.42, 0.51, 0.97) };
|
|
35
|
+
|
|
36
|
+
case 'power2':
|
|
37
|
+
return { duration: 500, easing: Easing.bezier(0.07, 0.11, 0.13, 1) };
|
|
38
|
+
|
|
39
|
+
case 'power3':
|
|
40
|
+
return { duration: 500, easing: Easing.bezier(0.09, 0.7, 0.16, 1.04) };
|
|
41
|
+
|
|
42
|
+
case 'power4':
|
|
43
|
+
return { duration: 500, easing: Easing.bezier(0.05, 0.54, 0, 1.03) };
|
|
44
|
+
|
|
45
|
+
case 'linear':
|
|
46
|
+
return { duration: 500, easing: Easing.linear };
|
|
47
|
+
|
|
48
|
+
case 'easein':
|
|
49
|
+
return { duration: 500, easing: Easing.in(Easing.ease) };
|
|
50
|
+
|
|
51
|
+
case 'easeout':
|
|
52
|
+
return { duration: 500, easing: Easing.out(Easing.ease) };
|
|
53
|
+
|
|
54
|
+
case 'easeinout':
|
|
55
|
+
return { duration: 500, easing: Easing.inOut(Easing.ease) };
|
|
56
|
+
|
|
57
|
+
case 'ease':
|
|
58
|
+
default:
|
|
59
|
+
return { mass: 1, friction: 26, tension: 170 };
|
|
60
|
+
}
|
|
61
|
+
};
|
package/src/animation/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export {
|
|
4
|
-
|
|
1
|
+
export * from './interpolation';
|
|
2
|
+
export * from './modules';
|
|
3
|
+
export {
|
|
4
|
+
useAnimatedValue,
|
|
5
|
+
ValueType,
|
|
6
|
+
UseAnimatedValueConfig,
|
|
7
|
+
} from './useAnimatedValue';
|
|
8
|
+
export { useMountedValue } from './useMountedValue';
|
|
9
|
+
export * from './animationType';
|
|
@@ -1,57 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @param
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
14
|
-
* @
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
inputRange,
|
|
26
|
-
outputRange,
|
|
27
|
-
extrapolateConfig
|
|
28
|
-
);
|
|
29
|
-
} else if (typeof value === "number") {
|
|
30
|
-
return interpolateNumbers(
|
|
31
|
-
value,
|
|
32
|
-
inputRange,
|
|
33
|
-
outputRange,
|
|
34
|
-
extrapolateConfig
|
|
35
|
-
);
|
|
36
|
-
} else {
|
|
37
|
-
throw new Error(`Error! ${typeof value} cannot be interpolated`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* bInterpolate functions maps input range [0, 1] to given [minOutput, maxOutput]
|
|
43
|
-
* sorthand function to interpolate input range [0, 1]
|
|
44
|
-
* @param value - number | TransitionValue
|
|
45
|
-
* @param minOutput - number | string
|
|
46
|
-
* @param maxOutput - number | string
|
|
47
|
-
* @param extrapolateConfig - "clamp" | "identity" | "extend"
|
|
48
|
-
* @returns - number | TransitionValue
|
|
49
|
-
*/
|
|
50
|
-
export function bInterpolate(
|
|
51
|
-
value: any,
|
|
52
|
-
minOutput: number | string,
|
|
53
|
-
maxOutput: number | string,
|
|
54
|
-
extrapolateConfig?: ExtrapolateConfig
|
|
55
|
-
) {
|
|
56
|
-
return interpolate(value, [0, 1], [minOutput, maxOutput], extrapolateConfig);
|
|
57
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
interpolate,
|
|
3
|
+
ExtrapolateConfig,
|
|
4
|
+
TransitionValue,
|
|
5
|
+
} from '@raidipesh78/re-motion';
|
|
6
|
+
export { interpolate } from '@raidipesh78/re-motion';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* bInterpolate functions maps input range [0, 1] to given [minOutput, maxOutput]
|
|
10
|
+
* sorthand function to interpolate input range [0, 1]
|
|
11
|
+
* @param value - number | TransitionValue
|
|
12
|
+
* @param minOutput - number | string
|
|
13
|
+
* @param maxOutput - number | string
|
|
14
|
+
* @param extrapolateConfig - "clamp" | "identity" | "extend"
|
|
15
|
+
* @returns - number | TransitionValue
|
|
16
|
+
*/
|
|
17
|
+
export function bInterpolate(
|
|
18
|
+
value: number | TransitionValue,
|
|
19
|
+
minOutput: number | string,
|
|
20
|
+
maxOutput: number | string,
|
|
21
|
+
extrapolateConfig?: ExtrapolateConfig
|
|
22
|
+
) {
|
|
23
|
+
return interpolate(value, [0, 1], [minOutput, maxOutput], extrapolateConfig);
|
|
24
|
+
}
|
|
@@ -1,105 +1,105 @@
|
|
|
1
|
-
import * as React from
|
|
2
|
-
import {
|
|
3
|
-
makeAnimatedComponent as animated,
|
|
4
|
-
TransitionValue,
|
|
5
|
-
} from
|
|
6
|
-
import { useAnimatedValue, UseAnimatedValueConfig } from
|
|
7
|
-
import { useMountedValue,
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Make any component animatable
|
|
11
|
-
*/
|
|
12
|
-
export function makeAnimatedComponent(
|
|
13
|
-
WrappedComponent: React.ElementType<any>
|
|
14
|
-
) {
|
|
15
|
-
return animated(WrappedComponent);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* AnimatedBlock : Animated Div
|
|
20
|
-
*/
|
|
21
|
-
export const AnimatedBlock = animated(
|
|
22
|
-
/**
|
|
23
|
-
* AnimatedInline : Animated Span
|
|
24
|
-
*/
|
|
25
|
-
export const AnimatedInline = animated(
|
|
26
|
-
/**
|
|
27
|
-
* AnimatedImage : Animated Image
|
|
28
|
-
*/
|
|
29
|
-
export const AnimatedImage = animated(
|
|
30
|
-
interface ScrollableBlockProps {
|
|
31
|
-
children?: (animation: any) => React.ReactNode;
|
|
32
|
-
direction?:
|
|
33
|
-
threshold?: number;
|
|
34
|
-
animationConfig?: UseAnimatedValueConfig;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* ScrollableBlock
|
|
39
|
-
* Used to animate element when enter into viewport
|
|
40
|
-
* Render props pattern with children accepts animation node
|
|
41
|
-
* animated value goes from 0 to 1 when appear on viewport & vice versa.
|
|
42
|
-
*/
|
|
43
|
-
export const ScrollableBlock: React.FC<ScrollableBlockProps> = (props) => {
|
|
44
|
-
const {
|
|
45
|
-
children,
|
|
46
|
-
direction =
|
|
47
|
-
animationConfig,
|
|
48
|
-
threshold = 0.2,
|
|
49
|
-
} = props;
|
|
50
|
-
const scrollableBlockRef = React.useRef<HTMLDivElement>(null);
|
|
51
|
-
const animation = useAnimatedValue(0, animationConfig); // 0: not intersecting | 1: intersecting
|
|
52
|
-
|
|
53
|
-
React.useEffect(() => {
|
|
54
|
-
const _scrollableBlock = scrollableBlockRef.current;
|
|
55
|
-
|
|
56
|
-
const observer = new IntersectionObserver(
|
|
57
|
-
function ([entry]) {
|
|
58
|
-
const { isIntersecting } = entry;
|
|
59
|
-
|
|
60
|
-
if (isIntersecting) {
|
|
61
|
-
animation.value = 1;
|
|
62
|
-
} else {
|
|
63
|
-
if (direction ===
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
root: null, // FOR VIEWPORT ONLY
|
|
68
|
-
threshold,
|
|
69
|
-
}
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
if (_scrollableBlock) {
|
|
73
|
-
observer.observe(_scrollableBlock);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return () => {
|
|
77
|
-
if (_scrollableBlock) {
|
|
78
|
-
observer.unobserve(_scrollableBlock);
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
}, []);
|
|
82
|
-
|
|
83
|
-
return <div ref={scrollableBlockRef}>{children && children(animation)}</div>;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
interface MountedBlockProps {
|
|
87
|
-
state: boolean;
|
|
88
|
-
children: (animation: { value: TransitionValue }) => React.ReactNode;
|
|
89
|
-
config
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* MountedBlock handles mounting and unmounting of a component
|
|
94
|
-
* @props - state: boolean, config: InnerUseMountedValueConfig
|
|
95
|
-
* @children - (animation: { value: TransitionValue }) => React.ReactNode
|
|
96
|
-
*/
|
|
97
|
-
export const MountedBlock = ({
|
|
98
|
-
state,
|
|
99
|
-
children,
|
|
100
|
-
config,
|
|
101
|
-
}: MountedBlockProps) => {
|
|
102
|
-
const open = useMountedValue(state,
|
|
103
|
-
|
|
104
|
-
return <>{open((animation, mounted) => mounted && children(animation))}</>;
|
|
105
|
-
};
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
makeAnimatedComponent as animated,
|
|
4
|
+
TransitionValue,
|
|
5
|
+
} from '@raidipesh78/re-motion';
|
|
6
|
+
import { useAnimatedValue, UseAnimatedValueConfig } from './useAnimatedValue';
|
|
7
|
+
import { useMountedValue, UseMountedValueConfig } from './useMountedValue';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Make any component animatable
|
|
11
|
+
*/
|
|
12
|
+
export function makeAnimatedComponent(
|
|
13
|
+
WrappedComponent: React.ElementType<any>
|
|
14
|
+
) {
|
|
15
|
+
return animated(WrappedComponent);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* AnimatedBlock : Animated Div
|
|
20
|
+
*/
|
|
21
|
+
export const AnimatedBlock = animated('div');
|
|
22
|
+
/**
|
|
23
|
+
* AnimatedInline : Animated Span
|
|
24
|
+
*/
|
|
25
|
+
export const AnimatedInline = animated('span');
|
|
26
|
+
/**
|
|
27
|
+
* AnimatedImage : Animated Image
|
|
28
|
+
*/
|
|
29
|
+
export const AnimatedImage = animated('img');
|
|
30
|
+
interface ScrollableBlockProps {
|
|
31
|
+
children?: (animation: any) => React.ReactNode;
|
|
32
|
+
direction?: 'single' | 'both';
|
|
33
|
+
threshold?: number;
|
|
34
|
+
animationConfig?: UseAnimatedValueConfig;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* ScrollableBlock
|
|
39
|
+
* Used to animate element when enter into viewport
|
|
40
|
+
* Render props pattern with children accepts animation node
|
|
41
|
+
* animated value goes from 0 to 1 when appear on viewport & vice versa.
|
|
42
|
+
*/
|
|
43
|
+
export const ScrollableBlock: React.FC<ScrollableBlockProps> = (props) => {
|
|
44
|
+
const {
|
|
45
|
+
children,
|
|
46
|
+
direction = 'single',
|
|
47
|
+
animationConfig,
|
|
48
|
+
threshold = 0.2,
|
|
49
|
+
} = props;
|
|
50
|
+
const scrollableBlockRef = React.useRef<HTMLDivElement>(null);
|
|
51
|
+
const animation = useAnimatedValue(0, animationConfig); // 0: not intersecting | 1: intersecting
|
|
52
|
+
|
|
53
|
+
React.useEffect(() => {
|
|
54
|
+
const _scrollableBlock = scrollableBlockRef.current;
|
|
55
|
+
|
|
56
|
+
const observer = new IntersectionObserver(
|
|
57
|
+
function ([entry]) {
|
|
58
|
+
const { isIntersecting } = entry;
|
|
59
|
+
|
|
60
|
+
if (isIntersecting) {
|
|
61
|
+
animation.value = 1;
|
|
62
|
+
} else {
|
|
63
|
+
if (direction === 'both') animation.value = 0;
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
root: null, // FOR VIEWPORT ONLY
|
|
68
|
+
threshold,
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (_scrollableBlock) {
|
|
73
|
+
observer.observe(_scrollableBlock);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return () => {
|
|
77
|
+
if (_scrollableBlock) {
|
|
78
|
+
observer.unobserve(_scrollableBlock);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}, []);
|
|
82
|
+
|
|
83
|
+
return <div ref={scrollableBlockRef}>{children && children(animation)}</div>;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
interface MountedBlockProps {
|
|
87
|
+
state: boolean;
|
|
88
|
+
children: (animation: { value: TransitionValue }) => React.ReactNode;
|
|
89
|
+
config: UseMountedValueConfig;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* MountedBlock handles mounting and unmounting of a component
|
|
94
|
+
* @props - state: boolean, config: InnerUseMountedValueConfig
|
|
95
|
+
* @children - (animation: { value: TransitionValue }) => React.ReactNode
|
|
96
|
+
*/
|
|
97
|
+
export const MountedBlock = ({
|
|
98
|
+
state,
|
|
99
|
+
children,
|
|
100
|
+
config,
|
|
101
|
+
}: MountedBlockProps) => {
|
|
102
|
+
const open = useMountedValue(state, config);
|
|
103
|
+
|
|
104
|
+
return <>{open((animation, mounted) => mounted && children(animation))}</>;
|
|
105
|
+
};
|
|
@@ -1,132 +1,62 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const animationType = config?.animationType ?? "ease"; // Defines default animation
|
|
65
|
-
const onAnimationEnd = config?.onAnimationEnd;
|
|
66
|
-
const listener = config?.listener;
|
|
67
|
-
|
|
68
|
-
const [animation, setAnimation] = useTransition(_initialValue, {
|
|
69
|
-
...getInitialConfig(animationType),
|
|
70
|
-
...config,
|
|
71
|
-
onRest: function (result: any) {
|
|
72
|
-
if (result.finished) {
|
|
73
|
-
onAnimationEnd && onAnimationEnd(result.value);
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
onChange: function (value: number) {
|
|
77
|
-
listener && listener(value);
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// doesn't fire on initial render
|
|
82
|
-
React.useEffect(() => {
|
|
83
|
-
if (!_isInitial.current) {
|
|
84
|
-
setAnimation({ toValue: _initialValue });
|
|
85
|
-
}
|
|
86
|
-
_isInitial.current = false;
|
|
87
|
-
}, [_initialValue]);
|
|
88
|
-
|
|
89
|
-
const targetObject: {
|
|
90
|
-
value: any;
|
|
91
|
-
currentValue: string | number;
|
|
92
|
-
} = {
|
|
93
|
-
value: animation,
|
|
94
|
-
currentValue: animation.get(),
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
return new Proxy(targetObject, {
|
|
98
|
-
set: function (
|
|
99
|
-
_,
|
|
100
|
-
key,
|
|
101
|
-
value: number | string | { toValue: number | string; immediate?: boolean }
|
|
102
|
-
) {
|
|
103
|
-
if (key === "value") {
|
|
104
|
-
if (typeof value === "number" || typeof value === "string") {
|
|
105
|
-
setAnimation({ toValue: value });
|
|
106
|
-
} else if (typeof value === "object") {
|
|
107
|
-
setAnimation({
|
|
108
|
-
toValue: value.toValue,
|
|
109
|
-
config: { immediate: value.immediate },
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
throw new Error("You cannot set any other property to animation node.");
|
|
117
|
-
},
|
|
118
|
-
get: function (_, key) {
|
|
119
|
-
if (key === "value") {
|
|
120
|
-
return animation;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (key === "currentValue") {
|
|
124
|
-
return animation.get();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
throw new Error(
|
|
128
|
-
"You cannot access any other property from animation node."
|
|
129
|
-
);
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
}
|
|
1
|
+
import { useTransition, UseTransitionConfig } from '@raidipesh78/re-motion';
|
|
2
|
+
|
|
3
|
+
// useAnimatedValue value type
|
|
4
|
+
type AnimatedValueType = number | string;
|
|
5
|
+
export interface UseAnimatedValueConfig extends UseTransitionConfig {}
|
|
6
|
+
|
|
7
|
+
type Length = number | string;
|
|
8
|
+
type AssignValue = {
|
|
9
|
+
toValue: Length;
|
|
10
|
+
config?: UseAnimatedValueConfig;
|
|
11
|
+
};
|
|
12
|
+
export type ValueType =
|
|
13
|
+
| Length
|
|
14
|
+
| AssignValue
|
|
15
|
+
| ((update: (next: AssignValue) => Promise<any>) => void);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* useAnimatedValue for animated transitions
|
|
19
|
+
*/
|
|
20
|
+
export function useAnimatedValue(
|
|
21
|
+
initialValue: AnimatedValueType,
|
|
22
|
+
config?: UseAnimatedValueConfig
|
|
23
|
+
) {
|
|
24
|
+
const [animation, setAnimation] = useTransition(initialValue, config);
|
|
25
|
+
|
|
26
|
+
const targetObject: {
|
|
27
|
+
value: any;
|
|
28
|
+
currentValue: number | string;
|
|
29
|
+
} = {
|
|
30
|
+
value: animation,
|
|
31
|
+
currentValue: animation.get(),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return new Proxy(targetObject, {
|
|
35
|
+
set: function (_, key, value: ValueType) {
|
|
36
|
+
if (key === 'value') {
|
|
37
|
+
if (typeof value === 'number' || typeof value === 'string') {
|
|
38
|
+
setAnimation({ toValue: value });
|
|
39
|
+
} else if (typeof value === 'object' || typeof value === 'function') {
|
|
40
|
+
setAnimation(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
throw new Error('You cannot set any other property to animation node.');
|
|
47
|
+
},
|
|
48
|
+
get: function (_, key) {
|
|
49
|
+
if (key === 'value') {
|
|
50
|
+
return animation;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (key === 'currentValue') {
|
|
54
|
+
return animation.get();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
throw new Error(
|
|
58
|
+
'You cannot access any other property from animation node.'
|
|
59
|
+
);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|