/
scale-fade.tsx
83 lines (78 loc) · 1.88 KB
/
scale-fade.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
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
import React from "react";
import Box, { BoxProps } from "../box/box";
import { AnimatePresence } from "moti";
export type ScaleFadeProps = BoxProps & {
/** Whether to show the component */
show: boolean;
/**
* The initial scale of the element.
*
* @default 0.7
*/
initialScale?: number;
};
/**
* ScaleFade is a component that provides an view with a scaling fade transition.
*/
const ScaleFade = React.memo(
React.forwardRef(
(
{
children,
show,
initialScale = 0.7,
transition = {},
exitTransition = {},
...rest
}: ScaleFadeProps,
ref: any
) => {
const fromStyle = React.useMemo(
() => ({ scale: initialScale, opacity: 0 }),
[initialScale]
);
const animateStyle = React.useMemo(() => ({ scale: 1, opacity: 1 }), []);
const exitStyle = React.useMemo(
() => ({ scale: initialScale, opacity: 0 }),
[initialScale]
);
const transitionProps = React.useMemo(
() => ({
dampingRatio: 1,
duration: 100,
type: "spring",
...(transition as any),
}),
[transition]
);
const exitTransitionProps = React.useMemo(
() => ({
dampingRatio: 1,
duration: 100,
type: "spring",
...(exitTransition as any),
}),
[exitTransition]
);
return (
<AnimatePresence>
{show && (
<Box
ref={ref}
{...rest}
from={fromStyle}
animate={animateStyle}
exit={exitStyle}
transition={transitionProps}
exitTransition={exitTransitionProps}
>
{children}
</Box>
)}
</AnimatePresence>
);
}
)
);
ScaleFade.displayName = "ScaleFade";
export default ScaleFade;