motion-on-native 1.0.7 → 1.1.0

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 (4) hide show
  1. package/LICENSE +20 -21
  2. package/lib/index.d.ts +53 -19
  3. package/lib/index.js +183 -272
  4. package/package.json +54 -54
package/LICENSE CHANGED
@@ -1,21 +1,20 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Mohd Bilal
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Mohd Bilal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/lib/index.d.ts CHANGED
@@ -1,31 +1,68 @@
1
1
  import React from 'react';
2
- import { ViewStyle } from 'react-native';
2
+ import { View, ViewStyle } from 'react-native';
3
3
  export interface AnimationProps {
4
4
  opacity?: number;
5
5
  x?: number;
6
6
  y?: number;
7
+ z?: number;
7
8
  scale?: number;
9
+ scaleX?: number;
10
+ scaleY?: number;
8
11
  rotate?: string;
12
+ rotateX?: string;
13
+ rotateY?: string;
14
+ rotateZ?: string;
15
+ skewX?: string;
16
+ skewY?: string;
9
17
  width?: number;
10
18
  height?: number;
11
- borderRadius?: number;
12
- backgroundColor?: string;
19
+ minWidth?: number;
20
+ minHeight?: number;
21
+ maxWidth?: number;
22
+ maxHeight?: number;
13
23
  margin?: number;
14
24
  marginTop?: number;
15
25
  marginBottom?: number;
16
26
  marginLeft?: number;
17
27
  marginRight?: number;
28
+ marginHorizontal?: number;
29
+ marginVertical?: number;
18
30
  padding?: number;
19
31
  paddingTop?: number;
20
32
  paddingBottom?: number;
21
33
  paddingLeft?: number;
22
34
  paddingRight?: number;
35
+ paddingHorizontal?: number;
36
+ paddingVertical?: number;
37
+ borderRadius?: number;
38
+ borderTopLeftRadius?: number;
39
+ borderTopRightRadius?: number;
40
+ borderBottomLeftRadius?: number;
41
+ borderBottomRightRadius?: number;
23
42
  borderWidth?: number;
43
+ borderTopWidth?: number;
44
+ borderBottomWidth?: number;
45
+ borderLeftWidth?: number;
46
+ borderRightWidth?: number;
24
47
  borderColor?: string;
48
+ borderTopColor?: string;
49
+ borderBottomColor?: string;
50
+ borderLeftColor?: string;
51
+ borderRightColor?: string;
52
+ backgroundColor?: string;
53
+ color?: string;
25
54
  top?: number;
26
55
  bottom?: number;
27
56
  left?: number;
28
57
  right?: number;
58
+ shadowColor?: string;
59
+ shadowOffset?: {
60
+ width: number;
61
+ height: number;
62
+ };
63
+ shadowOpacity?: number;
64
+ shadowRadius?: number;
65
+ elevation?: number;
29
66
  }
