|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import { View, StyleSheet, Animated, Easing } from 'react-native'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | + |
| 5 | +const CircularProgressBar = (props) => { |
| 6 | + const { |
| 7 | + activeColor, |
| 8 | + passiveColor, |
| 9 | + baseColor, |
| 10 | + radius, |
| 11 | + percent, |
| 12 | + width, |
| 13 | + duration, |
| 14 | + children, |
| 15 | + } = props; |
| 16 | + |
| 17 | + const initialValueHalfCircle = percent >= 50 ? 0 : 180; |
| 18 | + const initialValueInnerCircle = 0; |
| 19 | + const firstCircleAnimatedValue = new Animated.Value(initialValueHalfCircle); |
| 20 | + const secondCircleAnimatedValue = new Animated.Value(initialValueHalfCircle); |
| 21 | + const thirdCircleAnimatedValue = new Animated.Value(initialValueInnerCircle); |
| 22 | + const timePerDegree = duration / 360; |
| 23 | + const firstCircleColor = activeColor; |
| 24 | + const secondCircleColor = percent >= 50 ? activeColor : passiveColor; |
| 25 | + |
| 26 | + const secondAnimation = () => { |
| 27 | + firstCircleAnimatedValue.setValue(initialValueHalfCircle); |
| 28 | + secondCircleAnimatedValue.setValue(initialValueHalfCircle); |
| 29 | + thirdCircleAnimatedValue.setValue(initialValueHalfCircle); |
| 30 | + Animated.parallel([ |
| 31 | + Animated.timing(firstCircleAnimatedValue, { |
| 32 | + toValue: 180, |
| 33 | + duration: 180 * timePerDegree, |
| 34 | + useNativeDriver: true, |
| 35 | + easing: Easing.linear, |
| 36 | + }), |
| 37 | + Animated.timing(secondCircleAnimatedValue, { |
| 38 | + toValue: 180 + (percent - 50) * 3.6, |
| 39 | + duration: (180 + (percent - 50) * 3.6) * timePerDegree, |
| 40 | + useNativeDriver: true, |
| 41 | + easing: Easing.linear, |
| 42 | + }), |
| 43 | + Animated.timing(thirdCircleAnimatedValue, { |
| 44 | + toValue: (percent - 50) * 3.6, |
| 45 | + delay: 180 * timePerDegree, |
| 46 | + duration: timePerDegree * ((percent - 50) * 3.6), |
| 47 | + useNativeDriver: false, |
| 48 | + easing: Easing.linear, |
| 49 | + }), |
| 50 | + ]).start(); |
| 51 | + }; |
| 52 | + |
| 53 | + const firstAnimation = () => { |
| 54 | + Animated.timing(secondCircleAnimatedValue, { |
| 55 | + toValue: 180 + percent * 3.6, |
| 56 | + duration: percent * 3.6 * timePerDegree, |
| 57 | + useNativeDriver: true, |
| 58 | + easing: Easing.linear, |
| 59 | + }).start(); |
| 60 | + }; |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (percent < 50) { |
| 64 | + firstAnimation(); |
| 65 | + } else { |
| 66 | + secondAnimation(); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + const renderHalf = (color, transforms = [], otherStyles = {}) => ( |
| 71 | + <Animated.View |
| 72 | + style={[ |
| 73 | + styles.half, |
| 74 | + { backgroundColor: color, borderColor: color }, |
| 75 | + { width: radius, height: radius * 2, borderRadius: radius }, |
| 76 | + { |
| 77 | + transform: [ |
| 78 | + { translateX: radius / 2 }, |
| 79 | + ...transforms, |
| 80 | + { translateX: -radius / 2 }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + otherStyles, |
| 84 | + ]} |
| 85 | + /> |
| 86 | + ); |
| 87 | + |
| 88 | + const rotate1 = firstCircleAnimatedValue.interpolate({ |
| 89 | + inputRange: [0, 1], |
| 90 | + outputRange: ['0deg', '1deg'], |
| 91 | + }); |
| 92 | + |
| 93 | + const rotate2 = secondCircleAnimatedValue.interpolate({ |
| 94 | + inputRange: [0, 1], |
| 95 | + outputRange: ['0deg', '1deg'], |
| 96 | + }); |
| 97 | + |
| 98 | + const rotate3 = thirdCircleAnimatedValue.interpolate({ |
| 99 | + inputRange: [0, 1], |
| 100 | + outputRange: ['0deg', '1deg'], |
| 101 | + }); |
| 102 | + |
| 103 | + const elevation3 = thirdCircleAnimatedValue.interpolate({ |
| 104 | + inputRange: [0, 1], |
| 105 | + outputRange: [0, -1], |
| 106 | + }); |
| 107 | + |
| 108 | + const innerCircleStyle = { |
| 109 | + backgroundColor: baseColor, |
| 110 | + width: 2 * radius - width, |
| 111 | + height: 2 * radius - width, |
| 112 | + borderRadius: radius, |
| 113 | + elevation: 1000, |
| 114 | + display: 'flex', |
| 115 | + justifyContent: 'center', |
| 116 | + alignItems: 'center', |
| 117 | + }; |
| 118 | + |
| 119 | + return ( |
| 120 | + <View style={styles.container} key={percent}> |
| 121 | + <View |
| 122 | + style={[ |
| 123 | + styles.outerCircle, |
| 124 | + { |
| 125 | + height: radius * 2, |
| 126 | + width: radius * 2, |
| 127 | + borderRadius: radius, |
| 128 | + backgroundColor: passiveColor, |
| 129 | + overflow: 'hidden', |
| 130 | + }, |
| 131 | + ]} |
| 132 | + > |
| 133 | + {renderHalf(firstCircleColor, [{ rotate: rotate1 }])} |
| 134 | + {renderHalf(secondCircleColor, [{ rotate: rotate2 }])} |
| 135 | + {renderHalf(passiveColor, [{ rotate: rotate3 }], { |
| 136 | + elevation: elevation3, zIndex: elevation3, |
| 137 | + })} |
| 138 | + {<View style={innerCircleStyle}> |
| 139 | + {children} |
| 140 | + </View>} |
| 141 | + </View> |
| 142 | + </View> |
| 143 | + ); |
| 144 | +}; |
| 145 | + |
| 146 | +CircularProgressBar.propTypes = { |
| 147 | + activeColor: PropTypes.string.isRequired, |
| 148 | + passiveColor: PropTypes.string.isRequired, |
| 149 | + baseColor: PropTypes.string.isRequired, |
| 150 | + width: PropTypes.number.isRequired, |
| 151 | + radius: PropTypes.number.isRequired, |
| 152 | + percent: PropTypes.number.isRequired, |
| 153 | + duration: PropTypes.number.isRequired, |
| 154 | + children: PropTypes.element, |
| 155 | +}; |
| 156 | + |
| 157 | +const styles = StyleSheet.create({ |
| 158 | + container: { |
| 159 | + flex: 1, |
| 160 | + backgroundColor: '#fff', |
| 161 | + alignItems: 'center', |
| 162 | + justifyContent: 'center', |
| 163 | + }, |
| 164 | + outerCircle: { |
| 165 | + position: 'relative', |
| 166 | + justifyContent: 'center', |
| 167 | + alignItems: 'center', |
| 168 | + }, |
| 169 | + half: { |
| 170 | + position: 'absolute', |
| 171 | + left: 0, |
| 172 | + top: 0, |
| 173 | + borderTopRightRadius: 0, |
| 174 | + borderBottomRightRadius: 0, |
| 175 | + }, |
| 176 | +}); |
| 177 | + |
| 178 | +export default CircularProgressBar; |
0 commit comments