UNPKG

@heycar-uikit/core

Version:
88 lines (82 loc) 3.82 kB
import React, { useState, useEffect } from 'react'; import getCollapseStyles from './utils/getCollapseStyles.js'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function () { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; function useCollapse(open) { var collapseContent = React.useRef(null); var _a = useState({}), collapseStyles = _a[0], setCollapseStyles = _a[1]; var setExpandedStyle = function () { if (open) setCollapseStyles(function (styles) { return (__assign(__assign({}, styles), { height: 'auto', overflow: 'visible' })); }); }; var setCollapsedStyle = function () { return setCollapseStyles(function (styles) { var _a; return (__assign(__assign({}, styles), { height: ((_a = collapseContent.current) === null || _a === void 0 ? void 0 : _a.scrollHeight) + 'px' })); }); }; var handlerTransitionEnd = setExpandedStyle; useEffect(function () { var styles = getCollapseStyles(collapseContent, open); if (!open) setCollapsedStyle(); var animationFrameId = window.requestAnimationFrame(function () { return setTimeout(setCollapseStyles, 0, styles); }); return function () { return window.cancelAnimationFrame(animationFrameId); }; }, [open]); // Only for the first initialization useEffect(function () { var animationFrameId = window.requestAnimationFrame(function () { return setTimeout(function () { setCollapseStyles(function (styles) { return (__assign(__assign({}, styles), { overflow: open ? 'visible' : 'hidden' })); }); }, 0); }); return function () { return window.cancelAnimationFrame(animationFrameId); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return { collapseContent: collapseContent, collapseStyles: collapseStyles, handlerTransitionEnd: handlerTransitionEnd, }; } export { __rest as _, __assign as a, useCollapse as u };