30
67
  export interface TransitionProps {
31
68
  type?: 'spring' | 'timing';
@@ -33,31 +70,28 @@ export interface TransitionProps {
33
70
  damping?: number;
34
71
  stiffness?: number;
35
72
  mass?: number;
73
+ delay?: number;
74
+ ease?: string;
36
75
  }
37
76
  export interface MotionComponentProps {
38
- initial?: AnimationProps;
77
+ initial?: AnimationProps | false;
39
78
  animate?: AnimationProps;
40
79
  exit?: AnimationProps;
41
80
  transition?: TransitionProps;
42
81
  shouldExit?: boolean;
43
82
  onExitComplete?: () => void;
83
+ whileHover?: AnimationProps;
84
+ whileTap?: AnimationProps;
85
+ whileFocus?: AnimationProps;
86
+ layout?: boolean;
87
+ layoutId?: string;
44
88
  style?: ViewStyle;
45
89
  children?: React.ReactNode;
46
90
  }
47
91
  export declare const NativeMotion: {
48
- View: (props: MotionComponentProps & import("react-native").ViewProps) => React.FunctionComponentElement<{
49
- style: any[];
50
- } & Omit<MotionComponentProps & import("react-native").ViewProps, "style" | "animate" | "initial" | "exit" | "transition" | "children" | "shouldExit" | "onExitComplete">> | null;
51
- Text: (props: MotionComponentProps & import("react-native").TextProps) => React.FunctionComponentElement<{
52
- style: any[];
53
- } & Omit<MotionComponentProps & import("react-native").TextProps, "style" | "animate" | "initial" | "exit" | "transition" | "children" | "shouldExit" | "onExitComplete">> | null;
54
- Image: (props: MotionComponentProps & import("react-native").ImageProps) => React.FunctionComponentElement<{
55
- style: any[];
56
- } & Omit<MotionComponentProps & import("react-native").ImageProps, "style" | "animate" | "initial" | "exit" | "transition" | "children" | "shouldExit" | "onExitComplete">> | null;
57
- TextInput: (props: MotionComponentProps & import("react-native").TextInputProps) => React.FunctionComponentElement<{
58
- style: any[];
59
- } & Omit<MotionComponentProps & import("react-native").TextInputProps, "style" | "animate" | "initial" | "exit" | "transition" | "children" | "shouldExit" | "onExitComplete">> | null;
60
- TouchableOpacity: (props: MotionComponentProps & import("react-native").TouchableOpacityProps) => React.FunctionComponentElement<{
61
- style: any[];
62
- } & Omit<MotionComponentProps & import("react-native").TouchableOpacityProps, "style" | "animate" | "initial" | "exit" | "transition" | "children" | "shouldExit" | "onExitComplete">> | null;
92
+ View: (props: MotionComponentProps & import("react-native").ViewProps) => React.ReactElement<import("react-native-reanimated").AnimateProps<any>, string | React.JSXElementConstructor<any>> | null;
93
+ Text: (props: MotionComponentProps & import("react-native").TextProps) => React.ReactElement<import("react-native-reanimated").AnimateProps<any>, string | React.JSXElementConstructor<any>> | null;
94
+ Image: (props: MotionComponentProps & import("react-native").ImageProps) => React.ReactElement<import("react-native-reanimated").AnimateProps<any>, string | React.JSXElementConstructor<any>> | null;
95
+ TextInput: (props: MotionComponentProps & import("react-native").TextInputProps) => React.ReactElement<import("react-native-reanimated").AnimateProps<any>, string | React.JSXElementConstructor<any>> | null;
96
+ TouchableOpacity: (props: MotionComponentProps & import("react-native").TouchableOpacityProps & React.RefAttributes<View>) => React.ReactElement<import("react-native-reanimated").AnimateProps<any>, string | React.JSXElementConstructor<any>> | null;
63
97
  };
package/lib/index.js CHANGED
@@ -47,292 +47,172 @@ Object.defineProperty(exports, "__esModule", { value: true });
47
47
  exports.NativeMotion = void 0;
48
48
  const react_1 = __importStar(require("react"));
49
49
  const react_native_1 = require("react-native");
50
- // @ts-ignore
51
50
  const react_native_reanimated_1 = __importStar(require("react-native-reanimated"));
51
+ const DEFAULT_TRANSITION = {
52
+ type: 'spring',
53
+ damping: 15,
54
+ stiffness: 100,
55
+ duration: 300,
56
+ };
52
57
  function createMotionComponent(Component) {
53
58
  return function MotionComponent(props) {
54
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
55
- const { initial = {}, animate = {}, exit = {}, transition = {}, style = {}, children, shouldExit = false, onExitComplete = () => { } } = props, rest = __rest(props, ["initial", "animate", "exit", "transition", "style", "children", "shouldExit", "onExitComplete"]);
59
+ const { initial = {}, animate = {}, exit = {}, transition = DEFAULT_TRANSITION, shouldExit = false, onExitComplete, whileHover, whileTap, whileFocus, layout, layoutId, style = {}, children } = props, rest = __rest(props, ["initial", "animate", "exit", "transition", "shouldExit", "onExitComplete", "whileHover", "whileTap", "whileFocus", "layout", "layoutId", "style", "children"]);
56
60
  const [isPresent, setIsPresent] = (0, react_1.useState)(true);
57
- const [hasExited, setHasExited] = (0, react_1.useState)(false);
58
- const opacity = (0, react_native_reanimated_1.useSharedValue)((_a = initial.opacity) !== null && _a !== void 0 ? _a : 1);
59
- const translateX = (0, react_native_reanimated_1.useSharedValue)((_b = initial.x) !== null && _b !== void 0 ? _b : 0);
60
- const translateY = (0, react_native_reanimated_1.useSharedValue)((_c = initial.y) !== null && _c !== void 0 ? _c : 0);
61
- const scale = (0, react_native_reanimated_1.useSharedValue)((_d = initial.scale) !== null && _d !== void 0 ? _d : 1);
62
- const rotate = (0, react_native_reanimated_1.useSharedValue)((_e = initial.rotate) !== null && _e !== void 0 ? _e : '0deg');
63
- const width = (0, react_native_reanimated_1.useSharedValue)((_f = initial.width) !== null && _f !== void 0 ? _f : null);
64
- const height = (0, react_native_reanimated_1.useSharedValue)((_g = initial.height) !== null && _g !== void 0 ? _g : null);
65
- const borderRadius = (0, react_native_reanimated_1.useSharedValue)((_h = initial.borderRadius) !== null && _h !== void 0 ? _h : null);
66
- const backgroundColor = (0, react_native_reanimated_1.useSharedValue)((_j = initial.backgroundColor) !== null && _j !== void 0 ? _j : null);
67
- const margin = (0, react_native_reanimated_1.useSharedValue)((_k = initial.margin) !== null && _k !== void 0 ? _k : null);
68
- const marginTop = (0, react_native_reanimated_1.useSharedValue)((_l = initial.marginTop) !== null && _l !== void 0 ? _l : null);
69
- const marginBottom = (0, react_native_reanimated_1.useSharedValue)((_m = initial.marginBottom) !== null && _m !== void 0 ? _m : null);
70
- const marginLeft = (0, react_native_reanimated_1.useSharedValue)((_o = initial.marginLeft) !== null && _o !== void 0 ? _o : null);
71
- const marginRight = (0, react_native_reanimated_1.useSharedValue)((_p = initial.marginRight) !== null && _p !== void 0 ? _p : null);
72
- const padding = (0, react_native_reanimated_1.useSharedValue)((_q = initial.padding) !== null && _q !== void 0 ? _q : null);
73
- const paddingTop = (0, react_native_reanimated_1.useSharedValue)((_r = initial.paddingTop) !== null && _r !== void 0 ? _r : null);
74
- const paddingBottom = (0, react_native_reanimated_1.useSharedValue)((_s = initial.paddingBottom) !== null && _s !== void 0 ? _s : null);
75
- const paddingLeft = (0, react_native_reanimated_1.useSharedValue)((_t = initial.paddingLeft) !== null && _t !== void 0 ? _t : null);
76
- const paddingRight = (0, react_native_reanimated_1.useSharedValue)((_u = initial.paddingRight) !== null && _u !== void 0 ? _u : null);
77
- const borderWidth = (0, react_native_reanimated_1.useSharedValue)((_v = initial.borderWidth) !== null && _v !== void 0 ? _v : null);
78
- const borderColor = (0, react_native_reanimated_1.useSharedValue)((_w = initial.borderColor) !== null && _w !== void 0 ? _w : null);
79
- const top = (0, react_native_reanimated_1.useSharedValue)((_x = initial.top) !== null && _x !== void 0 ? _x : null);
80
- const bottom = (0, react_native_reanimated_1.useSharedValue)((_y = initial.bottom) !== null && _y !== void 0 ? _y : null);
81
- const left = (0, react_native_reanimated_1.useSharedValue)((_z = initial.left) !== null && _z !== void 0 ? _z : null);
82
- const right = (0, react_native_reanimated_1.useSharedValue)((_0 = initial.right) !== null && _0 !== void 0 ? _0 : null);
83
- const runAnim = (sharedVal, target) => {
84
- var _a;
85
- const config = transition.type === 'spring' ? (0, react_native_reanimated_1.withSpring)(target, transition) : (0, react_native_reanimated_1.withTiming)(target, { duration: (_a = transition.duration) !== null && _a !== void 0 ? _a : 300 });
86
- sharedVal.value = config;
61
+ const [hasAnimated, setHasAnimated] = (0, react_1.useState)(false);
62
+ const isExitingRef = (0, react_1.useRef)(false);
63
+ // Create shared values
64
+ const opacity = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('opacity', initial));
65
+ const x = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('x', initial));
66
+ const y = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('y', initial));
67
+ const scale = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('scale', initial));
68
+ const rotate = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('rotate', initial));
69
+ const width = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('width', initial));
70
+ const height = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('height', initial));
71
+ const backgroundColor = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('backgroundColor', initial));
72
+ const borderRadius = (0, react_native_reanimated_1.useSharedValue)(getInitialValue('borderRadius', initial));
73
+ // Animation helper
74
+ const animateToValues = (targetValues, transitionConfig = transition) => {
75
+ Object.entries(targetValues).forEach(([key, value]) => {
76
+ var _a, _b, _c, _d;
77
+ if (value !== undefined) {
78
+ const sharedValue = getSharedValue(key);
79
+ if (sharedValue) {
80
+ const config = transitionConfig.type === 'spring'
81
+ ? (0, react_native_reanimated_1.withSpring)(value, {
82
+ damping: (_a = transitionConfig.damping) !== null && _a !== void 0 ? _a : DEFAULT_TRANSITION.damping,
83
+ stiffness: (_b = transitionConfig.stiffness) !== null && _b !== void 0 ? _b : DEFAULT_TRANSITION.stiffness,
84
+ mass: (_c = transitionConfig.mass) !== null && _c !== void 0 ? _c : 1,
85
+ })
86
+ : (0, react_native_reanimated_1.withTiming)(value, {
87
+ duration: (_d = transitionConfig.duration) !== null && _d !== void 0 ? _d : DEFAULT_TRANSITION.duration,
88
+ });
89
+ if (transitionConfig.delay) {
90
+ setTimeout(() => {
91
+ sharedValue.value = config;
92
+ }, transitionConfig.delay);
93
+ }
94
+ else {
95
+ sharedValue.value = config;
96
+ }
97
+ }
98
+ }
99
+ });
100
+ };
101
+ // Get shared value by key
102
+ const getSharedValue = (key) => {
103
+ switch (key) {
104
+ case 'opacity': return opacity;
105
+ case 'x': return x;
106
+ case 'y': return y;
107
+ case 'scale': return scale;
108
+ case 'rotate': return rotate;
109
+ case 'width': return width;
110
+ case 'height': return height;
111
+ case 'backgroundColor': return backgroundColor;
112
+ case 'borderRadius': return borderRadius;
113
+ default: return null;
114
+ }
87
115
  };
