@material-ui/core
Version:
React components that implement Google's Material Design.
270 lines (233 loc) • 7.57 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React from 'react';
import clsx from 'clsx';
import PropTypes from 'prop-types';
import { Transition } from 'react-transition-group';
import withStyles from '../styles/withStyles';
import { duration } from '../styles/transitions';
import { getTransitionProps } from '../transitions/utils';
export var styles = function styles(theme) {
return {
/* Styles applied to the container element. */
container: {
height: 0,
overflow: 'hidden',
transition: theme.transitions.create('height')
},
/* Styles applied to the container element when the transition has entered. */
entered: {
height: 'auto',
overflow: 'visible'
},
/* Styles applied to the container element when the transition has exited and `collapsedHeight` != 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.
*/
var Collapse = React.forwardRef(function Collapse(props, ref) {
var children = props.children,
classes = props.classes,
className = props.className,
_props$collapsedHeigh = props.collapsedHeight,
collapsedHeight = _props$collapsedHeigh === void 0 ? '0px' : _props$collapsedHeigh,
_props$component = props.component,
Component = _props$component === void 0 ? 'div' : _props$component,
inProp = props.in,
onEnter = props.onEnter,
onEntered = props.onEntered,
onEntering = props.onEntering,
onExit = props.onExit,
onExiting = props.onExiting,
style = props.style,
theme = props.theme,
_props$timeout = props.timeout,
timeout = _props$timeout === void 0 ? duration.standard : _props$timeout,
other = _objectWithoutProperties(props, ["children", "classes", "className", "collapsedHeight", "component", "in", "onEnter", "onEntered", "onEntering", "onExit", "onExiting", "style", "theme", "timeout"]);
var timer = React.useRef();
var wrapperRef = React.useRef(null);
var autoTransitionDuration = React.useRef();
React.useEffect(function () {
return function () {
clearTimeout(timer.current);
};
}, []);
var handleEnter = function handleEnter(node) {
node.style.height = collapsedHeight;
if (onEnter) {
onEnter(node);
}
};
var handleEntering = function handleEntering(node) {
var wrapperHeight = wrapperRef.current ? wrapperRef.current.clientHeight : 0;
var _getTransitionProps = getTransitionProps({
style: style,
timeout: timeout
}, {
mode: 'enter'
}),
transitionDuration = _getTransitionProps.duration;
if (timeout === 'auto') {
var duration2 = theme.transitions.getAutoHeightDuration(wrapperHeight);
node.style.transitionDuration = "".concat(duration2, "ms");
autoTransitionDuration.current = duration2;
} else {
node.style.transitionDuration = typeof transitionDuration === 'string' ? transitionDuration : "".concat(transitionDuration, "ms");
}
node.style.height = "".concat(wrapperHeight, "px");
if (onEntering) {
onEntering(node);
}
};
var handleEntered = function handleEntered(node) {
node.style.height = 'auto';
if (onEntered) {
onEntered(node);
}
};
var handleExit = function handleExit(node) {
var wrapperHeight = wrapperRef.current ? wrapperRef.current.clientHeight : 0;
node.style.height = "".concat(wrapperHeight, "px");
if (onExit) {
onExit(node);
}
};
var handleExiting = function handleExiting(node) {
var wrapperHeight = wrapperRef.current ? wrapperRef.current.clientHeight : 0;
var _getTransitionProps2 = getTransitionProps({
style: style,
timeout: timeout
}, {
mode: 'exit'
}),
transitionDuration = _getTransitionProps2.duration;
if (timeout === 'auto') {
var duration2 = theme.transitions.getAutoHeightDuration(wrapperHeight);
node.style.transitionDuration = "".concat(duration2, "ms");
autoTransitionDuration.current = duration2;
} else {
node.style.transitionDuration = typeof transitionDuration === 'string' ? transitionDuration : "".concat(transitionDuration, "ms");
}
node.style.height = collapsedHeight;
if (onExiting) {
onExiting(node);
}
};
var addEndListener = function addEndListener(_, next) {
if (timeout === 'auto') {
timer.current = setTimeout(next, autoTransitionDuration.current || 0);
}
};
return React.createElement(Transition, _extends({
in: inProp,
onEnter: handleEnter,
onEntered: handleEntered,
onEntering: handleEntering,
onExit: handleExit,
onExiting: handleExiting,
addEndListener: addEndListener,
timeout: timeout === 'auto' ? null : timeout
}, other), function (state, childProps) {
return React.createElement(Component, _extends({
className: clsx(classes.container, className, {
entered: classes.entered,
exited: !inProp && collapsedHeight === '0px' && classes.hidden
}[state]),
style: _extends({
minHeight: collapsedHeight
}, style),
ref: ref
}, childProps), React.createElement("div", {
className: classes.wrapper,
ref: wrapperRef
}, React.createElement("div", {
className: classes.wrapperInner
}, children)));
});
});
process.env.NODE_ENV !== "production" ? Collapse.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: PropTypes.object.isRequired,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The height of the container when collapsed.
*/
collapsedHeight: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component: PropTypes.elementType,
/**
* 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
*/
onExiting: PropTypes.func,
/**
* @ignore
*/
style: PropTypes.object,
/**
* @ignore
*/
theme: PropTypes.object.isRequired,
/**
* 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.number, PropTypes.shape({
enter: PropTypes.number,
exit: PropTypes.number
}), PropTypes.oneOf(['auto'])])
} : void 0;
Collapse.muiSupportAuto = true;
export default withStyles(styles, {
withTheme: true,
name: 'MuiCollapse'
})(Collapse);