|
| 1 | +import React from 'react' |
| 2 | +import { View } from 'react-native' |
| 3 | +import LottieView from 'lottie-react-native' |
| 4 | +import { Text, useTheme } from '@avalabs/k2-alpine' |
| 5 | + |
| 6 | +// Import animation at the top level |
| 7 | +const connectWavesAnimation = require('assets/lotties/connect-waves.json') |
| 8 | + |
| 9 | +interface AnimatedIconWithTextProps { |
| 10 | + /** The icon component to display */ |
| 11 | + icon: React.ReactNode |
| 12 | + /** The main title text */ |
| 13 | + title: string |
| 14 | + /** The subtitle/description text */ |
| 15 | + subtitle: string |
| 16 | + /** Whether to show the animation behind the icon */ |
| 17 | + showAnimation?: boolean |
| 18 | + /** Custom animation source (defaults to connect-waves.json) - Lottie animation JSON |
| 19 | + * In React Native, require() for a Lottie JSON returns either a number (asset reference) or an object (parsed JSON). |
| 20 | + * This union type matches what LottieView expects and removes the any lint error. |
| 21 | + */ |
| 22 | + animationSource?: number | object |
| 23 | + /** Custom animation size (defaults to 220x220) */ |
| 24 | + animationSize?: { width: number; height: number } |
| 25 | + /** Custom icon positioning offset for animation centering */ |
| 26 | + animationOffset?: { top: number; left: number } |
| 27 | + /** Custom color for the animation (defaults to theme textPrimary) */ |
| 28 | + animationColor?: string |
| 29 | +} |
| 30 | + |
| 31 | +export const AnimatedIconWithText: React.FC<AnimatedIconWithTextProps> = ({ |
| 32 | + icon, |
| 33 | + title, |
| 34 | + subtitle, |
| 35 | + showAnimation = false, |
| 36 | + animationSource = connectWavesAnimation, |
| 37 | + animationSize = { width: 220, height: 220 }, |
| 38 | + animationColor |
| 39 | +}) => { |
| 40 | + const { |
| 41 | + theme: { colors } |
| 42 | + } = useTheme() |
| 43 | + |
| 44 | + // Calculate dynamic positioning based on animation size |
| 45 | + const iconContainerHeight = 44 // Assuming standard icon size |
| 46 | + const animationRadius = animationSize.width / 2 |
| 47 | + const iconRadius = iconContainerHeight / 2 |
| 48 | + |
| 49 | + // Calculate animation offset to center it around the icon |
| 50 | + const dynamicAnimationOffset = { |
| 51 | + top: -(animationRadius - iconRadius), |
| 52 | + left: -(animationRadius - iconRadius) |
| 53 | + } |
| 54 | + |
| 55 | + // Calculate consistent text position regardless of animation state |
| 56 | + const baseTopPosition = 160 // Base centering position |
| 57 | + const textOverlapPosition = baseTopPosition + iconContainerHeight + 16 // Keep text close to icon for both states |
| 58 | + |
| 59 | + return ( |
| 60 | + <View |
| 61 | + style={{ |
| 62 | + flex: 1, |
| 63 | + alignItems: 'center', |
| 64 | + paddingHorizontal: 32 |
| 65 | + }}> |
| 66 | + <View |
| 67 | + style={{ |
| 68 | + marginTop: baseTopPosition, |
| 69 | + alignItems: 'center', |
| 70 | + justifyContent: 'center' |
| 71 | + }}> |
| 72 | + {showAnimation && ( |
| 73 | + <LottieView |
| 74 | + source={animationSource} |
| 75 | + autoPlay |
| 76 | + loop |
| 77 | + resizeMode="contain" |
| 78 | + colorFilters={[ |
| 79 | + { |
| 80 | + keypath: '*', // Apply to all layers |
| 81 | + color: animationColor || colors.$textPrimary // Use custom color or theme default |
| 82 | + } |
| 83 | + ]} |
| 84 | + style={{ |
| 85 | + position: 'absolute', |
| 86 | + width: animationSize.width, |
| 87 | + height: animationSize.height, |
| 88 | + top: dynamicAnimationOffset.top, |
| 89 | + left: dynamicAnimationOffset.left |
| 90 | + }} |
| 91 | + /> |
| 92 | + )} |
| 93 | + {icon} |
| 94 | + </View> |
| 95 | + <View |
| 96 | + style={{ |
| 97 | + position: 'absolute', |
| 98 | + top: textOverlapPosition, |
| 99 | + left: 0, |
| 100 | + right: 0, |
| 101 | + alignItems: 'center', |
| 102 | + paddingHorizontal: 32 |
| 103 | + }}> |
| 104 | + <Text |
| 105 | + variant="heading6" |
| 106 | + style={{ |
| 107 | + textAlign: 'center', |
| 108 | + marginBottom: 4 |
| 109 | + }}> |
| 110 | + {title} |
| 111 | + </Text> |
| 112 | + <Text |
| 113 | + variant="body1" |
| 114 | + style={{ |
| 115 | + textAlign: 'center', |
| 116 | + color: colors.$textSecondary, |
| 117 | + maxWidth: 280 |
| 118 | + }}> |
| 119 | + {subtitle} |
| 120 | + </Text> |
| 121 | + </View> |
| 122 | + </View> |
| 123 | + ) |
| 124 | +} |
0 commit comments