116
+ // Mount animation: initial -> animate
88
117
  (0, react_1.useEffect)(() => {
89
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
90
- opacity.value = (_a = initial.opacity) !== null && _a !== void 0 ? _a : 1;
91
- translateX.value = (_b = initial.x) !== null && _b !== void 0 ? _b : 0;
92
- translateY.value = (_c = initial.y) !== null && _c !== void 0 ? _c : 0;
93
- scale.value = (_d = initial.scale) !== null && _d !== void 0 ? _d : 1;
94
- rotate.value = (_e = initial.rotate) !== null && _e !== void 0 ? _e : '0deg';
95
- width.value = (_f = initial.width) !== null && _f !== void 0 ? _f : null;
96
- height.value = (_g = initial.height) !== null && _g !== void 0 ? _g : null;
97
- borderRadius.value = (_h = initial.borderRadius) !== null && _h !== void 0 ? _h : null;
98
- backgroundColor.value = (_j = initial.backgroundColor) !== null && _j !== void 0 ? _j : null;
99
- margin.value = (_k = initial.margin) !== null && _k !== void 0 ? _k : null;
100
- marginTop.value = (_l = initial.marginTop) !== null && _l !== void 0 ? _l : null;
101
- marginBottom.value = (_m = initial.marginBottom) !== null && _m !== void 0 ? _m : null;
102
- marginLeft.value = (_o = initial.marginLeft) !== null && _o !== void 0 ? _o : null;
103
- marginRight.value = (_p = initial.marginRight) !== null && _p !== void 0 ? _p : null;
104
- padding.value = (_q = initial.padding) !== null && _q !== void 0 ? _q : null;
105
- paddingTop.value = (_r = initial.paddingTop) !== null && _r !== void 0 ? _r : null;
106
- paddingBottom.value = (_s = initial.paddingBottom) !== null && _s !== void 0 ? _s : null;
107
- paddingLeft.value = (_t = initial.paddingLeft) !== null && _t !== void 0 ? _t : null;
108
- paddingRight.value = (_u = initial.paddingRight) !== null && _u !== void 0 ? _u : null;
109
- borderWidth.value = (_v = initial.borderWidth) !== null && _v !== void 0 ? _v : null;
110
- borderColor.value = (_w = initial.borderColor) !== null && _w !== void 0 ? _w : null;
111
- top.value = (_x = initial.top) !== null && _x !== void 0 ? _x : null;
112
- bottom.value = (_y = initial.bottom) !== null && _y !== void 0 ? _y : null;
113
- left.value = (_z = initial.left) !== null && _z !== void 0 ? _z : null;
114
- right.value = (_0 = initial.right) !== null && _0 !== void 0 ? _0 : null;
115
- }, [initial.opacity, initial.x, initial.y, initial.scale, initial.rotate, initial.width, initial.height, initial.borderRadius, initial.backgroundColor, initial.margin, initial.marginTop, initial.marginBottom, initial.marginLeft, initial.marginRight, initial.padding, initial.paddingTop, initial.paddingBottom, initial.paddingLeft, initial.paddingRight, initial.borderWidth, initial.borderColor, initial.top, initial.bottom, initial.left, initial.right]);
118
+ if (!hasAnimated && !isExitingRef.current) {
119
+ // Set initial values immediately
120
+ if (initial !== false) {
121
+ Object.entries(initial).forEach(([key, value]) => {
122
+ const sharedValue = getSharedValue(key);
123
+ if (sharedValue && value !== undefined) {
124
+ sharedValue.value = value;
125
+ }
126
+ });
127
+ }
128
+ // Animate to target values
129
+ const timer = setTimeout(() => {
130
+ if (!isExitingRef.current) {
131
+ animateToValues(animate);
132
+ setHasAnimated(true);
133
+ }
134
+ }, 16);
135
+ return () => clearTimeout(timer);
136
+ }
137
+ return undefined;
138
+ }, []);
139
+ // Handle animate prop changes
116
140
  (0, react_1.useEffect)(() => {
117
- var _a, _b, _c, _d, _e;
118
- runAnim(opacity, (_a = animate.opacity) !== null && _a !== void 0 ? _a : opacity.value);
119
- runAnim(translateX, (_b = animate.x) !== null && _b !== void 0 ? _b : translateX.value);
120
- runAnim(translateY, (_c = animate.y) !== null && _c !== void 0 ? _c : translateY.value);
121
- runAnim(scale, (_d = animate.scale) !== null && _d !== void 0 ? _d : scale.value);
122
- runAnim(rotate, (_e = animate.rotate) !== null && _e !== void 0 ? _e : rotate.value);
123
- if (animate.width !== undefined)
124
- runAnim(width, animate.width);
125
- if (animate.height !== undefined)
126
- runAnim(height, animate.height);
127
- if (animate.borderRadius !== undefined)
128
- runAnim(borderRadius, animate.borderRadius);
129
- if (animate.backgroundColor !== undefined)
130
- runAnim(backgroundColor, animate.backgroundColor);
131
- if (animate.margin !== undefined)
132
- runAnim(margin, animate.margin);
133
- if (animate.marginTop !== undefined)
134
- runAnim(marginTop, animate.marginTop);
135
- if (animate.marginBottom !== undefined)
136
- runAnim(marginBottom, animate.marginBottom);
137
- if (animate.marginLeft !== undefined)
138
- runAnim(marginLeft, animate.marginLeft);
139
- if (animate.marginRight !== undefined)
140
- runAnim(marginRight, animate.marginRight);
141
- if (animate.padding !== undefined)
142
- runAnim(padding, animate.padding);
143
- if (animate.paddingTop !== undefined)
144
- runAnim(paddingTop, animate.paddingTop);
145
- if (animate.paddingBottom !== undefined)
146
- runAnim(paddingBottom, animate.paddingBottom);
147
- if (animate.paddingLeft !== undefined)
148
- runAnim(paddingLeft, animate.paddingLeft);
149
- if (animate.paddingRight !== undefined)
150
- runAnim(paddingRight, animate.paddingRight);
151
- if (animate.borderWidth !== undefined)
152
- runAnim(borderWidth, animate.borderWidth);
153
- if (animate.borderColor !== undefined)
154
- runAnim(borderColor, animate.borderColor);
155
- if (animate.top !== undefined)
156
- runAnim(top, animate.top);
157
- if (animate.bottom !== undefined)
158
- runAnim(bottom, animate.bottom);
159
- if (animate.left !== undefined)
160
- runAnim(left, animate.left);
161
- if (animate.right !== undefined)
162
- runAnim(right, animate.right);
163
- }, [animate.opacity, animate.x, animate.y, animate.scale, animate.rotate, animate.width, animate.height, animate.borderRadius, animate.backgroundColor, animate.margin, animate.marginTop, animate.marginBottom, animate.marginLeft, animate.marginRight, animate.padding, animate.paddingTop, animate.paddingBottom, animate.paddingLeft, animate.paddingRight, animate.borderWidth, animate.borderColor, animate.top, animate.bottom, animate.left, animate.right]);
141
+ if (hasAnimated && !isExitingRef.current) {
142
+ animateToValues(animate);
143
+ }
144
+ }, [animate]);
145
+ // Handle shouldExit
164
146
  (0, react_1.useEffect)(() => {
165
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6;
166
- if (shouldExit && !hasExited && Object.keys(exit).length > 0) {
167
- setHasExited(true);
168
- runAnim(opacity, (_a = exit.opacity) !== null && _a !== void 0 ? _a : 0);
169
- runAnim(translateX, (_b = exit.x) !== null && _b !== void 0 ? _b : translateX.value);
170
- runAnim(translateY, (_c = exit.y) !== null && _c !== void 0 ? _c : translateY.value);
171
- runAnim(scale, (_d = exit.scale) !== null && _d !== void 0 ? _d : scale.value);
172
- runAnim(rotate, (_e = exit.rotate) !== null && _e !== void 0 ? _e : rotate.value);
173
- if (exit.width !== undefined)
174
- runAnim(width, exit.width);
175
- if (exit.height !== undefined)
176
- runAnim(height, exit.height);
177
- if (exit.borderRadius !== undefined)
178
- runAnim(borderRadius, exit.borderRadius);
179
- if (exit.backgroundColor !== undefined)
180
- runAnim(backgroundColor, exit.backgroundColor);
181
- if (exit.margin !== undefined)
182
- runAnim(margin, exit.margin);
183
- if (exit.marginTop !== undefined)
184
- runAnim(marginTop, exit.marginTop);
185
- if (exit.marginBottom !== undefined)
186
- runAnim(marginBottom, exit.marginBottom);
187
- if (exit.marginLeft !== undefined)
188
- runAnim(marginLeft, exit.marginLeft);
189
- if (exit.marginRight !== undefined)
190
- runAnim(marginRight, exit.marginRight);
191
- if (exit.padding !== undefined)
192
- runAnim(padding, exit.padding);
193
- if (exit.paddingTop !== undefined)
194
- runAnim(paddingTop, exit.paddingTop);
195
- if (exit.paddingBottom !== undefined)
196
- runAnim(paddingBottom, exit.paddingBottom);
197
- if (exit.paddingLeft !== undefined)
198
- runAnim(paddingLeft, exit.paddingLeft);
199
- if (exit.paddingRight !== undefined)
200
- runAnim(paddingRight, exit.paddingRight);
201
- if (exit.borderWidth !== undefined)
202
- runAnim(borderWidth, exit.borderWidth);
203
- if (exit.borderColor !== undefined)
204
- runAnim(borderColor, exit.borderColor);
205
- if (exit.top !== undefined)
206
- runAnim(top, exit.top);
207
- if (exit.bottom !== undefined)
208
- runAnim(bottom, exit.bottom);
209
- if (exit.left !== undefined)
210
- runAnim(left, exit.left);
211
- if (exit.right !== undefined)
212
- runAnim(right, exit.right);
213
- setTimeout(() => { setIsPresent(false); onExitComplete === null || onExitComplete === void 0 ? void 0 : onExitComplete(); }, (_f = transition.duration) !== null && _f !== void 0 ? _f : 300);
147
+ var _a;
148
+ if (shouldExit && !isExitingRef.current) {
149
+ isExitingRef.current = true;
150
+ if (exit && Object.keys(exit).length > 0) {
151
+ animateToValues(exit, transition);
152
+ const exitDuration = (_a = transition.duration) !== null && _a !== void 0 ? _a : 300;
153
+ setTimeout(() => {
154
+ setIsPresent(false);
155
+ if (onExitComplete) {
156
+ onExitComplete();
157
+ }
158
+ }, exitDuration);
159
+ }
160
+ else {
161
+ setIsPresent(false);
162
+ if (onExitComplete) {
163
+ onExitComplete();
164
+ }
165
+ }
214
166
  }
215
- else if (!shouldExit && hasExited) {
216
- setHasExited(false);
167
+ else if (!shouldExit && isExitingRef.current) {
168
+ // Re-entering: reset everything
169
+ isExitingRef.current = false;
217
170
  setIsPresent(true);
218
- opacity.value = (_g = initial.opacity) !== null && _g !== void 0 ? _g : 1;
219
- translateX.value = (_h = initial.x) !== null && _h !== void 0 ? _h : 0;
220
- translateY.value = (_j = initial.y) !== null && _j !== void 0 ? _j : 0;
221
- scale.value = (_k = initial.scale) !== null && _k !== void 0 ? _k : 1;
222
- rotate.value = (_l = initial.rotate) !== null && _l !== void 0 ? _l : '0deg';
223
- width.value = (_m = initial.width) !== null && _m !== void 0 ? _m : null;
224
- height.value = (_o = initial.height) !== null && _o !== void 0 ? _o : null;
225
- borderRadius.value = (_p = initial.borderRadius) !== null && _p !== void 0 ? _p : null;
226
- backgroundColor.value = (_q = initial.backgroundColor) !== null && _q !== void 0 ? _q : null;
227
- margin.value = (_r = initial.margin) !== null && _r !== void 0 ? _r : null;
228
- marginTop.value = (_s = initial.marginTop) !== null && _s !== void 0 ? _s : null;
229
- marginBottom.value = (_t = initial.marginBottom) !== null && _t !== void 0 ? _t : null;
230
- marginLeft.value = (_u = initial.marginLeft) !== null && _u !== void 0 ? _u : null;
231
- marginRight.value = (_v = initial.marginRight) !== null && _v !== void 0 ? _v : null;
232
- padding.value = (_w = initial.padding) !== null && _w !== void 0 ? _w : null;
233
- paddingTop.value = (_x = initial.paddingTop) !== null && _x !== void 0 ? _x : null;
234
- paddingBottom.value = (_y = initial.paddingBottom) !== null && _y !== void 0 ? _y : null;
235
- paddingLeft.value = (_z = initial.paddingLeft) !== null && _z !== void 0 ? _z : null;
236
- paddingRight.value = (_0 = initial.paddingRight) !== null && _0 !== void 0 ? _0 : null;
237
- borderWidth.value = (_1 = initial.borderWidth) !== null && _1 !== void 0 ? _1 : null;
238
- borderColor.value = (_2 = initial.borderColor) !== null && _2 !== void 0 ? _2 : null;
239
- top.value = (_3 = initial.top) !== null && _3 !== void 0 ? _3 : null;
240
- bottom.value = (_4 = initial.bottom) !== null && _4 !== void 0 ? _4 : null;
241
- left.value = (_5 = initial.left) !== null && _5 !== void 0 ? _5 : null;
242
- right.value = (_6 = initial.right) !== null && _6 !== void 0 ? _6 : null;
171
+ setHasAnimated(false);
172
+ // Reset to initial values
173
+ if (initial !== false) {
174
+ Object.entries(initial).forEach(([key, value]) => {
175
+ const sharedValue = getSharedValue(key);
176
+ if (sharedValue && value !== undefined) {
177
+ sharedValue.value = value;
178
+ }
179
+ });
180
+ }
181
+ // Animate to target after a frame
243
182
  setTimeout(() => {
244
- var _a, _b, _c, _d, _e;
245
- runAnim(opacity, (_a = animate.opacity) !== null && _a !== void 0 ? _a : opacity.value);
246
- runAnim(translateX, (_b = animate.x) !== null && _b !== void 0 ? _b : translateX.value);
247
- runAnim(translateY, (_c = animate.y) !== null && _c !== void 0 ? _c : translateY.value);
248
- runAnim(scale, (_d = animate.scale) !== null && _d !== void 0 ? _d : scale.value);
249
- runAnim(rotate, (_e = animate.rotate) !== null && _e !== void 0 ? _e : rotate.value);
250
- if (animate.width !== undefined)
251
- runAnim(width, animate.width);
252
- if (animate.height !== undefined)
253
- runAnim(height, animate.height);
254
- if (animate.borderRadius !== undefined)
255
- runAnim(borderRadius, animate.borderRadius);
256
- if (animate.backgroundColor !== undefined)
257
- runAnim(backgroundColor, animate.backgroundColor);
258
- if (animate.margin !== undefined)
259
- runAnim(margin, animate.margin);
260
- if (animate.marginTop !== undefined)
261
- runAnim(marginTop, animate.marginTop);
262
- if (animate.marginBottom !== undefined)
263
- runAnim(marginBottom, animate.marginBottom);
264
- if (animate.marginLeft !== undefined)
265
- runAnim(marginLeft, animate.marginLeft);
266
- if (animate.marginRight !== undefined)
267
- runAnim(marginRight, animate.marginRight);
268
- if (animate.padding !== undefined)
269
- runAnim(padding, animate.padding);
270
- if (animate.paddingTop !== undefined)
271
- runAnim(paddingTop, animate.paddingTop);
272
- if (animate.paddingBottom !== undefined)
273
- runAnim(paddingBottom, animate.paddingBottom);
274
- if (animate.paddingLeft !== undefined)
275
- runAnim(paddingLeft, animate.paddingLeft);
276
- if (animate.paddingRight !== undefined)
277
- runAnim(paddingRight, animate.paddingRight);
278
- if (animate.borderWidth !== undefined)
279
- runAnim(borderWidth, animate.borderWidth);
280
- if (animate.borderColor !== undefined)
281
- runAnim(borderColor, animate.borderColor);
282
- if (animate.top !== undefined)
283
- runAnim(top, animate.top);
284
- if (animate.bottom !== undefined)
285
- runAnim(bottom, animate.bottom);
286
- if (animate.left !== undefined)
287
- runAnim(left, animate.left);
288
- if (animate.right !== undefined)
289
- runAnim(right, animate.right);
290
- }, 50);
183
+ if (!isExitingRef.current) {
184
+ animateToValues(animate);
185
+ setHasAnimated(true);
186
+ }
187
+ }, 16);
291
188
  }
292
189
  }, [shouldExit]);
