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