-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtransition.tsx
More file actions
executable file
·135 lines (115 loc) · 2.97 KB
/
transition.tsx
File metadata and controls
executable file
·135 lines (115 loc) · 2.97 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
import React from 'react';
import useTransition, { TransitionState } from './use-transition';
export type AnimationName =
| 'zoom-center-top'
| 'zoom-center-bottom'
| 'zoom-center-left'
| 'zoom-center-right'
| 'zoom-top-start'
| 'zoom-top'
| 'zoom-top-end'
| 'zoom-bottom-start'
| 'zoom-bottom'
| 'zoom-bottom-end'
| 'zoom-left-start'
| 'zoom-left'
| 'zoom-left-end'
| 'zoom-right-start'
| 'zoom-right'
| 'zoom-right-end'
| 'slide-up'
| 'slide-down';
export type TransitionProps = {
in?: boolean;
timeout?: number | { enter: number; exit: number };
appear?: boolean;
unmountOnExit?: boolean;
mountOnEnter?: boolean;
/** Animation prefix */
prefix?: string;
/** Preset animation name */
animation?: AnimationName;
/** Custom class name base (overrides prefix + animation) */
classNames?: string;
/** Prevent the transition conflict with the inner component */
wrapper?: boolean;
nodeRef?: React.RefObject<HTMLElement | null>;
onEnter?: () => void;
onEntering?: () => void;
onEntered?: () => void;
onExit?: () => void;
onExiting?: () => void;
onExited?: () => void;
children?: React.ReactNode;
};
function getTransitionClasses(base: string, state: TransitionState): string {
switch (state) {
case 'enter':
return `${base}-enter`;
case 'entering':
return `${base}-enter ${base}-enter-active`;
case 'entered':
return `${base}-enter-done`;
case 'exit':
return `${base}-exit`;
case 'exiting':
return `${base}-exit ${base}-exit-active`;
case 'exited':
return `${base}-exit-done`;
default:
return '';
}
}
const Transition = (props: TransitionProps): React.ReactElement | null => {
const {
in: inProp = false,
timeout = 300,
unmountOnExit = true,
mountOnEnter,
appear = true,
prefix = 'ty',
animation,
classNames: classNamesProp,
nodeRef,
children,
wrapper,
onEnter,
onEntering,
onEntered,
onExit,
onExiting,
onExited,
} = props;
const { state, shouldMount } = useTransition({
in: inProp,
timeout,
appear,
unmountOnExit,
mountOnEnter,
onEnter,
onEntering,
onEntered,
onExit,
onExiting,
onExited,
nodeRef,
});
if (!shouldMount) {
return null;
}
const base = classNamesProp ? classNamesProp : `${prefix}-${animation}`;
const transitionClasses = getTransitionClasses(base, state);
const child = wrapper ? <div>{children}</div> : children;
if (React.isValidElement(child)) {
const existingClassName = (child.props as { className?: string }).className || '';
const mergedClassName = existingClassName
? `${existingClassName} ${transitionClasses}`.trim()
: transitionClasses;
return React.cloneElement(child as React.ReactElement<{ className?: string }>, {
className: mergedClassName || undefined,
});
}
return child as React.ReactElement;
};
Transition.displayName = 'Transition';
export default Transition;