190
+ // Animated style
293
191
  const animatedStyle = (0, react_native_reanimated_1.useAnimatedStyle)(() => {
294
- const styleObj = { opacity: opacity.value, transform: [{ translateX: translateX.value }, { translateY: translateY.value }, { scale: scale.value }, { rotate: rotate.value }] };
295
- if (width.value !== null)
296
- styleObj.width = width.value;
297
- if (height.value !== null)
298
- styleObj.height = height.value;
299
- if (borderRadius.value !== null)
300
- styleObj.borderRadius = borderRadius.value;
301
- if (backgroundColor.value !== null)
302
- styleObj.backgroundColor = backgroundColor.value;
303
- if (margin.value !== null)
304
- styleObj.margin = margin.value;
305
- if (marginTop.value !== null)
306
- styleObj.marginTop = marginTop.value;
307
- if (marginBottom.value !== null)
308
- styleObj.marginBottom = marginBottom.value;
309
- if (marginLeft.value !== null)
310
- styleObj.marginLeft = marginLeft.value;
311
- if (marginRight.value !== null)
312
- styleObj.marginRight = marginRight.value;
313
- if (padding.value !== null)
314
- styleObj.padding = padding.value;
315
- if (paddingTop.value !== null)
316
- styleObj.paddingTop = paddingTop.value;
317
- if (paddingBottom.value !== null)
318
- styleObj.paddingBottom = paddingBottom.value;
319
- if (paddingLeft.value !== null)
320
- styleObj.paddingLeft = paddingLeft.value;
321
- if (paddingRight.value !== null)
322
- styleObj.paddingRight = paddingRight.value;
323
- if (borderWidth.value !== null)
324
- styleObj.borderWidth = borderWidth.value;
325
- if (borderColor.value !== null)
326
- styleObj.borderColor = borderColor.value;
327
- if (top.value !== null)
328
- styleObj.top = top.value;
329
- if (bottom.value !== null)
330
- styleObj.bottom = bottom.value;
331
- if (left.value !== null)
332
- styleObj.left = left.value;
333
- if (right.value !== null)
334
- styleObj.right = right.value;
335
- return styleObj;
192
+ const style = {};
193
+ const transform = [];
194
+ // Transform properties
195
+ style.opacity = opacity.value;
196
+ if (x.value !== 0)
197
+ transform.push({ translateX: x.value });
198
+ if (y.value !== 0)
199
+ transform.push({ translateY: y.value });
200
+ if (scale.value !== 1)
201
+ transform.push({ scale: scale.value });
202
+ if (rotate.value !== '0deg')
203
+ transform.push({ rotate: rotate.value });
204
+ if (transform.length > 0)
205
+ style.transform = transform;
206
+ // Layout properties
207
+ if (width.value !== 0)
208
+ style.width = width.value;
209
+ if (height.value !== 0)
210
+ style.height = height.value;
211
+ if (backgroundColor.value !== 0)
212
+ style.backgroundColor = backgroundColor.value;
213
+ if (borderRadius.value !== 0)
214
+ style.borderRadius = borderRadius.value;
215
+ return style;
336
216
  });
