forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_ValidationAnimationContainer.tsx
43 lines (37 loc) · 1.08 KB
/
_ValidationAnimationContainer.tsx
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
import React, {HTMLProps, useEffect, useState} from 'react'
import styled, {keyframes, css} from 'styled-components'
import {Box} from '.'
interface Props extends HTMLProps<HTMLDivElement> {
show?: boolean
}
const fadeIn = keyframes`
0% {
opacity: 0;
transform: translateY(-100%);
}
100% {
opacity: 1;
transform: translateY(0);
}
`
// using easeOutQuint easing fn https://easings.net/#easeOutQuint
const AnimatedElement = styled.div<Props>`
animation: ${props => props.show && css`170ms ${fadeIn} cubic-bezier(0.44, 0.74, 0.36, 1);`};
`
const ValidationAnimationContainer: React.FC<Props> = ({show, children}) => {
const [shouldRender, setRender] = useState(show)
useEffect(() => {
if (show) setRender(true)
}, [show])
const onAnimationEnd = () => {
if (!show) setRender(false)
}
return shouldRender ? (
<Box height={show ? 'auto' : 0} overflow="hidden">
<AnimatedElement show={show} onAnimationEnd={onAnimationEnd}>
{children}
</AnimatedElement>
</Box>
) : null
}
export default ValidationAnimationContainer