| 6 | import { animations } from '../assets'; |
| 7 | |
| 8 | function LoadingContainer({ |
| 9 | animation, |
| 10 | children, |
| 11 | loading, |
| 12 | animationScale, |
| 13 | style, |
| 14 | }) { |
| 15 | const [dimensions, setDimensions] = useState({ width: 0, height: 0 }); |
| 16 | |
| 17 | function onLayoutChange(event) { |
| 18 | const { width, height } = event.nativeEvent.layout; |
| 19 | |
| 20 | if (width === 0 && height === 0) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | setDimensions({ width, height }); |
| 25 | } |
| 26 | |
| 27 | const lottieViewDimensions = useMemo( |
| 28 | () => ({ |
| 29 | width: dimensions.width * animationScale, |
| 30 | height: dimensions.height * animationScale, |
| 31 | }), |
| 32 | [animationScale, dimensions], |
| 33 | ); |
| 34 | |
| 35 | if (!children) { |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | return ( |
| 40 | <View style={style.container}> |
| 41 | {!loading && <View onLayout={onLayoutChange}>{children}</View>} |
| 42 | {loading && ( |
| 43 | <View style={[style.container, dimensions]}> |
| 44 | <LottieView |
| 45 | style={lottieViewDimensions} |
| 46 | source={animation} |
| 47 | colorFilters={style.animationFilters} |
| 48 | autoPlay |
| 49 | loop |
| 50 | /> |
| 51 | </View> |
| 52 | )} |
| 53 | </View> |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | LoadingContainer.propTypes = { |
| 58 | style: PropTypes.object.isRequired, |
| 59 | animation: PropTypes.object, |
| 60 | animationScale: PropTypes.number, |
| 61 | children: PropTypes.oneOfType([ |
| 62 | PropTypes.object, |
| 63 | PropTypes.func, |
| 64 | PropTypes.node, |
| 65 | ]), |