UNPKG

@chayns-components/core

Version:

A set of beautiful React components for developing your own applications with chayns.

106 lines (105 loc) 3.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _DelayedDropdownContent = require("./DelayedDropdownContent.styles"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const ANIMATION_DELAY_MS = 200; var AnimationType = /*#__PURE__*/function (AnimationType) { AnimationType[AnimationType["None"] = 0] = "None"; AnimationType[AnimationType["Hidden"] = 1] = "Hidden"; AnimationType[AnimationType["Visible"] = 2] = "Visible"; AnimationType[AnimationType["FadeIn"] = 3] = "FadeIn"; AnimationType[AnimationType["FadeOut"] = 4] = "FadeOut"; return AnimationType; }(AnimationType || {}); const DelayedDropdownContent = ({ children, shouldShowContent, onMeasure, coordinates, transform }) => { const [animationState, setAnimationState] = (0, _react.useState)(AnimationType.None); const ref = (0, _react.useRef)(); const timeoutRef = (0, _react.useRef)(); const measureElement = (0, _react.useCallback)(() => { if (ref.current) { const { height, width, x, y } = ref.current.getBoundingClientRect(); const { scrollHeight } = ref.current; if (typeof onMeasure === 'function') { onMeasure({ x, y, height, scrollHeight, width, element: ref.current }); } setAnimationState(AnimationType.FadeIn); timeoutRef.current = window.setTimeout(() => { setAnimationState(AnimationType.Visible); }, ANIMATION_DELAY_MS); } }, [onMeasure]); (0, _react.useEffect)(() => { if (!shouldShowContent) return () => {}; const observer = new ResizeObserver(() => { measureElement(); }); if (ref.current) { observer.observe(ref.current); } return () => observer.disconnect(); }, [measureElement, shouldShowContent]); (0, _react.useEffect)(() => { if (shouldShowContent) { setAnimationState(AnimationType.Hidden); } else { clearTimeout(timeoutRef.current); setAnimationState(prevState => { if (prevState === AnimationType.None) { return prevState; } return AnimationType.FadeOut; }); window.setTimeout(() => { setAnimationState(AnimationType.None); }, ANIMATION_DELAY_MS); } }, [measureElement, shouldShowContent]); const refCallback = (0, _react.useCallback)(reference => { ref.current = reference ?? undefined; measureElement(); }, [measureElement]); if (animationState === AnimationType.None) { return null; } return /*#__PURE__*/_react.default.createElement(_DelayedDropdownContent.StyledMotionDelayedDropdownContent, { ref: refCallback, $coordinates: coordinates, $transform: transform, $shouldHideContent: animationState === AnimationType.Hidden, animate: { opacity: [AnimationType.FadeIn, AnimationType.Visible].includes(animationState) ? 1 : 0 }, transition: { duration: ANIMATION_DELAY_MS / 1000, type: 'tween' }, inert: !shouldShowContent ? 'true' : undefined }, children); }; DelayedDropdownContent.displayName = 'DelayedDropdownContent'; var _default = exports.default = DelayedDropdownContent; //# sourceMappingURL=DelayedDropdownContent.js.map