-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
collapse.tsx
232 lines (205 loc) · 7.9 KB
/
collapse.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright 2015 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import classNames from "classnames";
import * as React from "react";
import { AbstractPureComponent } from "../../common/abstractPureComponent";
import * as Classes from "../../common/classes";
import { DISPLAYNAME_PREFIX, IProps } from "../../common/props";
export interface ICollapseProps extends IProps {
/**
* Component to render as the root element.
* Useful when rendering a `Collapse` inside a `<table>`, for instance.
* @default "div"
*/
component?: React.ReactType;
/**
* Whether the component is open or closed.
* @default false
*/
isOpen?: boolean;
/**
* Whether the child components will remain mounted when the `Collapse` is closed.
* Setting to true may improve performance by avoiding re-mounting children.
* @default false
*/
keepChildrenMounted?: boolean;
/**
* The length of time the transition takes, in milliseconds. This must match
* the duration of the animation in CSS. Only set this prop if you override
* Blueprint's default transitions with new transitions of a different
* length.
* @default 200
*/
transitionDuration?: number;
}
export interface ICollapseState {
/** The height that should be used for the content animations. This is a CSS value, not just a number. */
height: string;
/** The state the element is currently in. */
animationState: AnimationStates;
}
/**
* `Collapse` can be in one of six states, enumerated here.
* When changing the `isOpen` prop, the following happens to the states:
* isOpen={true} : CLOSED -> OPEN_START -> OPENING -> OPEN
* isOpen={false} : OPEN -> CLOSING_START -> CLOSING -> CLOSED
*/
export enum AnimationStates {
/**
* The body is re-rendered, height is set to the measured body height and
* the body Y is set to 0.
*/
OPEN_START,
/**
* Animation begins, height is set to auto. This is all animated, and on
* complete, the state changes to OPEN.
*/
OPENING,
/**
* The collapse height is set to auto, and the body Y is set to 0 (so the
* element can be seen as normal).
*/
OPEN,
/**
* Height has been changed from auto to the measured height of the body to
* prepare for the closing animation in CLOSING.
*/
CLOSING_START,
/**
* Height is set to 0 and the body Y is at -height. Both of these properties
* are transformed, and then after the animation is complete, the state
* changes to CLOSED.
*/
CLOSING,
/**
* The contents of the collapse is not rendered, the collapse height is 0,
* and the body Y is at -height (so that the bottom of the body is at Y=0).
*/
CLOSED,
}
export class Collapse extends AbstractPureComponent<ICollapseProps, ICollapseState> {
public static displayName = `${DISPLAYNAME_PREFIX}.Collapse`;
public static defaultProps: ICollapseProps = {
component: "div",
isOpen: false,
keepChildrenMounted: false,
transitionDuration: 200,
};
public state = {
animationState: this.props.isOpen ? AnimationStates.OPEN : AnimationStates.CLOSED,
height: "0px",
};
// The element containing the contents of the collapse.
private contents: HTMLElement;
// The most recent non-0 height (once a height has been measured - is 0 until then)
private height: number = 0;
public componentWillReceiveProps(nextProps: ICollapseProps) {
if (this.props.isOpen !== nextProps.isOpen) {
this.clearTimeouts();
if (this.state.animationState !== AnimationStates.CLOSED && !nextProps.isOpen) {
this.setState({
animationState: AnimationStates.CLOSING_START,
height: `${this.height}px`,
});
} else if (this.state.animationState !== AnimationStates.OPEN && nextProps.isOpen) {
this.setState({
animationState: AnimationStates.OPEN_START,
});
}
}
}
public render() {
const isContentVisible = this.state.animationState !== AnimationStates.CLOSED;
const shouldRenderChildren = isContentVisible || this.props.keepChildrenMounted;
const displayWithTransform = isContentVisible && this.state.animationState !== AnimationStates.CLOSING;
const isAutoHeight = this.state.height === "auto";
const containerStyle = {
height: isContentVisible ? this.state.height : undefined,
overflowY: (isAutoHeight ? "visible" : undefined) as "visible" | undefined,
transition: isAutoHeight ? "none" : undefined,
};
const contentsStyle = {
transform: displayWithTransform ? "translateY(0)" : `translateY(-${this.height}px)`,
transition: isAutoHeight ? "none" : undefined,
};
return React.createElement(
this.props.component,
{
className: classNames(Classes.COLLAPSE, this.props.className),
style: containerStyle,
},
<div
className={Classes.COLLAPSE_BODY}
ref={this.contentsRefHandler}
style={contentsStyle}
aria-hidden={!isContentVisible && this.props.keepChildrenMounted}
>
{shouldRenderChildren ? this.props.children : null}
</div>,
);
}
public componentDidMount() {
this.forceUpdate();
if (this.props.isOpen) {
this.setState({ animationState: AnimationStates.OPEN, height: "auto" });
} else {
this.setState({ animationState: AnimationStates.CLOSED });
}
}
public componentDidUpdate() {
if (this.contents != null && this.contents.clientHeight !== 0) {
this.height = this.contents.clientHeight;
}
if (this.state.animationState === AnimationStates.CLOSING_START) {
this.setTimeout(() =>
this.setState({
animationState: AnimationStates.CLOSING,
height: "0px",
}),
);
this.setTimeout(() => this.onDelayedStateChange(), this.props.transitionDuration);
}
if (this.state.animationState === AnimationStates.OPEN_START) {
this.setState({
animationState: AnimationStates.OPENING,
height: this.height + "px",
});
this.setTimeout(() => this.onDelayedStateChange(), this.props.transitionDuration);
}
}
private contentsRefHandler = (el: HTMLElement) => {
this.contents = el;
if (el != null) {
this.height = this.contents.clientHeight;
this.setState({
animationState: this.props.isOpen ? AnimationStates.OPEN : AnimationStates.CLOSED,
height: `${this.height}px`,
});
}
};
private onDelayedStateChange() {
switch (this.state.animationState) {
case AnimationStates.OPENING:
this.setState({ animationState: AnimationStates.OPEN, height: "auto" });
break;
case AnimationStates.CLOSING:
this.setState({ animationState: AnimationStates.CLOSED });
break;
default:
break;
}
}
}