Skip to content

Commit

Permalink
add animation direction
Browse files Browse the repository at this point in the history
  • Loading branch information
Salman Salari committed Jul 20, 2020
1 parent a3ec965 commit d8a7b64
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $ cd ios
| delay | No | delay in millisecond for every round of animation default is zero |
| forColor | No | the animated gradient color default is #CBCBCB |
| backColor | No | the backgound of maked view the default is #E0E0E0 |
| dir | No | animation direction the default is "ltr", ltr/rtl |


## Usage
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-masked-loader",
"version": "1.0.9",
"version": "1.1.0",
"description": "A simple content and skeleton loader component for react native with animation",
"main": "index.js",
"scripts": {},
Expand Down
63 changes: 39 additions & 24 deletions src/ContentLoader.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { View, Animated, Dimensions, StyleSheet, I18nManager } from 'react-native';
import MaskedView from '@react-native-community/masked-view';
import Svg, { LinearGradient, Defs, Rect, Stop } from 'react-native-svg';

const { width } = Dimensions.get('window');
const translateYValue = new Animated.Value(0);
const animatedStyle = {
transform: [{ translateX: translateYValue }],
width: width,
left: -width,
backgroundColor: 'transparent',
zIndex: 2
};

const styles = StyleSheet.create({
container: {
position: 'absolute',
Expand All @@ -28,27 +21,30 @@ const BackgroundComponent = (props) => {
return <View style={{...styles.container, backgroundColor: props.backColor}}/>;
}

const dirOrigin = {
ltr: {
left: (I18nManager.isRTL ? -1 : 1) * -width,
},
rtl: {
left: (I18nManager.isRTL ? -1 : 1) * width
}
}

const dirDestination = {
ltr: 2 * width,
rtl: 2 * -width
}

const ContentLoader = (props) => {
const { MaskedElement } = props || {};
let { forColor, backColor } = props || {};

forColor = !!forColor ? forColor : '#CBCBCB';
backColor = !!backColor ? backColor : '#E0E0E0';

const translateYValue = new Animated.Value(0)
const [animatedStyle, setAnimatedStyle] = useState({});


const show = () => {
let { duration, delay } = props;
duration = !!duration ? duration : 1200;
delay = !!delay ? delay : 0;
Animated.loop(Animated.timing(translateYValue, {
toValue: !I18nManager.isRTL ? (2 * width) : (-2 * width),
duration,
delay,
useNativeDriver: true,
})).start();
}

const getAnimationElement = (forColor) => {
return (
React.createElement(Svg, { height: "100%", preserveAspectRatio: "xMinYMin slice", width: "100%", viewBox: "0 0 100 100" },
Expand All @@ -63,8 +59,27 @@ const ContentLoader = (props) => {
}

useEffect(() => {
show()
}, []);

setAnimatedStyle({
transform: [{ translateX: translateYValue }],
width: width,
backgroundColor: 'transparent',
zIndex: 2,
...dirOrigin[props.dir || 'ltr']
});

let { duration, delay } = props;
duration = !!duration ? duration : 1200;
delay = !!delay ? delay : 0;

Animated.loop(Animated.timing(translateYValue, {
toValue: dirDestination[props.dir || 'ltr'],
duration,
delay,
useNativeDriver: true,
})).start();

},[]);

return (
React.createElement(MaskedView, { maskElement: MaskedElement },
Expand Down

0 comments on commit d8a7b64

Please sign in to comment.