UNPKG

@navinc/base-react-components

Version:
59 lines 3.04 kB
"use strict"; var __rest = (this && this.__rest) || function (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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnimateHeight = exports.AnimateHeightContainer = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const lodash_debounce_1 = __importDefault(require("lodash.debounce")); const styled_components_1 = __importDefault(require("styled-components")); exports.AnimateHeightContainer = styled_components_1.default.div.withConfig({ displayName: "brc-sc-AnimateHeightContainer", componentId: "brc-sc-uxntoa" }) ` overflow: hidden; height: ${({ height }) => (height === 'auto' ? 'auto' : `${Math.ceil(height)}px`)}; transition: height 0.4s ease-in-out; will-change: height; `; exports.AnimateHeightContainer.displayName = 'AnimateHeightContainer'; const AnimateHeight = (_a) => { var { children } = _a, props = __rest(_a, ["children"]); const [contentHeight, setContentHeight] = (0, react_1.useState)('auto'); const contentRef = (0, react_1.useRef)(null); const setHeight = (0, react_1.useMemo)(() => (0, lodash_debounce_1.default)(() => { window.requestAnimationFrame(() => { var _a, _b; setContentHeight((_b = (_a = contentRef === null || contentRef === void 0 ? void 0 : contentRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect().height) !== null && _b !== void 0 ? _b : 'auto'); }); }, 100), []); (0, react_1.useEffect)(() => { window.addEventListener('resize', setHeight); // ResizeObserver is not available in older browsers like Safari v12 const observer = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(setHeight) : undefined; if (observer) { observer.observe(contentRef.current); } return () => { setHeight.cancel(); window.removeEventListener('resize', setHeight); if (observer) { observer.disconnect(); } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return ((0, jsx_runtime_1.jsx)(exports.AnimateHeightContainer, Object.assign({}, props, { height: contentHeight }, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ ref: contentRef }, { children: children })) }))); }; exports.AnimateHeight = AnimateHeight; exports.default = exports.AnimateHeight; //# sourceMappingURL=animate-height.js.map