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
|
// Taken from https://github.com/chakra-ui/chakra-ui/blob/fc3b97d0978cf2adb9fc79157c6e42b4b68155c5/packages/transition/src/transition-utils.ts
import { isNumber } from '@chakra-ui/utils'
import { Target, TargetAndTransition, Transition } from 'framer-motion'
type TargetResolver<P = {}> = (
props: P & {
transition?: TransitionConfig
transitionEnd?: TransitionEndConfig
delay?: number | DelayConfig
},
) => TargetAndTransition
type Variant<P = {}> = TargetAndTransition | TargetResolver<P>
export type Variants<P = {}> = {
enter: Variant<P>
exit: Variant<P>
initial?: Variant<P>
}
type WithMotionState<P> = Partial<Record<'enter' | 'exit', P>>
export type TransitionConfig = WithMotionState<Transition>
export type TransitionEndConfig = WithMotionState<Target>
export type DelayConfig = WithMotionState<number>
export const TransitionEasings = {
ease: [0.25, 0.1, 0.25, 1],
easeIn: [0.4, 0, 1, 1],
easeOut: [0, 0, 0.2, 1],
easeInOut: [0.4, 0, 0.2, 1],
} as const
export const TransitionVariants = {
scale: {
enter: { scale: 1 },
exit: { scale: 0.95 },
},
fade: {
enter: { opacity: 1 },
exit: { opacity: 0 },
},
pushLeft: {
enter: { x: '100%' },
exit: { x: '-30%' },
},
pushRight: {
enter: { x: '-100%' },
exit: { x: '30%' },
},
pushUp: {
enter: { y: '100%' },
exit: { y: '-30%' },
},
pushDown: {
enter: { y: '-100%' },
exit: { y: '30%' },
},
slideLeft: {
position: { left: 0, top: 0, bottom: 0, width: '100%' },
enter: { x: 0 },
exit: { x: '-100%' },
},
slideRight: {
position: { right: 0, top: 0, bottom: 0, width: '100%' },
enter: { x: 0 },
exit: { x: '100%' },
},
slideUp: {
position: { top: 0, left: 0, right: 0, maxWidth: '100vw' },
enter: { y: 0 },
exit: { y: '-100%' },
},
slideDown: {
position: { bottom: 0, left: 0, right: 0, maxWidth: '100vw' },
enter: { y: 0 },
exit: { y: '100%' },
},
}
export type SlideDirection = 'top' | 'left' | 'bottom' | 'right'
export function slideTransition(options?: { direction?: SlideDirection }) {
const side = options?.direction ?? 'right'
switch (side) {
case 'right':
return TransitionVariants.slideRight
case 'left':
return TransitionVariants.slideLeft
case 'bottom':
return TransitionVariants.slideDown
case 'top':
return TransitionVariants.slideUp
default:
return TransitionVariants.slideRight
}
}
export const TransitionDefaults = {
enter: {
duration: 0.2,
ease: TransitionEasings.easeOut,
},
exit: {
duration: 0.1,
ease: TransitionEasings.easeIn,
},
} as const
export type WithTransitionConfig<P extends object> = Omit<P, 'transition'> & {
/**
* If `true`, the element will unmount when `in={false}` and animation is done
*/
unmountOnExit?: boolean
/**
* Show the component; triggers the enter or exit states
*/
in?: boolean
/**
* Custom `transition` definition for `enter` and `exit`
*/
transition?: TransitionConfig
/**
* Custom `transitionEnd` definition for `enter` and `exit`
*/
transitionEnd?: TransitionEndConfig
/**
* Custom `delay` definition for `enter` and `exit`
*/
delay?: number | DelayConfig
}
export const withDelay = {
enter: (transition: Transition, delay?: number | DelayConfig) => ({
...transition,
delay: isNumber(delay) ? delay : delay?.['enter'],
}),
exit: (transition: Transition, delay?: number | DelayConfig) => ({
...transition,
delay: isNumber(delay) ? delay : delay?.['exit'],
}),
}
|