|
| 1 | +import * as React from 'react' |
| 2 | +import { View } from 'react-native' |
| 3 | +import LinearGradient from 'react-native-linear-gradient' |
| 4 | +import Animated, { |
| 5 | + SharedValue, |
| 6 | + useAnimatedStyle, |
| 7 | + useSharedValue, |
| 8 | + withRepeat, |
| 9 | + withSequence, |
| 10 | + withTiming |
| 11 | +} from 'react-native-reanimated' |
| 12 | + |
| 13 | +import { useHandler } from '../../hooks/useHandler' |
| 14 | +import { useLayout } from '../../hooks/useLayout' |
| 15 | +import { styled } from '../hoc/styled' |
| 16 | +import { useTheme } from '../services/ThemeContext' |
| 17 | + |
| 18 | +interface Props { |
| 19 | + /** Whether the component is shown (rendered). Default: true */ |
| 20 | + isShown?: boolean |
| 21 | + |
| 22 | + /** Number of characters to represent in size for the shimmer. */ |
| 23 | + characters?: number |
| 24 | + |
| 25 | + /** Number of lines to represent in size for the shimmer. */ |
| 26 | + lines?: number |
| 27 | +} |
| 28 | + |
| 29 | +export const ShimmerText = (props: Props) => { |
| 30 | + const { isShown = true, characters, lines = 1 } = props |
| 31 | + const theme = useTheme() |
| 32 | + |
| 33 | + const containerHeight = React.useMemo( |
| 34 | + () => theme.rem(lines * 1.5), |
| 35 | + [lines, theme] |
| 36 | + ) |
| 37 | + const containerWidth = React.useMemo( |
| 38 | + () => |
| 39 | + characters |
| 40 | + ? characters * theme.rem(0.75) |
| 41 | + : Math.floor(Math.random() * 80) + 20 + '%', |
| 42 | + [characters, theme] |
| 43 | + ) |
| 44 | + |
| 45 | + const [containerLayout, handleContainerLayout] = useLayout() |
| 46 | + const containerLayoutWidth = containerLayout.width |
| 47 | + const gradientWidth = containerLayoutWidth * 6 |
| 48 | + |
| 49 | + const offset = useSharedValue(0) |
| 50 | + |
| 51 | + const startAnimation = useHandler(() => { |
| 52 | + const duration = 2000 |
| 53 | + const startPosition = -gradientWidth |
| 54 | + const endPosition = containerLayoutWidth |
| 55 | + offset.value = startPosition |
| 56 | + offset.value = withRepeat( |
| 57 | + withSequence( |
| 58 | + withTiming(startPosition, { duration: duration / 2 }), |
| 59 | + withTiming(endPosition, { duration }) |
| 60 | + ), |
| 61 | + -1, |
| 62 | + false |
| 63 | + ) |
| 64 | + }) |
| 65 | + |
| 66 | + React.useEffect(() => { |
| 67 | + if (gradientWidth > 0) startAnimation() |
| 68 | + }, [startAnimation, gradientWidth]) |
| 69 | + |
| 70 | + return isShown ? ( |
| 71 | + <ContainerView |
| 72 | + width={containerWidth} |
| 73 | + height={containerHeight} |
| 74 | + onLayout={handleContainerLayout} |
| 75 | + > |
| 76 | + <Shimmer width={gradientWidth} offset={offset}> |
| 77 | + <Gradient |
| 78 | + start={{ x: 0, y: 0 }} |
| 79 | + end={{ x: 1, y: 1 }} |
| 80 | + colors={['rgba(0,0,0,0)', theme.shimmerBackgroundHighlight]} |
| 81 | + /> |
| 82 | + <Gradient |
| 83 | + start={{ x: 1, y: 0 }} |
| 84 | + end={{ x: 0, y: 1 }} |
| 85 | + colors={['rgba(0,0,0,0)', theme.shimmerBackgroundHighlight]} |
| 86 | + /> |
| 87 | + </Shimmer> |
| 88 | + </ContainerView> |
| 89 | + ) : null |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * This is the track of the component that contains a gradient that overflows |
| 94 | + * in width. |
| 95 | + */ |
| 96 | +const ContainerView = styled(View)<{ |
| 97 | + width: string | number |
| 98 | + height: string | number |
| 99 | +}>(theme => props => ({ |
| 100 | + width: props.width, |
| 101 | + maxWidth: '100%', |
| 102 | + height: props.height, |
| 103 | + borderRadius: theme.rem(0.25), |
| 104 | + backgroundColor: theme.shimmerBackgroundColor, |
| 105 | + overflow: 'hidden' |
| 106 | +})) |
| 107 | + |
| 108 | +/** |
| 109 | + * This is the animated view that within the {@link ContainerView}. It animates |
| 110 | + * by an offset value which represents the horizontal position of the shimmer. |
| 111 | + */ |
| 112 | +const Shimmer = styled(Animated.View)<{ |
| 113 | + width: string | number |
| 114 | + offset: SharedValue<number> |
| 115 | +}>(_ => props => [ |
| 116 | + { |
| 117 | + position: 'absolute', |
| 118 | + top: 0, |
| 119 | + left: 0, |
| 120 | + bottom: 0, |
| 121 | + display: 'flex', |
| 122 | + flexDirection: 'row', |
| 123 | + width: props.width |
| 124 | + }, |
| 125 | + useAnimatedStyle(() => ({ |
| 126 | + transform: [{ translateX: props.offset.value }] |
| 127 | + })) |
| 128 | +]) |
| 129 | + |
| 130 | +/** |
| 131 | + * This is gradient nested within the {@link Shimmer}. |
| 132 | + */ |
| 133 | +const Gradient = styled(LinearGradient)({ |
| 134 | + flex: 1, |
| 135 | + width: '100%', |
| 136 | + height: '100%' |
| 137 | +}) |
0 commit comments