UNPKG

14.2 kBJavaScriptView Raw
1'use client';
2
3import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
5import _extends from "@babel/runtime/helpers/esm/extends";
6import * as React from 'react';
7import clsx from 'clsx';
8import PropTypes from 'prop-types';
9import { Transition } from 'react-transition-group';
10import useTimeout from '@mui/utils/useTimeout';
11import elementTypeAcceptingRef from '@mui/utils/elementTypeAcceptingRef';
12import composeClasses from '@mui/utils/composeClasses';
13import styled from '../styles/styled';
14import { useDefaultProps } from '../DefaultPropsProvider';
15import { duration } from '../styles/createTransitions';
16import { getTransitionProps } from '../transitions/utils';
17import useTheme from '../styles/useTheme';
18import { useForkRef } from '../utils';
19import { getCollapseUtilityClass } from './collapseClasses';
20import { jsx as _jsx } from "react/jsx-runtime";
21var useUtilityClasses = function useUtilityClasses(ownerState) {
22 var orientation = ownerState.orientation,
23 classes = ownerState.classes;
24 var slots = {
25 root: ['root', "".concat(orientation)],
26 entered: ['entered'],
27 hidden: ['hidden'],
28 wrapper: ['wrapper', "".concat(orientation)],
29 wrapperInner: ['wrapperInner', "".concat(orientation)]
30 };
31 return composeClasses(slots, getCollapseUtilityClass, classes);
32};
33var CollapseRoot = styled('div', {
34 name: 'MuiCollapse',
35 slot: 'Root',
36 overridesResolver: function overridesResolver(props, styles) {
37 var ownerState = props.ownerState;
38 return [styles.root, styles[ownerState.orientation], ownerState.state === 'entered' && styles.entered, ownerState.state === 'exited' && !ownerState.in && ownerState.collapsedSize === '0px' && styles.hidden];
39 }
40})(function (_ref) {
41 var theme = _ref.theme,
42 ownerState = _ref.ownerState;
43 return _extends({
44 height: 0,
45 overflow: 'hidden',
46 transition: theme.transitions.create('height')
47 }, ownerState.orientation === 'horizontal' && {
48 height: 'auto',
49 width: 0,
50 transition: theme.transitions.create('width')
51 }, ownerState.state === 'entered' && _extends({
52 height: 'auto',
53 overflow: 'visible'
54 }, ownerState.orientation === 'horizontal' && {
55 width: 'auto'
56 }), ownerState.state === 'exited' && !ownerState.in && ownerState.collapsedSize === '0px' && {
57 visibility: 'hidden'
58 });
59});
60var CollapseWrapper = styled('div', {
61 name: 'MuiCollapse',
62 slot: 'Wrapper',
63 overridesResolver: function overridesResolver(props, styles) {
64 return styles.wrapper;
65 }
66})(function (_ref2) {
67 var ownerState = _ref2.ownerState;
68 return _extends({
69 // Hack to get children with a negative margin to not falsify the height computation.
70 display: 'flex',
71 width: '100%'
72 }, ownerState.orientation === 'horizontal' && {
73 width: 'auto',
74 height: '100%'
75 });
76});
77var CollapseWrapperInner = styled('div', {
78 name: 'MuiCollapse',
79 slot: 'WrapperInner',
80 overridesResolver: function overridesResolver(props, styles) {
81 return styles.wrapperInner;
82 }
83})(function (_ref3) {
84 var ownerState = _ref3.ownerState;
85 return _extends({
86 width: '100%'
87 }, ownerState.orientation === 'horizontal' && {
88 width: 'auto',
89 height: '100%'
90 });
91});
92
93/**
94 * The Collapse transition is used by the
95 * [Vertical Stepper](/material-ui/react-stepper/#vertical-stepper) StepContent component.
96 * It uses [react-transition-group](https://github.com/reactjs/react-transition-group) internally.
97 */
98var Collapse = /*#__PURE__*/React.forwardRef(function Collapse(inProps, ref) {
99 var props = useDefaultProps({
100 props: inProps,
101 name: 'MuiCollapse'
102 });
103 var addEndListener = props.addEndListener,
104 _children = props.children,
105 className = props.className,
106 _props$collapsedSize = props.collapsedSize,
107 collapsedSizeProp = _props$collapsedSize === void 0 ? '0px' : _props$collapsedSize,
108 component = props.component,
109 easing = props.easing,
110 inProp = props.in,
111 onEnter = props.onEnter,
112 onEntered = props.onEntered,
113 onEntering = props.onEntering,
114 onExit = props.onExit,
115 onExited = props.onExited,
116 onExiting = props.onExiting,
117 _props$orientation = props.orientation,
118 orientation = _props$orientation === void 0 ? 'vertical' : _props$orientation,
119 style = props.style,
120 _props$timeout = props.timeout,
121 timeout = _props$timeout === void 0 ? duration.standard : _props$timeout,
122 _props$TransitionComp = props.TransitionComponent,
123 TransitionComponent = _props$TransitionComp === void 0 ? Transition : _props$TransitionComp,
124 other = _objectWithoutProperties(props, ["addEndListener", "children", "className", "collapsedSize", "component", "easing", "in", "onEnter", "onEntered", "onEntering", "onExit", "onExited", "onExiting", "orientation", "style", "timeout", "TransitionComponent"]);
125 var ownerState = _extends({}, props, {
126 orientation: orientation,
127 collapsedSize: collapsedSizeProp
128 });
129 var classes = useUtilityClasses(ownerState);
130 var theme = useTheme();
131 var timer = useTimeout();
132 var wrapperRef = React.useRef(null);
133 var autoTransitionDuration = React.useRef();
134 var collapsedSize = typeof collapsedSizeProp === 'number' ? "".concat(collapsedSizeProp, "px") : collapsedSizeProp;
135 var isHorizontal = orientation === 'horizontal';
136 var size = isHorizontal ? 'width' : 'height';
137 var nodeRef = React.useRef(null);
138 var handleRef = useForkRef(ref, nodeRef);
139 var normalizedTransitionCallback = function normalizedTransitionCallback(callback) {
140 return function (maybeIsAppearing) {
141 if (callback) {
142 var node = nodeRef.current;
143
144 // onEnterXxx and onExitXxx callbacks have a different arguments.length value.
145 if (maybeIsAppearing === undefined) {
146 callback(node);
147 } else {
148 callback(node, maybeIsAppearing);
149 }
150 }
151 };
152 };
153 var getWrapperSize = function getWrapperSize() {
154 return wrapperRef.current ? wrapperRef.current[isHorizontal ? 'clientWidth' : 'clientHeight'] : 0;
155 };
156 var handleEnter = normalizedTransitionCallback(function (node, isAppearing) {
157 if (wrapperRef.current && isHorizontal) {
158 // Set absolute position to get the size of collapsed content
159 wrapperRef.current.style.position = 'absolute';
160 }
161 node.style[size] = collapsedSize;
162 if (onEnter) {
163 onEnter(node, isAppearing);
164 }
165 });
166 var handleEntering = normalizedTransitionCallback(function (node, isAppearing) {
167 var wrapperSize = getWrapperSize();
168 if (wrapperRef.current && isHorizontal) {
169 // After the size is read reset the position back to default
170 wrapperRef.current.style.position = '';
171 }
172 var _getTransitionProps = getTransitionProps({
173 style: style,
174 timeout: timeout,
175 easing: easing
176 }, {
177 mode: 'enter'
178 }),
179 transitionDuration = _getTransitionProps.duration,
180 transitionTimingFunction = _getTransitionProps.easing;
181 if (timeout === 'auto') {
182 var duration2 = theme.transitions.getAutoHeightDuration(wrapperSize);
183 node.style.transitionDuration = "".concat(duration2, "ms");
184 autoTransitionDuration.current = duration2;
185 } else {
186 node.style.transitionDuration = typeof transitionDuration === 'string' ? transitionDuration : "".concat(transitionDuration, "ms");
187 }
188 node.style[size] = "".concat(wrapperSize, "px");
189 node.style.transitionTimingFunction = transitionTimingFunction;
190 if (onEntering) {
191 onEntering(node, isAppearing);
192 }
193 });
194 var handleEntered = normalizedTransitionCallback(function (node, isAppearing) {
195 node.style[size] = 'auto';
196 if (onEntered) {
197 onEntered(node, isAppearing);
198 }
199 });
200 var handleExit = normalizedTransitionCallback(function (node) {
201 node.style[size] = "".concat(getWrapperSize(), "px");
202 if (onExit) {
203 onExit(node);
204 }
205 });
206 var handleExited = normalizedTransitionCallback(onExited);
207 var handleExiting = normalizedTransitionCallback(function (node) {
208 var wrapperSize = getWrapperSize();
209 var _getTransitionProps2 = getTransitionProps({
210 style: style,
211 timeout: timeout,
212 easing: easing
213 }, {
214 mode: 'exit'
215 }),
216 transitionDuration = _getTransitionProps2.duration,
217 transitionTimingFunction = _getTransitionProps2.easing;
218 if (timeout === 'auto') {
219 // TODO: rename getAutoHeightDuration to something more generic (width support)
220 // Actually it just calculates animation duration based on size
221 var duration2 = theme.transitions.getAutoHeightDuration(wrapperSize);
222 node.style.transitionDuration = "".concat(duration2, "ms");
223 autoTransitionDuration.current = duration2;
224 } else {
225 node.style.transitionDuration = typeof transitionDuration === 'string' ? transitionDuration : "".concat(transitionDuration, "ms");
226 }
227 node.style[size] = collapsedSize;
228 node.style.transitionTimingFunction = transitionTimingFunction;
229 if (onExiting) {
230 onExiting(node);
231 }
232 });
233 var handleAddEndListener = function handleAddEndListener(next) {
234 if (timeout === 'auto') {
235 timer.start(autoTransitionDuration.current || 0, next);
236 }
237 if (addEndListener) {
238 // Old call signature before `react-transition-group` implemented `nodeRef`
239 addEndListener(nodeRef.current, next);
240 }
241 };
242 return /*#__PURE__*/_jsx(TransitionComponent, _extends({
243 in: inProp,
244 onEnter: handleEnter,
245 onEntered: handleEntered,
246 onEntering: handleEntering,
247 onExit: handleExit,
248 onExited: handleExited,
249 onExiting: handleExiting,
250 addEndListener: handleAddEndListener,
251 nodeRef: nodeRef,
252 timeout: timeout === 'auto' ? null : timeout
253 }, other, {
254 children: function children(state, childProps) {
255 return /*#__PURE__*/_jsx(CollapseRoot, _extends({
256 as: component,
257 className: clsx(classes.root, className, {
258 'entered': classes.entered,
259 'exited': !inProp && collapsedSize === '0px' && classes.hidden
260 }[state]),
261 style: _extends(_defineProperty({}, isHorizontal ? 'minWidth' : 'minHeight', collapsedSize), style),
262 ref: handleRef
263 }, childProps, {
264 // `ownerState` is set after `childProps` to override any existing `ownerState` property in `childProps`
265 // that might have been forwarded from the Transition component.
266 ownerState: _extends({}, ownerState, {
267 state: state
268 }),
269 children: /*#__PURE__*/_jsx(CollapseWrapper, {
270 ownerState: _extends({}, ownerState, {
271 state: state
272 }),
273 className: classes.wrapper,
274 ref: wrapperRef,
275 children: /*#__PURE__*/_jsx(CollapseWrapperInner, {
276 ownerState: _extends({}, ownerState, {
277 state: state
278 }),
279 className: classes.wrapperInner,
280 children: _children
281 })
282 })
283 }));
284 }
285 }));
286});
287process.env.NODE_ENV !== "production" ? Collapse.propTypes /* remove-proptypes */ = {
288 // ┌────────────────────────────── Warning ──────────────────────────────┐
289 // │ These PropTypes are generated from the TypeScript type definitions. │
290 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
291 // └─────────────────────────────────────────────────────────────────────┘
292 /**
293 * Add a custom transition end trigger. Called with the transitioning DOM
294 * node and a done callback. Allows for more fine grained transition end
295 * logic. Note: Timeouts are still used as a fallback if provided.
296 */
297 addEndListener: PropTypes.func,
298 /**
299 * The content node to be collapsed.
300 */
301 children: PropTypes.node,
302 /**
303 * Override or extend the styles applied to the component.
304 */
305 classes: PropTypes.object,
306 /**
307 * @ignore
308 */
309 className: PropTypes.string,
310 /**
311 * The width (horizontal) or height (vertical) of the container when collapsed.
312 * @default '0px'
313 */
314 collapsedSize: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
315 /**
316 * The component used for the root node.
317 * Either a string to use a HTML element or a component.
318 */
319 component: elementTypeAcceptingRef,
320 /**
321 * The transition timing function.
322 * You may specify a single easing or a object containing enter and exit values.
323 */
324 easing: PropTypes.oneOfType([PropTypes.shape({
325 enter: PropTypes.string,
326 exit: PropTypes.string
327 }), PropTypes.string]),
328 /**
329 * If `true`, the component will transition in.
330 */
331 in: PropTypes.bool,
332 /**
333 * @ignore
334 */
335 onEnter: PropTypes.func,
336 /**
337 * @ignore
338 */
339 onEntered: PropTypes.func,
340 /**
341 * @ignore
342 */
343 onEntering: PropTypes.func,
344 /**
345 * @ignore
346 */
347 onExit: PropTypes.func,
348 /**
349 * @ignore
350 */
351 onExited: PropTypes.func,
352 /**
353 * @ignore
354 */
355 onExiting: PropTypes.func,
356 /**
357 * The transition orientation.
358 * @default 'vertical'
359 */
360 orientation: PropTypes.oneOf(['horizontal', 'vertical']),
361 /**
362 * @ignore
363 */
364 style: PropTypes.object,
365 /**
366 * The system prop that allows defining system overrides as well as additional CSS styles.
367 */
368 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
369 /**
370 * The duration for the transition, in milliseconds.
371 * You may specify a single timeout for all transitions, or individually with an object.
372 *
373 * Set to 'auto' to automatically calculate transition time based on height.
374 * @default duration.standard
375 */
376 timeout: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number, PropTypes.shape({
377 appear: PropTypes.number,
378 enter: PropTypes.number,
379 exit: PropTypes.number
380 })])
381} : void 0;
382Collapse.muiSupportAuto = true;
383export default Collapse;
\No newline at end of file