-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
transitions.tsx
148 lines (133 loc) · 3.83 KB
/
transitions.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
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
import * as React from 'react';
import { ComponentType, CSSProperties, ReactNode, useRef } from 'react';
import { Transition } from 'react-transition-group';
import {
ExitHandler,
TransitionStatus,
} from 'react-transition-group/Transition';
// ==============================
// Fade Transition
// ==============================
type FadeProps<ComponentProps> = {
component: ComponentType<ComponentProps>;
in?: boolean;
onExited?: ExitHandler<undefined | HTMLElement>;
duration?: number;
} & ComponentProps;
export const Fade = <ComponentProps extends {}>({
component: Tag,
duration = 1,
in: inProp,
onExited,
...props
}: FadeProps<ComponentProps>) => {
const nodeRef = useRef<HTMLElement>(null);
const transition: { [K in TransitionStatus]?: CSSProperties } = {
entering: { opacity: 0 },
entered: { opacity: 1, transition: `opacity ${duration}ms` },
exiting: { opacity: 0 },
exited: { opacity: 0 },
};
return (
<Transition
mountOnEnter
unmountOnExit
in={inProp}
timeout={duration}
nodeRef={nodeRef}
>
{(state) => {
const innerProps = {
style: {
...transition[state],
},
ref: nodeRef,
};
return <Tag innerProps={innerProps} {...(props as any)} />;
}}
</Transition>
);
};
// ==============================
// Collapse Transition
// ==============================
export const collapseDuration = 260;
type Width = number | 'auto';
/** Get base styles. */
function getBaseStyles(width: Width): CSSProperties {
return {
overflow: 'hidden',
whiteSpace: 'nowrap',
width,
};
}
const transition: { [K in TransitionStatus]?: CSSProperties } = {
exiting: { width: 0, transition: `width ${collapseDuration}ms ease-out` },
exited: { width: 0 },
};
/** Get styles based on the transition state. */
function getTransitionStyles(state: TransitionStatus) {
return transition[state];
}
interface CollapseProps {
/** The children to be rendered. */
children: ReactNode;
/** Show the component; triggers the enter or exit states. */
in?: boolean;
/** Callback fired after the "exited" status is applied. */
onExited?: ExitHandler<undefined | HTMLElement>;
}
/**
* Wrap each MultiValue with a collapse transition; decreases width until
* finally removing from DOM.
*/
export function Collapse({ children, in: inProp, onExited }: CollapseProps) {
const [width, setWidth] = React.useState<Width>('auto');
const nodeRef = useRef<HTMLDivElement>(null);
React.useEffect(() => {
/**
* Here we're invoking requestAnimationFrame with a callback invoking our
* call to getBoundingClientRect and setWidth in order to resolve an
* edge-case around portalling.
* Certain portalling solutions briefly remove children from the DOM before
* appending them to the target node. This is to avoid us trying to call
* getBoundingClientRect while the Select component is in this state.
*
* NOTE: we cannot use offsetWidth here because it is rounded.
*/
const id = window.requestAnimationFrame(() => {
if (nodeRef.current) {
setWidth(nodeRef.current.getBoundingClientRect().width);
}
});
return () => window.cancelAnimationFrame(id);
}, []);
const exitedProp = React.useCallback(() => {
if (nodeRef.current && onExited) {
onExited(nodeRef.current);
}
}, [onExited]);
return (
<Transition
enter={false}
mountOnEnter
unmountOnExit
in={inProp}
onExited={exitedProp}
timeout={collapseDuration}
nodeRef={nodeRef}
>
{(state) => {
const style = {
...getBaseStyles(width),
...getTransitionStyles(state),
};
return (
<div ref={nodeRef} style={style}>
{children}
</div>
);
}}
</Transition>
);
}