|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import styled from 'styled-components/native'; |
| 3 | +import Animated, { |
| 4 | + cancelAnimation, |
| 5 | + useAnimatedStyle, |
| 6 | + useSharedValue, |
| 7 | + withDelay, |
| 8 | + withRepeat, |
| 9 | + withSequence, |
| 10 | + withTiming, |
| 11 | +} from 'react-native-reanimated'; |
| 12 | +import { Text as SVGText, Circle } from 'react-native-svg'; |
| 13 | +import type { CircularProgressBarFilledProps } from './types'; |
| 14 | +import { circularProgressSizeTokens, getCircularProgressSVGTokens } from './progressBarTokens'; |
| 15 | +import { CircularProgressLabel } from './CircularProgressLabel'; |
| 16 | +import getIn from '~utils/lodashButBetter/get'; |
| 17 | +import BaseBox from '~components/Box/BaseBox'; |
| 18 | +import { makeMotionTime } from '~utils/makeMotionTime'; |
| 19 | +import type { TextProps } from '~components/Typography'; |
| 20 | +import { getTextProps } from '~components/Typography'; |
| 21 | +import { useTheme } from '~components/BladeProvider'; |
| 22 | +import { castNativeType } from '~utils'; |
| 23 | +import { Svg } from '~components/Icons/_Svg'; |
| 24 | +import getBaseTextStyles from '~components/Typography/BaseText/getBaseTextStyles'; |
| 25 | + |
| 26 | +const pulseAnimation = { |
| 27 | + opacityInitial: 1, |
| 28 | + opacityMid: 0.65, |
| 29 | + opacityFinal: 1, |
| 30 | +}; |
| 31 | + |
| 32 | +const StyledSVGText = styled(SVGText)<Pick<TextProps<{ variant: 'body' }>, 'size' | 'weight'>>( |
| 33 | + ({ theme, size, weight }) => { |
| 34 | + const textProps = getTextProps({ variant: 'body', size, weight }); |
| 35 | + |
| 36 | + return { |
| 37 | + ...getBaseTextStyles({ theme, ...textProps }), |
| 38 | + strokeWidth: 0, |
| 39 | + fill: getIn(theme.colors, textProps.color!), |
| 40 | + }; |
| 41 | + }, |
| 42 | +); |
| 43 | + |
| 44 | +const CircularProgressBarFilled = ({ |
| 45 | + progressPercent, |
| 46 | + fillColor, |
| 47 | + backgroundColor, |
| 48 | + size = 'small', |
| 49 | + label, |
| 50 | + showPercentage = true, |
| 51 | + isMeter, |
| 52 | + motionEasing, |
| 53 | + pulseMotionDuration, |
| 54 | + pulseMotionDelay, |
| 55 | + fillMotionDuration, |
| 56 | +}: CircularProgressBarFilledProps): React.ReactElement => { |
| 57 | + const { |
| 58 | + sqSize, |
| 59 | + strokeWidth, |
| 60 | + radius, |
| 61 | + viewBox, |
| 62 | + dashArray, |
| 63 | + dashOffset, |
| 64 | + } = getCircularProgressSVGTokens({ size, progressPercent }); |
| 65 | + |
| 66 | + const AnimatedCircle = Animated.createAnimatedComponent(Circle); |
| 67 | + const animatedOpacity = useSharedValue(pulseAnimation.opacityInitial); |
| 68 | + const animatedStrokeDashoffset = useSharedValue(dashOffset); |
| 69 | + const { theme } = useTheme(); |
| 70 | + const fillAndPulseEasing = getIn(theme.motion, motionEasing); |
| 71 | + const pulseDuration = |
| 72 | + castNativeType(makeMotionTime(getIn(theme.motion, pulseMotionDuration))) / 2; |
| 73 | + |
| 74 | + // Trigger animation for progress fill |
| 75 | + useEffect(() => { |
| 76 | + const fillDuration = castNativeType(makeMotionTime(getIn(theme.motion, fillMotionDuration))); |
| 77 | + animatedStrokeDashoffset.value = withTiming(dashOffset, { |
| 78 | + duration: fillDuration, |
| 79 | + easing: fillAndPulseEasing, |
| 80 | + }); |
| 81 | + return () => { |
| 82 | + cancelAnimation(animatedStrokeDashoffset); |
| 83 | + }; |
| 84 | + }, [dashOffset, animatedStrokeDashoffset, fillMotionDuration, theme, fillAndPulseEasing]); |
| 85 | + |
| 86 | + // Trigger pulsating animation |
| 87 | + useEffect(() => { |
| 88 | + const pulsatingAnimationTimingConfig = { |
| 89 | + duration: pulseDuration, |
| 90 | + easing: fillAndPulseEasing, |
| 91 | + }; |
| 92 | + if (!isMeter) { |
| 93 | + animatedOpacity.value = withDelay( |
| 94 | + castNativeType(makeMotionTime(getIn(theme.motion, pulseMotionDelay))), |
| 95 | + withRepeat( |
| 96 | + withSequence( |
| 97 | + withTiming(pulseAnimation.opacityMid, pulsatingAnimationTimingConfig), |
| 98 | + withTiming(pulseAnimation.opacityFinal, pulsatingAnimationTimingConfig), |
| 99 | + ), |
| 100 | + -1, |
| 101 | + ), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return () => { |
| 106 | + cancelAnimation(animatedOpacity); |
| 107 | + }; |
| 108 | + }, [animatedOpacity, fillAndPulseEasing, pulseDuration, pulseMotionDelay, theme, isMeter]); |
| 109 | + |
| 110 | + const firstIndicatorStyles = useAnimatedStyle(() => { |
| 111 | + return { |
| 112 | + strokeDashoffset: animatedStrokeDashoffset.value, |
| 113 | + opacity: progressPercent < 100 ? animatedOpacity.value : 1, |
| 114 | + }; |
| 115 | + }); |
| 116 | + |
| 117 | + return ( |
| 118 | + <BaseBox display="flex" width="fit-content" alignItems="center"> |
| 119 | + <Svg width={String(sqSize)} height={String(sqSize)} viewBox={viewBox}> |
| 120 | + <Circle |
| 121 | + fill="none" |
| 122 | + stroke={backgroundColor} |
| 123 | + cx={String(sqSize / 2)} |
| 124 | + cy={String(sqSize / 2)} |
| 125 | + r={String(radius)} |
| 126 | + strokeWidth={`${strokeWidth}px`} |
| 127 | + /> |
| 128 | + |
| 129 | + <AnimatedCircle |
| 130 | + fill="none" |
| 131 | + stroke={fillColor} |
| 132 | + cx={sqSize / 2} |
| 133 | + cy={sqSize / 2} |
| 134 | + r={radius} |
| 135 | + strokeWidth={`${strokeWidth}px`} |
| 136 | + // Start progress marker at 12 O'Clock |
| 137 | + transform={`rotate(-90 ${sqSize / 2} ${sqSize / 2})`} |
| 138 | + strokeDasharray={dashArray} |
| 139 | + strokeDashoffset={dashOffset} |
| 140 | + style={firstIndicatorStyles} |
| 141 | + /> |
| 142 | + |
| 143 | + {showPercentage && size !== 'small' && ( |
| 144 | + <StyledSVGText |
| 145 | + size={circularProgressSizeTokens[size].percentTextSize} |
| 146 | + weight="semibold" |
| 147 | + x="50%" |
| 148 | + y="50%" |
| 149 | + textAnchor="middle" |
| 150 | + dy=".5em" |
| 151 | + > |
| 152 | + {`${progressPercent}%`} |
| 153 | + </StyledSVGText> |
| 154 | + )} |
| 155 | + </Svg> |
| 156 | + |
| 157 | + <CircularProgressLabel |
| 158 | + progressPercent={progressPercent} |
| 159 | + size={size} |
| 160 | + label={label} |
| 161 | + showPercentage={showPercentage} |
| 162 | + /> |
| 163 | + </BaseBox> |
| 164 | + ); |
| 165 | +}; |
| 166 | + |
| 167 | +export { CircularProgressBarFilled }; |
0 commit comments