-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathAnimatedButtonProvider.tsx
More file actions
184 lines (161 loc) · 4.32 KB
/
AnimatedButtonProvider.tsx
File metadata and controls
184 lines (161 loc) · 4.32 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {
memo,
useState,
useEffect,
useRef,
useCallback,
MouseEvent
} from 'react'
import { useInstanceVar } from '@audius/common/hooks'
import cn from 'classnames'
import Lottie, { LottieRefCurrentProps } from 'lottie-react'
import { SeoLink } from 'components/link'
import { applyThemeToLottie } from 'utils/lottieTheme'
import { useLottieThemeColors } from 'utils/theme/theme'
import styles from './AnimatedButtonProvider.module.css'
type BaseAnimatedButtonProps = {
href?: string
onClick: ((e: MouseEvent) => void) | (() => void)
uniqueKey: string
isActive: boolean
isDisabled?: boolean
className?: string
disabledClassName?: string
activeClassName?: string
wrapperClassName?: string
stopPropagation?: boolean
isMatrix: boolean
// If we mount in the active state,
// should we animate that transition or not?
disableAnimationOnMount?: boolean
}
type IconJSON = any
type AnimatedButtonProps = {
iconJSON: IconJSON
} & BaseAnimatedButtonProps
const AnimatedButton = ({
iconJSON,
onClick,
href,
isActive,
isMatrix,
isDisabled = false,
className = '',
activeClassName = '',
disabledClassName = '',
wrapperClassName = '',
stopPropagation = false,
disableAnimationOnMount = true,
uniqueKey,
...buttonProps
}: AnimatedButtonProps) => {
const [isPaused, setIsPaused] = useState(true)
const [getDidMount, setDidMount] = useInstanceVar(false)
useEffect(() => {
if (isActive) {
if (!disableAnimationOnMount || getDidMount()) {
setIsPaused(false)
}
} else {
setIsPaused(true)
}
}, [isActive, disableAnimationOnMount, getDidMount])
useEffect(() => {
setDidMount(true)
}, [setDidMount])
const handleClick = useCallback(
(e: MouseEvent) => {
e.preventDefault()
if (stopPropagation) {
e.stopPropagation()
}
if (isDisabled) return
setImmediate(() => {
onClick(e)
})
},
[isDisabled, onClick, stopPropagation]
)
const animationRef = useRef<LottieRefCurrentProps>(null)
useEffect(() => {
if (animationRef.current) {
if (isPaused) {
animationRef.current.pause()
} else {
animationRef.current.play()
}
}
}, [animationRef, isPaused])
const buttonElement = (
<div className={cn(wrapperClassName)}>
<Lottie
lottieRef={animationRef}
animationData={iconJSON}
loop={false}
autoplay={false}
onComplete={() => {
if (!isDisabled) {
setIsPaused(true)
}
}}
/>
</div>
)
const rootProps = {
onClick: handleClick,
className: cn(styles.baseStyle, className, {
[activeClassName]: isActive,
[disabledClassName]: isDisabled,
[styles.glow]: isActive && isMatrix
})
}
return href ? (
<SeoLink to={href} {...rootProps} {...buttonProps}>
{buttonElement}
</SeoLink>
) : (
<button {...rootProps} {...buttonProps}>
{buttonElement}
</button>
)
}
export type LottieThemeVariant = 'accent' | 'neutral'
export type AnimatedButtonProviderProps = {
/** @deprecated No longer used - theme applied via useLottieThemeColors */
darkMode?: boolean
isMatrix: boolean
/** Base Lottie JSON loader - theme colors applied at runtime */
iconJSON: () => Promise<IconJSON>
/** 'accent' = active state, 'neutral' = inactive state. Defaults from isActive. */
variant?: LottieThemeVariant
} & BaseAnimatedButtonProps
const AnimatedButtonProvider = ({
iconJSON: iconJSONLoader,
isActive,
variant: variantProp,
...buttonProps
}: AnimatedButtonProviderProps) => {
const [iconJSON, setIconJSON] = useState<IconJSON | null>(null)
const baseAnimation = useRef<IconJSON | null>(null)
const themeColors = useLottieThemeColors()
const variant = variantProp ?? (isActive ? 'accent' : 'neutral')
useEffect(() => {
const loadAndTheme = async () => {
if (!baseAnimation.current) {
baseAnimation.current = await iconJSONLoader()
}
setIconJSON(
applyThemeToLottie(
baseAnimation.current,
themeColors,
variant
) as IconJSON
)
}
loadAndTheme()
}, [iconJSONLoader, themeColors, variant])
return iconJSON ? (
<AnimatedButton iconJSON={iconJSON} isActive={isActive} {...buttonProps} />
) : null
}
export default memo(AnimatedButtonProvider)