337
217
  const AnimatedComponent = react_native_reanimated_1.default.createAnimatedComponent(Component);
338
218
  if (!isPresent)
@@ -340,6 +220,37 @@ function createMotionComponent(Component) {
340
220
  return react_1.default.createElement(AnimatedComponent, Object.assign({ style: [style, animatedStyle] }, rest), children);
341
221
  };
342
222
  }
223
+ function getInitialValue(key, initial) {
224
+ if (initial === false) {
225
+ return getDefaultValue(key);
226
+ }
227
+ const value = initial[key];
228
+ return value !== undefined ? value : getDefaultValue(key);
229
+ }
230
+ function getDefaultValue(key) {
231
+ switch (key) {
232
+ case 'opacity':
233
+ case 'scale':
234
+ case 'scaleX':
235
+ case 'scaleY':
236
+ return 1;
237
+ case 'x':
238
+ case 'y':
239
+ case 'z':
240
+ return 0;
241
+ case 'rotate':
242
+ case 'rotateX':
243
+ case 'rotateY':
244
+ case 'rotateZ':
245
+ case 'skewX':
246
+ case 'skewY':
247
+ return '0deg';
248
+ case 'shadowOffset':
249
+ return { width: 0, height: 0 };
250
+ default:
251
+ return 0;
252
+ }
253
+ }
343
254
  exports.NativeMotion = {
344
255
  View: createMotionComponent(react_native_1.View),
345
256
  Text: createMotionComponent(react_native_1.Text),
package/package.json CHANGED
@@ -1,54 +1,54 @@
1
- {
2
- "name": "motion-on-native",
3
- "version": "1.0.7",
4
- "description": "Framer Motion-inspired animation library for React Native with Reanimated. Easy spring animations, gestures, and transitions for mobile apps.",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "scripts": {
8
- "build": "tsc",
9
- "prepare": "npm run build",
10
- "test": "echo \"Error: no test specified\" && exit 1"
11
- },
12
- "keywords": [
13
- "react-native",
14
- "framer-motion",
15
- "animation",
16
- "reanimated",
17
- "motion",
18
- "spring-animation",
19
- "mobile-animation",
20
- "react-native-animation",
21
- "gesture",
22
- "transition",
23
- "ui-animation",
24
- "native-animation",
25
- "ios",
26
- "android",
27
- "typescript"
28
- ],
29
- "author": "Mohd Bilal",
30
- "license": "MIT",
31
- "peerDependencies": {
32
- "react": ">=16.8.0",
33
- "react-native": ">=0.60.0",
34
- "react-native-reanimated": ">=3.0.0"
35
- },
36
- "devDependencies": {
37
- "@types/react": "^19.1.9",
38
- "@types/react-native": "^0.72.8",
39
- "typescript": "^5.0.0"
40
- },
41
- "files": [
42
- "lib",
43
- "README.md",
44
- "LICENSE"
45
- ],
46
- "repository": {
47
- "type": "git",
48
- "url": "https://github.com/mohd-Bilal-exe/motion-on-native.git"
49
- },
50
- "bugs": {
51
- "url": "https://github.com/mohd-Bilal-exe/motion-on-native/issues"
52
- },
53
- "homepage": "https://github.com/mohd-Bilal-exe/motion-on-native#readme"
54
- }
1
+ {
2
+ "name": "motion-on-native",
3
+ "version": "1.1.0",
4
+ "description": "Framer Motion-inspired animation library for React Native with Reanimated. Easy spring animations, gestures, and transitions for mobile apps.",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "prepare": "npm run build",
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [
13
+ "react-native",
14
+ "framer-motion",
15
+ "animation",
16
+ "reanimated",
17
+ "motion",
18
+ "spring-animation",
19
+ "mobile-animation",
20
+ "react-native-animation",
21
+ "gesture",
22
+ "transition",
23
+ "ui-animation",
24
+ "native-animation",
25
+ "ios",
26
+ "android",
27
+ "typescript"
28
+ ],
29
+ "author": "Mohd Bilal",
30
+ "license": "MIT",
31
+ "peerDependencies": {
32
+ "react": ">=16.8.0",
33
+ "react-native": ">=0.60.0",
34
+ "react-native-reanimated": ">=3.0.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/react": "^19.1.9",
38
+ "@types/react-native": "^0.72.8",
39
+ "typescript": "^5.0.0"
40
+ },
41
+ "files": [
42
+ "lib",
43
+ "README.md",
44
+ "LICENSE"
45
+ ],
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/mohd-Bilal-exe/motion-on-native.git"
49
+ },
50
+ "bugs": {
51
+ "url": "https://github.com/mohd-Bilal-exe/motion-on-native/issues"
52
+ },
53
+ "homepage": "https://github.com/mohd-Bilal-exe/motion-on-native#readme"
54
+ }