1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
3 | var _excluded = ["onEnter", "onEntering", "onEntered", "onExit", "onExiting", "className", "children", "dimension", "getDimensionValue"];
|
4 |
|
5 | var _collapseStyles;
|
6 |
|
7 | import classNames from 'classnames';
|
8 | import css from 'dom-helpers/css';
|
9 | import React, { useMemo } from 'react';
|
10 | import Transition, { ENTERED, ENTERING, EXITED, EXITING } from 'react-transition-group/Transition';
|
11 | import transitionEndListener from './transitionEndListener';
|
12 | import createChainedFunction from './createChainedFunction';
|
13 | import triggerBrowserReflow from './triggerBrowserReflow';
|
14 | var MARGINS = {
|
15 | height: ['marginTop', 'marginBottom'],
|
16 | width: ['marginLeft', 'marginRight']
|
17 | };
|
18 |
|
19 | function getDefaultDimensionValue(dimension, elem) {
|
20 | var offset = "offset" + dimension[0].toUpperCase() + dimension.slice(1);
|
21 | var value = elem[offset];
|
22 | var margins = MARGINS[dimension];
|
23 | return value +
|
24 | parseInt(css(elem, margins[0]), 10) +
|
25 | parseInt(css(elem, margins[1]), 10);
|
26 | }
|
27 |
|
28 | var collapseStyles = (_collapseStyles = {}, _collapseStyles[EXITED] = 'collapse', _collapseStyles[EXITING] = 'collapsing', _collapseStyles[ENTERING] = 'collapsing', _collapseStyles[ENTERED] = 'collapse show', _collapseStyles);
|
29 | var defaultProps = {
|
30 | in: false,
|
31 | timeout: 300,
|
32 | mountOnEnter: false,
|
33 | unmountOnExit: false,
|
34 | appear: false,
|
35 | getDimensionValue: getDefaultDimensionValue
|
36 | };
|
37 | var Collapse = React.forwardRef(function (_ref, ref) {
|
38 | var onEnter = _ref.onEnter,
|
39 | onEntering = _ref.onEntering,
|
40 | onEntered = _ref.onEntered,
|
41 | onExit = _ref.onExit,
|
42 | onExiting = _ref.onExiting,
|
43 | className = _ref.className,
|
44 | children = _ref.children,
|
45 | _ref$dimension = _ref.dimension,
|
46 | dimension = _ref$dimension === void 0 ? 'height' : _ref$dimension,
|
47 | _ref$getDimensionValu = _ref.getDimensionValue,
|
48 | getDimensionValue = _ref$getDimensionValu === void 0 ? getDefaultDimensionValue : _ref$getDimensionValu,
|
49 | props = _objectWithoutPropertiesLoose(_ref, _excluded);
|
50 |
|
51 |
|
52 | var computedDimension = typeof dimension === 'function' ? dimension() : dimension;
|
53 |
|
54 |
|
55 | var handleEnter = useMemo(function () {
|
56 | return createChainedFunction(function (elem) {
|
57 | elem.style[computedDimension] = '0';
|
58 | }, onEnter);
|
59 | }, [computedDimension, onEnter]);
|
60 | var handleEntering = useMemo(function () {
|
61 | return createChainedFunction(function (elem) {
|
62 | var scroll = "scroll" + computedDimension[0].toUpperCase() + computedDimension.slice(1);
|
63 | elem.style[computedDimension] = elem[scroll] + "px";
|
64 | }, onEntering);
|
65 | }, [computedDimension, onEntering]);
|
66 | var handleEntered = useMemo(function () {
|
67 | return createChainedFunction(function (elem) {
|
68 | elem.style[computedDimension] = null;
|
69 | }, onEntered);
|
70 | }, [computedDimension, onEntered]);
|
71 |
|
72 |
|
73 | var handleExit = useMemo(function () {
|
74 | return createChainedFunction(function (elem) {
|
75 | elem.style[computedDimension] = getDimensionValue(computedDimension, elem) + "px";
|
76 | triggerBrowserReflow(elem);
|
77 | }, onExit);
|
78 | }, [onExit, getDimensionValue, computedDimension]);
|
79 | var handleExiting = useMemo(function () {
|
80 | return createChainedFunction(function (elem) {
|
81 | elem.style[computedDimension] = null;
|
82 | }, onExiting);
|
83 | }, [computedDimension, onExiting]);
|
84 | return React.createElement(Transition
|
85 | , _extends({
|
86 | ref: ref,
|
87 | addEndListener: transitionEndListener
|
88 | }, props, {
|
89 | "aria-expanded": props.role ? props.in : null,
|
90 | onEnter: handleEnter,
|
91 | onEntering: handleEntering,
|
92 | onEntered: handleEntered,
|
93 | onExit: handleExit,
|
94 | onExiting: handleExiting
|
95 | }), function (state, innerProps) {
|
96 | return React.cloneElement(children, _extends({}, innerProps, {
|
97 | className: classNames(className, children.props.className, collapseStyles[state], computedDimension === 'width' && 'width')
|
98 | }));
|
99 | });
|
100 | });
|
101 |
|
102 |
|
103 | Collapse.defaultProps = defaultProps;
|
104 | export default Collapse; |
\ | No newline at end of file |