/
transitions.tsx
163 lines (147 loc) · 4.04 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, {
Component,
ComponentType,
createRef,
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';
interface CollapseProps {
children: ReactNode;
in?: boolean;
onExited?: ExitHandler<undefined | HTMLElement>;
}
interface CollapseState {
width: Width;
}
// wrap each MultiValue with a collapse transition; decreases width until
// finally removing from DOM
export class Collapse extends Component<CollapseProps, CollapseState> {
duration = collapseDuration;
rafID?: number | null;
state: CollapseState = { width: 'auto' };
transition: { [K in TransitionStatus]?: CSSProperties } = {
exiting: { width: 0, transition: `width ${this.duration}ms ease-out` },
exited: { width: 0 },
};
nodeRef = createRef<HTMLDivElement>();
componentDidMount() {
const { current: ref } = this.nodeRef;
/*
A check on existence of ref should not be necessary at this point,
but TypeScript demands it.
*/
if (ref) {
/*
Here we're invoking requestAnimationFrame with a callback invoking our
call to getBoundingClientRect and setState 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.
*/
// cannot use `offsetWidth` because it is rounded
this.rafID = window.requestAnimationFrame(() => {
const { width } = ref.getBoundingClientRect();
this.setState({ width });
});
}
}
componentWillUnmount() {
if (this.rafID) {
window.cancelAnimationFrame(this.rafID);
}
}
// get base styles
getStyle = (width: Width): CSSProperties => ({
overflow: 'hidden',
whiteSpace: 'nowrap',
width,
});
// get transition styles
getTransition = (state: TransitionStatus) => this.transition[state];
render() {
const { children, in: inProp, onExited } = this.props;
const exitedProp = () => {
if (this.nodeRef.current && onExited) {
onExited(this.nodeRef.current);
}
};
const { width } = this.state;
return (
<Transition
enter={false}
mountOnEnter
unmountOnExit
in={inProp}
onExited={exitedProp}
timeout={this.duration}
nodeRef={this.nodeRef}
>
{(state) => {
const style = {
...this.getStyle(width),
...this.getTransition(state),
};
return (
<div ref={this.nodeRef} style={style}>
{children}
</div>
);
}}
</Transition>
);
}
}