/
Collapse.js
332 lines (303 loc) · 9.4 KB
/
Collapse.js
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import * as React from 'react';
import clsx from 'clsx';
import PropTypes from 'prop-types';
import { chainPropTypes } from '@material-ui/utils';
import { Transition } from 'react-transition-group';
import withStyles from '../styles/withStyles';
import { duration } from '../styles/transitions';
import deprecatedPropType from '../utils/deprecatedPropType';
import { getTransitionProps } from '../transitions/utils';
import useTheme from '../styles/useTheme';
import { useForkRef } from '../utils';
export const styles = (theme) => ({
/* Styles applied to the root element. */
root: {
height: 0,
overflow: 'hidden',
transition: theme.transitions.create('height'),
},
/* Styles applied to the root element when the transition has entered. */
entered: {
height: 'auto',
overflow: 'visible',
},
/* Styles applied to the root element when the transition has exited and `collapsedSize` != 0px. */
hidden: {
visibility: 'hidden',
},
/* Styles applied to the outer wrapper element. */
wrapper: {
// Hack to get children with a negative margin to not falsify the height computation.
display: 'flex',
},
/* Styles applied to the inner wrapper element. */
wrapperInner: {
width: '100%',
},
});
/**
* The Collapse transition is used by the
* [Vertical Stepper](/components/steppers/#vertical-stepper) StepContent component.
* It uses [react-transition-group](https://github.com/reactjs/react-transition-group) internally.
*/
const Collapse = React.forwardRef(function Collapse(props, ref) {
const {
children,
classes,
className,
collapsedHeight,
collapsedSize: collapsedSizeProp = '0px',
component: Component = 'div',
disableStrictModeCompat = false,
in: inProp,
onEnter,
onEntered,
onEntering,
onExit,
onExited,
onExiting,
style,
timeout = duration.standard,
// eslint-disable-next-line react/prop-types
TransitionComponent = Transition,
...other
} = props;
const theme = useTheme();
const timer = React.useRef();
const wrapperRef = React.useRef(null);
const autoTransitionDuration = React.useRef();
const collapsedSize =
typeof (collapsedHeight || collapsedSizeProp) === 'number'
? `${collapsedHeight || collapsedSizeProp}px`
: collapsedHeight || collapsedSizeProp;
React.useEffect(() => {
return () => {
clearTimeout(timer.current);
};
}, []);
const enableStrictModeCompat = theme.unstable_strictMode && !disableStrictModeCompat;
const nodeRef = React.useRef(null);
const handleRef = useForkRef(ref, enableStrictModeCompat ? nodeRef : undefined);
const normalizedTransitionCallback = (callback) => (nodeOrAppearing, maybeAppearing) => {
if (callback) {
const [node, isAppearing] = enableStrictModeCompat
? [nodeRef.current, nodeOrAppearing]
: [nodeOrAppearing, maybeAppearing];
// onEnterXxx and onExitXxx callbacks have a different arguments.length value.
if (isAppearing === undefined) {
callback(node);
} else {
callback(node, isAppearing);
}
}
};
const handleEnter = normalizedTransitionCallback((node, isAppearing) => {
node.style.height = collapsedSize;
if (onEnter) {
onEnter(node, isAppearing);
}
});
const handleEntering = normalizedTransitionCallback((node, isAppearing) => {
const wrapperHeight = wrapperRef.current ? wrapperRef.current.clientHeight : 0;
const { duration: transitionDuration } = getTransitionProps(
{ style, timeout },
{
mode: 'enter',
},
);
if (timeout === 'auto') {
const duration2 = theme.transitions.getAutoHeightDuration(wrapperHeight);
node.style.transitionDuration = `${duration2}ms`;
autoTransitionDuration.current = duration2;
} else {
node.style.transitionDuration =
typeof transitionDuration === 'string' ? transitionDuration : `${transitionDuration}ms`;
}
node.style.height = `${wrapperHeight}px`;
if (onEntering) {
onEntering(node, isAppearing);
}
});
const handleEntered = normalizedTransitionCallback((node, isAppearing) => {
node.style.height = 'auto';
if (onEntered) {
onEntered(node, isAppearing);
}
});
const handleExit = normalizedTransitionCallback((node) => {
const wrapperHeight = wrapperRef.current ? wrapperRef.current.clientHeight : 0;
node.style.height = `${wrapperHeight}px`;
if (onExit) {
onExit(node);
}
});
const handleExited = normalizedTransitionCallback(onExited);
const handleExiting = normalizedTransitionCallback((node) => {
const wrapperHeight = wrapperRef.current ? wrapperRef.current.clientHeight : 0;
const { duration: transitionDuration } = getTransitionProps(
{ style, timeout },
{
mode: 'exit',
},
);
if (timeout === 'auto') {
const duration2 = theme.transitions.getAutoHeightDuration(wrapperHeight);
node.style.transitionDuration = `${duration2}ms`;
autoTransitionDuration.current = duration2;
} else {
node.style.transitionDuration =
typeof transitionDuration === 'string' ? transitionDuration : `${transitionDuration}ms`;
}
node.style.height = collapsedSize;
if (onExiting) {
onExiting(node);
}
});
const addEndListener = (nodeOrNext, maybeNext) => {
const next = enableStrictModeCompat ? nodeOrNext : maybeNext;
if (timeout === 'auto') {
timer.current = setTimeout(next, autoTransitionDuration.current || 0);
}
};
return (
<TransitionComponent
in={inProp}
onEnter={handleEnter}
onEntered={handleEntered}
onEntering={handleEntering}
onExit={handleExit}
onExited={handleExited}
onExiting={handleExiting}
addEndListener={addEndListener}
nodeRef={enableStrictModeCompat ? nodeRef : undefined}
timeout={timeout === 'auto' ? null : timeout}
{...other}
>
{(state, childProps) => (
<Component
className={clsx(
classes.root,
classes.container,
{
[classes.entered]: state === 'entered',
[classes.hidden]: state === 'exited' && !inProp && collapsedSize === '0px',
},
className,
)}
style={{
minHeight: collapsedSize,
...style,
}}
ref={handleRef}
{...childProps}
>
<div className={classes.wrapper} ref={wrapperRef}>
<div className={classes.wrapperInner}>{children}</div>
</div>
</Component>
)}
</TransitionComponent>
);
});
Collapse.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content node to be collapsed.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: chainPropTypes(PropTypes.object, (props) => {
if (props.classes && props.classes.container) {
throw new Error(
[
'Material-UI: the classes.container key is deprecated.',
'Use `classes.root` instead',
'The name of the pseudo-class was changed for consistency.',
].join('\n'),
);
}
return null;
}),
/**
* @ignore
*/
className: PropTypes.string,
/**
* The height of the container when collapsed.
* @deprecated The prop was renamed to support the addition of horizontal orientation, use `collapsedSize` instead.
*/
collapsedHeight: deprecatedPropType(
PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
'The prop was renamed to support the vertical orientation, use `collapsedSize` instead',
),
/**
* The height of the container when collapsed.
*/
collapsedSize: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes /* @typescript-to-proptypes-ignore */.elementType,
/**
* Enable this prop if you encounter 'Function components cannot be given refs',
* use `unstable_createStrictModeTheme`,
* and can't forward the ref in the passed `Component`.
*/
disableStrictModeCompat: PropTypes.bool,
/**
* If `true`, the component will transition in.
*/
in: PropTypes.bool,
/**
* @ignore
*/
onEnter: PropTypes.func,
/**
* @ignore
*/
onEntered: PropTypes.func,
/**
* @ignore
*/
onEntering: PropTypes.func,
/**
* @ignore
*/
onExit: PropTypes.func,
/**
* @ignore
*/
onExited: PropTypes.func,
/**
* @ignore
*/
onExiting: PropTypes.func,
/**
* @ignore
*/
style: PropTypes.object,
/**
* The duration for the transition, in milliseconds.
* You may specify a single timeout for all transitions, or individually with an object.
*
* Set to 'auto' to automatically calculate transition time based on height.
*/
timeout: PropTypes.oneOfType([
PropTypes.oneOf(['auto']),
PropTypes.number,
PropTypes.shape({
appear: PropTypes.number,
enter: PropTypes.number,
exit: PropTypes.number,
}),
]),
};
Collapse.muiSupportAuto = true;
export default withStyles(styles, { name: 'MuiCollapse' })(Collapse);