UNPKG

2.38 kBJavaScriptView Raw
1import { Dimensions } from 'react-native';
2import * as animatable from 'react-native-animatable';
3const { height, width } = Dimensions.get('window');
4export const initializeAnimations = () => {
5 // Since react-native-animatable applies by default a margin of 100 to its
6 // sliding animation, we reset them here overriding the margin to 0.
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};
19export const makeSlideTranslation = (translationType, fromValue, toValue) => {
20 return {
21 from: {
22 [translationType]: fromValue,
23 },
24 to: {
25 [translationType]: toValue,
26 },
27 };
28};
29// User can define custom react-native-animatable animations, see PR #72
30// Utility for creating our own custom react-native-animatable animations
31export 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};
55export const reversePercentage = (x) => -(x - 1);
56const makeAnimation = (name, obj) => {
57 animatable.registerAnimation(name, animatable.createAnimation(obj));
58};
59const isObject = (obj) => {
60 return obj !== null && typeof obj === 'object';
61};