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.
Files changed (52) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/LICENSE +21 -21
  3. package/README.md +115 -115
  4. package/dist/animation/animationType.d.ts +15 -0
  5. package/dist/animation/getInitialConfig.d.ts +3 -3
  6. package/dist/animation/index.d.ts +5 -4
  7. package/dist/animation/interpolation.d.ts +3 -11
  8. package/dist/animation/modules.d.ts +18 -10
  9. package/dist/animation/useAnimatedValue.d.ts +11 -23
  10. package/dist/animation/useMountedValue.d.ts +5 -14
  11. package/dist/gestures/controllers/DragGesture.d.ts +2 -2
  12. package/dist/index.d.ts +5 -4
  13. package/dist/index.js +102 -114
  14. package/dist/index.js.map +1 -1
  15. package/dist/utils/delay.d.ts +5 -0
  16. package/dist/utils/index.d.ts +2 -1
  17. package/package.json +49 -49
  18. package/rollup.config.js +18 -18
  19. package/src/animation/animationType.ts +17 -0
  20. package/src/animation/getInitialConfig.ts +61 -31
  21. package/src/animation/index.ts +9 -4
  22. package/src/animation/interpolation.ts +24 -57
  23. package/src/animation/modules.tsx +105 -105
  24. package/src/animation/useAnimatedValue.ts +62 -132
  25. package/src/animation/useMountedValue.ts +66 -82
  26. package/src/gestures/controllers/DragGesture.ts +177 -176
  27. package/src/gestures/controllers/Gesture.ts +54 -54
  28. package/src/gestures/controllers/MouseMoveGesture.ts +111 -111
  29. package/src/gestures/controllers/ScrollGesture.ts +107 -107
  30. package/src/gestures/controllers/WheelGesture.ts +123 -123
  31. package/src/gestures/controllers/index.ts +4 -4
  32. package/src/gestures/eventAttacher.ts +67 -67
  33. package/src/gestures/hooks/index.ts +5 -5
  34. package/src/gestures/hooks/useDrag.ts +14 -14
  35. package/src/gestures/hooks/useGesture.ts +38 -38
  36. package/src/gestures/hooks/useMouseMove.ts +11 -11
  37. package/src/gestures/hooks/useRecognizer.ts +59 -59
  38. package/src/gestures/hooks/useScroll.ts +11 -11
  39. package/src/gestures/hooks/useWheel.ts +11 -11
  40. package/src/gestures/index.ts +2 -2
  41. package/src/gestures/math.ts +120 -120
  42. package/src/gestures/types.ts +49 -49
  43. package/src/gestures/withDefault.ts +3 -3
  44. package/src/hooks/index.ts +3 -3
  45. package/src/hooks/useMeasure.ts +133 -133
  46. package/src/hooks/useOutsideClick.ts +36 -36
  47. package/src/hooks/useWindowDimension.ts +59 -59
  48. package/src/index.ts +5 -4
  49. package/src/utils/delay.ts +9 -0
  50. package/src/utils/index.ts +2 -1
  51. package/src/utils/isDefined.ts +4 -4
  52. package/tsconfig.json +25 -25
@@ -1,31 +1,61 @@
1
- import { Easing } from "@raidipesh78/re-motion";
2
- import { GenericAnimationConfig } from "./useAnimatedValue";
3
- export type InitialConfigType =
4
- | "ease"
5
- | "elastic"
6
- | "stiff"
7
- | "wooble"
8
- | "bounce"
9
- | undefined;
10
-
11
- export const getInitialConfig = (
12
- animationType: InitialConfigType
13
- ): GenericAnimationConfig => {
14
- switch (animationType) {
15
- case "elastic":
16
- return { mass: 1, friction: 18, tension: 250 };
17
-
18
- case "stiff":
19
- return { mass: 1, friction: 18, tension: 350 };
20
-
21
- case "wooble":
22
- return { mass: 1, friction: 8, tension: 250 };
23
-
24
- case "bounce":
25
- return { duration: 500, easing: Easing.bounce };
26
-
27
- case "ease":
28
- default:
29
- return { mass: 1, friction: 26, tension: 170 };
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
+ };
@@ -1,4 +1,9 @@
1
- export * from "./interpolation";
2
- export * from "./modules";
3
- export { useAnimatedValue } from "./useAnimatedValue";
4
- export { useMountedValue } from "./useMountedValue";
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
- interpolateNumbers,
3
- interpolateTransitionValue,
4
- ExtrapolateConfig,
5
- isSubscriber,
6
- } from "@raidipesh78/re-motion";
7
-
8
- /**
9
- * interpolate function maps input range to output range
10
- * @param value - number | TransitionValue
11
- * @param inputRange - Array<number>
12
- * @param outputRange - Array<string | number>
13
- * @param extrapolateConfig - "clamp" | "identity" | "extend"
14
- * @returns - number | TransitionValue
15
- */
16
- export function interpolate(
17
- value: any,
18
- inputRange: Array<number>,
19
- outputRange: Array<number | string>,
20
- extrapolateConfig?: ExtrapolateConfig
21
- ) {
22
- if (typeof value === "object" && isSubscriber(value)) {
23
- return interpolateTransitionValue(
24
- value,
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 "react";
2
- import {
3
- makeAnimatedComponent as animated,
4
- TransitionValue,
5
- } from "@raidipesh78/re-motion";
6
- import { useAnimatedValue, UseAnimatedValueConfig } from "./useAnimatedValue";
7
- import { useMountedValue, InnerUseMountedValueConfig } 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?: InnerUseMountedValueConfig;
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, { from: 0, enter: 1, exit: 0, config });
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 * as React from "react";
2
- import {
3
- useTransition,
4
- TransitionValue,
5
- ResultType,
6
- } from "@raidipesh78/re-motion";
7
- import { InitialConfigType, getInitialConfig } from "./getInitialConfig";
8
-
9
- // useAnimatedValue value type
10
- type AnimatedValueType = number | string;
11
-
12
- /**
13
- * getValue checks for type of initialValue and throws error
14
- * for type other than AnimatedValueType
15
- */
16
- const getValue = (value: AnimatedValueType) => {
17
- if (typeof value === "number" || typeof value === "string") {
18
- return value;
19
- } else {
20
- throw new Error(
21
- "Invalid Value! Animated value only accepts string or number."
22
- );
23
- }
24
- };
25
-
26
- // General config type
27
- export interface GenericAnimationConfig {
28
- duration?: number;
29
- mass?: number;
30
- friction?: number;
31
- tension?: number;
32
- easing?: (t: number) => number;
33
- delay?: number;
34
- }
35
-
36
- export interface UseAnimatedValueConfig extends GenericAnimationConfig {
37
- animationType?: InitialConfigType;
38
- onAnimationEnd?: (value: ResultType) => void;
39
- listener?: (value: number) => void;
40
- immediate?: boolean;
41
- }
42
-
43
- export type ValueReturnType =
44
- | TransitionValue
45
- | number
46
- | string
47
- | { toValue: number | string; immediate?: boolean };
48
-
49
- interface UseAnimatedValueReturn {
50
- value: ValueReturnType;
51
- currentValue: number | string;
52
- }
53
-
54
- /**
55
- * useAnimatedValue for animated transitions
56
- */
57
- export function useAnimatedValue(
58
- initialValue: AnimatedValueType,
59
- config?: UseAnimatedValueConfig
60
- ): UseAnimatedValueReturn {
61
- const _isInitial = React.useRef(true);
62
- const _initialValue: number | string = getValue(initialValue);
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
+ }