forked from mfrachet/rn-placeholder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shine.tsx
63 lines (53 loc) · 1.33 KB
/
Shine.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React, { useEffect, useRef } from "react";
import { Animated, StyleSheet, ViewProps } from "react-native";
import { AnimationContext } from "./context";
const START_VALUE = 0;
const END_VALUE = 100;
const isInteraction = false;
export interface ShineProps extends ViewProps {
/* Animation duration, default is 750 */
duration?: number;
/* Play the animation in reverse mod */
reverse: boolean;
}
export const Shine: React.FC<ShineProps> = ({
duration,
children,
style,
reverse,
}) => {
const animation = useRef(new Animated.Value(START_VALUE));
const start = () => {
animation.current.setValue(START_VALUE);
Animated.timing(animation.current, {
duration: duration || 750,
isInteraction,
toValue: END_VALUE,
useNativeDriver: false,
}).start((e) => {
if (e.finished) {
start();
}
});
};
useEffect(() => {
start();
}, []);
const left = animation.current.interpolate({
inputRange: [START_VALUE, END_VALUE],
outputRange: reverse ? ["100%", "0%"] : ["0%", "100%"],
});
return (
<AnimationContext.Provider value={[styles.shine, { left }, style]}>
{children}
</AnimationContext.Provider>
);
};
const styles = StyleSheet.create({
shine: {
backgroundColor: "white",
height: "100%",
opacity: 0.5,
width: "40%",
},
});