about summary refs log tree commit diff
path: root/src/view/com/util/LoadingPlaceholder.tsx
blob: 55b6ad1b3df2f383f25dfa5e117f25becae44e77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, {useEffect, useMemo} from 'react'
import {
  Animated,
  StyleProp,
  useWindowDimensions,
  View,
  ViewStyle,
} from 'react-native'
import LinearGradient from 'react-native-linear-gradient'
import {colors} from '../../lib/styles'

export function LoadingPlaceholder({
  width,
  height,
  style,
}: {
  width: string | number
  height: string | number
  style?: StyleProp<ViewStyle>
}) {
  const dim = useWindowDimensions()
  const elWidth = typeof width === 'string' ? dim.width : width
  const offset = useMemo(() => new Animated.Value(elWidth * -1), [])
  useEffect(() => {
    const anim = Animated.loop(
      Animated.sequence([
        Animated.timing(offset, {
          toValue: elWidth,
          duration: 1e3,
          useNativeDriver: true,
          isInteraction: false,
        }),
        Animated.timing(offset, {
          toValue: elWidth * -1,
          duration: 0,
          delay: 500,
          useNativeDriver: true,
          isInteraction: false,
        }),
      ]),
    )
    anim.start()
    return () => anim.stop()
  }, [])

  return (
    <View
      style={[
        {
          width,
          height,
          backgroundColor: colors.gray2,
          borderRadius: 6,
          overflow: 'hidden',
        },
        style,
      ]}>
      <Animated.View
        style={{
          width,
          height,
          transform: [{translateX: offset}],
        }}>
        <LinearGradient
          colors={[colors.gray2, '#d4d2d2', colors.gray2]}
          start={{x: 0, y: 0}}
          end={{x: 1, y: 0}}
          style={{width: '100%', height: '100%'}}
        />
      </Animated.View>
    </View>
  )
}