UNPKG

@ariakit/react-core

Version:

Ariakit React core

194 lines (169 loc) 6.97 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _YDPERDKFcjs = require('./YDPERDKF.cjs'); var _KVXNVDJKcjs = require('./KVXNVDJK.cjs'); var _25BPIGZHcjs = require('./25BPIGZH.cjs'); var _WULEED4Qcjs = require('./WULEED4Q.cjs'); var _OZM4QA2Vcjs = require('./OZM4QA2V.cjs'); var _7EQBAZ46cjs = require('./7EQBAZ46.cjs'); // src/disclosure/disclosure-content.tsx var _misc = require('@ariakit/core/utils/misc'); var _react = require('react'); var _reactdom = require('react-dom'); var _jsxruntime = require('react/jsx-runtime'); var TagName = "div"; function afterTimeout(timeoutMs, cb) { const timeoutId = setTimeout(cb, timeoutMs); return () => clearTimeout(timeoutId); } function afterPaint(cb) { let raf = requestAnimationFrame(() => { raf = requestAnimationFrame(cb); }); return () => cancelAnimationFrame(raf); } function parseCSSTime(...times) { return times.join(", ").split(", ").reduce((longestTime, currentTimeString) => { const multiplier = currentTimeString.endsWith("ms") ? 1 : 1e3; const currentTime = Number.parseFloat(currentTimeString || "0s") * multiplier; if (currentTime > longestTime) return currentTime; return longestTime; }, 0); } function isHidden(mounted, hidden, alwaysVisible) { return !alwaysVisible && hidden !== false && (!mounted || !!hidden); } var useDisclosureContent = _WULEED4Qcjs.createHook.call(void 0, function useDisclosureContent2(_a) { var _b = _a, { store, alwaysVisible } = _b, props = _7EQBAZ46cjs.__objRest.call(void 0, _b, ["store", "alwaysVisible"]); const context = _KVXNVDJKcjs.useDisclosureProviderContext.call(void 0, ); store = store || context; _misc.invariant.call(void 0, store, process.env.NODE_ENV !== "production" && "DisclosureContent must receive a `store` prop or be wrapped in a DisclosureProvider component." ); const ref = _react.useRef.call(void 0, null); const id = _OZM4QA2Vcjs.useId.call(void 0, props.id); const [transition, setTransition] = _react.useState.call(void 0, null); const open = store.useState("open"); const mounted = store.useState("mounted"); const animated = store.useState("animated"); const contentElement = store.useState("contentElement"); const otherElement = _25BPIGZHcjs.useStoreState.call(void 0, store.disclosure, "contentElement"); _OZM4QA2Vcjs.useSafeLayoutEffect.call(void 0, () => { if (!ref.current) return; store == null ? void 0 : store.setContentElement(ref.current); }, [store]); _OZM4QA2Vcjs.useSafeLayoutEffect.call(void 0, () => { let previousAnimated; store == null ? void 0 : store.setState("animated", (animated2) => { previousAnimated = animated2; return true; }); return () => { if (previousAnimated === void 0) return; store == null ? void 0 : store.setState("animated", previousAnimated); }; }, [store]); _OZM4QA2Vcjs.useSafeLayoutEffect.call(void 0, () => { if (!animated) return; if (!(contentElement == null ? void 0 : contentElement.isConnected)) { setTransition(null); return; } return afterPaint(() => { setTransition(open ? "enter" : mounted ? "leave" : null); }); }, [animated, contentElement, open, mounted]); _OZM4QA2Vcjs.useSafeLayoutEffect.call(void 0, () => { if (!store) return; if (!animated) return; if (!transition) return; if (!contentElement) return; const stopAnimation = () => store == null ? void 0 : store.setState("animating", false); const stopAnimationSync = () => _reactdom.flushSync.call(void 0, stopAnimation); if (transition === "leave" && open) return; if (transition === "enter" && !open) return; if (typeof animated === "number") { const timeout2 = animated; return afterTimeout(timeout2, stopAnimationSync); } const { transitionDuration, animationDuration, transitionDelay, animationDelay } = getComputedStyle(contentElement); const { transitionDuration: transitionDuration2 = "0", animationDuration: animationDuration2 = "0", transitionDelay: transitionDelay2 = "0", animationDelay: animationDelay2 = "0" } = otherElement ? getComputedStyle(otherElement) : {}; const delay = parseCSSTime( transitionDelay, animationDelay, transitionDelay2, animationDelay2 ); const duration = parseCSSTime( transitionDuration, animationDuration, transitionDuration2, animationDuration2 ); const timeout = delay + duration; if (!timeout) { if (transition === "enter") { store.setState("animated", false); } stopAnimation(); return; } const frameRate = 1e3 / 60; const maxTimeout = Math.max(timeout - frameRate, 0); return afterTimeout(maxTimeout, stopAnimationSync); }, [store, animated, contentElement, otherElement, open, transition]); props = _OZM4QA2Vcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _YDPERDKFcjs.DialogScopedContextProvider, { value: store, children: element }), [store] ); const hidden = isHidden(mounted, props.hidden, alwaysVisible); const styleProp = props.style; const style = _react.useMemo.call(void 0, () => { if (hidden) { return _7EQBAZ46cjs.__spreadProps.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {}, styleProp), { display: "none" }); } return styleProp; }, [hidden, styleProp]); props = _7EQBAZ46cjs.__spreadProps.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, { id, "data-open": open || void 0, "data-enter": transition === "enter" || void 0, "data-leave": transition === "leave" || void 0, hidden }, props), { ref: _OZM4QA2Vcjs.useMergeRefs.call(void 0, id ? store.setContentElement : null, ref, props.ref), style }); return _misc.removeUndefinedValues.call(void 0, props); }); var DisclosureContentImpl = _WULEED4Qcjs.forwardRef.call(void 0, function DisclosureContentImpl2(props) { const htmlProps = useDisclosureContent(props); return _WULEED4Qcjs.createElement.call(void 0, TagName, htmlProps); }); var DisclosureContent = _WULEED4Qcjs.forwardRef.call(void 0, function DisclosureContent2(_a) { var _b = _a, { unmountOnHide } = _b, props = _7EQBAZ46cjs.__objRest.call(void 0, _b, [ "unmountOnHide" ]); const context = _KVXNVDJKcjs.useDisclosureProviderContext.call(void 0, ); const store = props.store || context; const mounted = _25BPIGZHcjs.useStoreState.call(void 0, store, (state) => !unmountOnHide || (state == null ? void 0 : state.mounted) ); if (mounted === false) return null; return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, DisclosureContentImpl, _7EQBAZ46cjs.__spreadValues.call(void 0, {}, props)); }); exports.isHidden = isHidden; exports.useDisclosureContent = useDisclosureContent; exports.DisclosureContent = DisclosureContent;