1 | import { Dimensions } from 'react-native';
|
2 | import * as animatable from 'react-native-animatable';
|
3 | const { height, width } = Dimensions.get('window');
|
4 | export const initializeAnimations = () => {
|
5 |
|
6 |
|
7 | const animationDefinitions = {
|
8 | slideInDown: makeSlideTranslation('translateY', -height, 0),
|
9 | slideInUp: makeSlideTranslation('translateY', height, 0),
|
10 | slideInLeft: makeSlideTranslation('translateX', -width, 0),
|
11 | slideInRight: makeSlideTranslation('translateX', width, 0),
|
12 | slideOutDown: makeSlideTranslation('translateY', 0, height),
|
13 | slideOutUp: makeSlideTranslation('translateY', 0, -height),
|
14 | slideOutLeft: makeSlideTranslation('translateX', 0, -width),
|
15 | slideOutRight: makeSlideTranslation('translateX', 0, width),
|
16 | };
|
17 | animatable.initializeRegistryWithDefinitions(animationDefinitions);
|
18 | };
|
19 | export const makeSlideTranslation = (translationType, fromValue, toValue) => {
|
20 | return {
|
21 | from: {
|
22 | [translationType]: fromValue,
|
23 | },
|
24 | to: {
|
25 | [translationType]: toValue,
|
26 | },
|
27 | };
|
28 | };
|
29 |
|
30 |
|
31 | export const buildAnimations = ({ animationIn, animationOut, }) => {
|
32 | let updatedAnimationIn;
|
33 | let updatedAnimationOut;
|
34 | if (isObject(animationIn)) {
|
35 | const animationName = JSON.stringify(animationIn);
|
36 | makeAnimation(animationName, animationIn);
|
37 | updatedAnimationIn = animationName;
|
38 | }
|
39 | else {
|
40 | updatedAnimationIn = animationIn;
|
41 | }
|
42 | if (isObject(animationOut)) {
|
43 | const animationName = JSON.stringify(animationOut);
|
44 | makeAnimation(animationName, animationOut);
|
45 | updatedAnimationOut = animationName;
|
46 | }
|
47 | else {
|
48 | updatedAnimationOut = animationOut;
|
49 | }
|
50 | return {
|
51 | animationIn: updatedAnimationIn,
|
52 | animationOut: updatedAnimationOut,
|
53 | };
|
54 | };
|
55 | export const reversePercentage = (x) => -(x - 1);
|
56 | const makeAnimation = (name, obj) => {
|
57 | animatable.registerAnimation(name, animatable.createAnimation(obj));
|
58 | };
|
59 | const isObject = (obj) => {
|
60 | return obj !== null && typeof obj === 'object';
|
61 | };
|