about summary refs log tree commit diff
path: root/src/lib/custom-animations/CountWheel.web.tsx
blob: 618dcb1a5deac153b5991153cc2d5293ba5dc4a4 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React from 'react'
import {View} from 'react-native'
import {useReducedMotion} from 'react-native-reanimated'
import {i18n} from '@lingui/core'

import {decideShouldRoll} from 'lib/custom-animations/util'
import {s} from 'lib/styles'
import {formatCount} from 'view/com/util/numeric/format'
import {Text} from 'view/com/util/text/Text'
import {atoms as a, useTheme} from '#/alf'

const animationConfig = {
  duration: 400,
  easing: 'cubic-bezier(0.4, 0, 0.2, 1)',
  fill: 'forwards' as FillMode,
}

const enteringUpKeyframe = [
  {opacity: 0, transform: 'translateY(18px)'},
  {opacity: 1, transform: 'translateY(0)'},
]

const enteringDownKeyframe = [
  {opacity: 0, transform: 'translateY(-18px)'},
  {opacity: 1, transform: 'translateY(0)'},
]

const exitingUpKeyframe = [
  {opacity: 1, transform: 'translateY(0)'},
  {opacity: 0, transform: 'translateY(-18px)'},
]

const exitingDownKeyframe = [
  {opacity: 1, transform: 'translateY(0)'},
  {opacity: 0, transform: 'translateY(18px)'},
]

export function CountWheel({
  likeCount,
  big,
  isLiked,
}: {
  likeCount: number
  big?: boolean
  isLiked: boolean
}) {
  const t = useTheme()
  const shouldAnimate = !useReducedMotion()
  const shouldRoll = decideShouldRoll(isLiked, likeCount)

  const countView = React.useRef<HTMLDivElement>(null)
  const prevCountView = React.useRef<HTMLDivElement>(null)

  const [prevCount, setPrevCount] = React.useState(likeCount)
  const prevIsLiked = React.useRef(isLiked)
  const formattedCount = formatCount(i18n, likeCount)
  const formattedPrevCount = formatCount(i18n, prevCount)

  React.useEffect(() => {
    if (isLiked === prevIsLiked.current) {
      return
    }

    const newPrevCount = isLiked ? likeCount - 1 : likeCount + 1
    if (shouldAnimate && shouldRoll) {
      countView.current?.animate?.(
        isLiked ? enteringUpKeyframe : enteringDownKeyframe,
        animationConfig,
      )
      prevCountView.current?.animate?.(
        isLiked ? exitingUpKeyframe : exitingDownKeyframe,
        animationConfig,
      )
      setPrevCount(newPrevCount)
    }
    prevIsLiked.current = isLiked
  }, [isLiked, likeCount, shouldAnimate, shouldRoll])

  if (likeCount < 1) {
    return null
  }

  return (
    <View>
      <View
        aria-disabled={true}
        // @ts-expect-error is div
        ref={countView}>
        <Text
          testID="likeCount"
          style={[
            big ? a.text_md : {fontSize: 15},
            a.user_select_none,
            isLiked
              ? [a.font_bold, s.likeColor]
              : {color: t.palette.contrast_500},
          ]}>
          {formattedCount}
        </Text>
      </View>
      {shouldAnimate ? (
        <View
          style={{position: 'absolute'}}
          aria-disabled={true}
          // @ts-expect-error is div
          ref={prevCountView}>
          <Text
            style={[
              big ? a.text_md : {fontSize: 15},
              a.user_select_none,
              isLiked
                ? [a.font_bold, s.likeColor]
                : {color: t.palette.contrast_500},
            ]}>
            {formattedPrevCount}
          </Text>
        </View>
      ) : null}
    </View>
  )
}