about summary refs log tree commit diff
path: root/src/components/anim/AnimatedCheck.tsx
blob: 2198598ca22d29568573b8edfa8786dcb9db3b5d (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React from 'react'
import Animated, {
  Easing,
  useAnimatedProps,
  useSharedValue,
  withDelay,
  withTiming,
} from 'react-native-reanimated'
import Svg, {Circle, Path} from 'react-native-svg'

import {type Props, useCommonSVGProps} from '#/components/icons/common'

const AnimatedPath = Animated.createAnimatedComponent(Path)
const AnimatedCircle = Animated.createAnimatedComponent(Circle)

const PATH = 'M14.1 27.2l7.1 7.2 16.7-16.8'

export interface AnimatedCheckRef {
  play(cb?: () => void): void
}

export interface AnimatedCheckProps extends Props {
  playOnMount?: boolean
}

export const AnimatedCheck = React.forwardRef<
  AnimatedCheckRef,
  AnimatedCheckProps
>(function AnimatedCheck({playOnMount, ...props}, ref) {
  const {fill, size, style, ...rest} = useCommonSVGProps(props)
  const circleAnim = useSharedValue(0)
  const checkAnim = useSharedValue(0)

  const circleAnimatedProps = useAnimatedProps(() => ({
    strokeDashoffset: 166 - circleAnim.get() * 166,
  }))
  const checkAnimatedProps = useAnimatedProps(() => ({
    strokeDashoffset: 48 - 48 * checkAnim.get(),
  }))

  const play = React.useCallback(
    (cb?: () => void) => {
      circleAnim.set(0)
      checkAnim.set(0)

      circleAnim.set(() =>
        withTiming(1, {duration: 500, easing: Easing.linear}),
      )
      checkAnim.set(() =>
        withDelay(
          500,
          withTiming(1, {duration: 300, easing: Easing.linear}, cb),
        ),
      )
    },
    [circleAnim, checkAnim],
  )

  React.useImperativeHandle(ref, () => ({
    play,
  }))

  React.useEffect(() => {
    if (playOnMount) {
      play()
    }
  }, [play, playOnMount])

  return (
    <Svg
      fill="none"
      {...rest}
      viewBox="0 0 52 52"
      width={size}
      height={size}
      style={style}>
      <AnimatedCircle
        animatedProps={circleAnimatedProps}
        cx="26"
        cy="26"
        r="24"
        fill="none"
        stroke={fill}
        strokeWidth={4}
        strokeDasharray={166}
      />
      <AnimatedPath
        animatedProps={checkAnimatedProps}
        stroke={fill}
        d={PATH}
        strokeWidth={4}
        strokeDasharray={48}
      />
    </Svg>
  )
})