forked from codedailyio/react-native-animated-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (108 loc) · 2.68 KB
/
index.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, { Component } from "react";
import { View, StyleSheet, Animated } from "react-native";
class AnimatedBar extends Component {
state = {
animation: new Animated.Value(this.props.progress),
};
componentWillMount() {
this.widthInterpolate = this.state.animation.interpolate({
inputRange: [0, 1],
outputRange: ["0%", "100%"],
extrapolate: "clamp",
});
}
componentDidMount() {
this.attachListener();
}
attachListener = () => {
this.removeListeners();
if (typeof this.props.onAnimate === "function") {
this.attachListener(this.props.onAnimate);
}
};
removeListeners = () => {
this.state.animation.removeAllListeners();
};
componentWillUnmount() {
this.removeListeners();
}
componentDidUpdate(prevProps, prevState) {
//If our function changes then attach the new one
if (prevProps.onAnimate !== this.props.onAnimate) {
this.attachListener();
}
//If our progress has changed we should animate
if (prevProps.progress !== this.props.progress) {
if (this.props.animate) {
Animated.timing(this.state.animation, {
useNativeDriver: false, // Hardware rendering doesn't sit well with module
toValue: this.props.progress,
duration: this.props.duration,
}).start();
} else {
this.state.animation.setValue(this.props.progress);
}
}
}
render() {
const {
children,
height,
borderColor,
borderWidth,
borderRadius,
barColor,
fillColor,
row,
style,
wrapStyle,
fillStyle,
barStyle,
} = this.props;
return (
<Animated.View style={[styles.outer, { height }, row ? styles.flex : undefined, style]}>
<Animated.View style={[styles.flex, { borderColor, borderWidth, borderRadius }, wrapStyle]}>
<Animated.View
style={[StyleSheet.absoluteFill, { backgroundColor: fillColor }, fillStyle]}
/>
<Animated.View
style={[
styles.bar,
{
width: this.widthInterpolate,
backgroundColor: barColor,
},
barStyle,
]}
/>
{children}
</Animated.View>
</Animated.View>
);
}
}
const styles = StyleSheet.create({
outer: {
flexDirection: "row",
},
flex: {
flex: 1,
},
bar: {
position: "absolute",
left: 0,
top: 0,
bottom: 0,
},
});
AnimatedBar.defaultProps = {
height: 10,
borderColor: "#000",
borderWidth: 1,
borderRadius: 0,
barColor: "#FFF",
fillColor: "rgba(0,0,0,.5)",
duration: 100,
animate: true,
};
export default AnimatedBar;