UNPKG

@coreui/react

Version:

UI Components Library for React.js

75 lines (72 loc) 2.99 kB
import { __rest, __assign } from '../../node_modules/tslib/tslib.es6.js'; import React, { forwardRef, useRef, useState } from 'react'; import PropTypes from 'prop-types'; import classNames from '../../_virtual/index.js'; import { useForkedRef } from '../../hooks/useForkedRef.js'; import '@popperjs/core'; import CSSTransition from '../../node_modules/react-transition-group/esm/CSSTransition.js'; var CCollapse = forwardRef(function (_a, ref) { var children = _a.children, className = _a.className, horizontal = _a.horizontal, onHide = _a.onHide, onShow = _a.onShow, visible = _a.visible, rest = __rest(_a, ["children", "className", "horizontal", "onHide", "onShow", "visible"]); var collapseRef = useRef(null); var forkedRef = useForkedRef(ref, collapseRef); var _b = useState(), height = _b[0], setHeight = _b[1]; var _c = useState(), width = _c[0], setWidth = _c[1]; var onEntering = function () { onShow && onShow(); if (horizontal) { collapseRef.current && setWidth(collapseRef.current.scrollWidth); return; } collapseRef.current && setHeight(collapseRef.current.scrollHeight); }; var onEntered = function () { if (horizontal) { setWidth(0); return; } setHeight(0); }; var onExit = function () { if (horizontal) { collapseRef.current && setWidth(collapseRef.current.scrollWidth); return; } collapseRef.current && setHeight(collapseRef.current.scrollHeight); }; var onExiting = function () { onHide && onHide(); if (horizontal) { setWidth(0); return; } setHeight(0); }; var onExited = function () { if (horizontal) { setWidth(0); return; } setHeight(0); }; return (React.createElement(CSSTransition, { in: visible, nodeRef: collapseRef, onEntering: onEntering, onEntered: onEntered, onExit: onExit, onExiting: onExiting, onExited: onExited, timeout: 350 }, function (state) { var currentHeight = height === 0 ? null : { height: height }; var currentWidth = width === 0 ? null : { width: width }; return (React.createElement("div", __assign({ className: classNames(className, { 'collapse-horizontal': horizontal, collapsing: state === 'entering' || state === 'exiting', 'collapse show': state === 'entered', collapse: state === 'exited', }), style: __assign(__assign({}, currentHeight), currentWidth) }, rest, { ref: forkedRef }), children)); })); }); CCollapse.propTypes = { children: PropTypes.node, className: PropTypes.string, horizontal: PropTypes.bool, onHide: PropTypes.func, onShow: PropTypes.func, visible: PropTypes.bool, }; CCollapse.displayName = 'CCollapse'; export { CCollapse }; //# sourceMappingURL=CCollapse.js.map