@ariakit/react-core
Version:
Ariakit React core
186 lines (165 loc) • 6.53 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _5WCU5NVKcjs = require('./5WCU5NVK.cjs');
var _57Z4CQLBcjs = require('./57Z4CQLB.cjs');
var _EMYYI4CZcjs = require('./EMYYI4CZ.cjs');
var _WBFXWJUHcjs = require('./WBFXWJUH.cjs');
var _MZ2HG624cjs = require('./MZ2HG624.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 = _WBFXWJUHcjs.createHook.call(void 0, function useDisclosureContent2({ store, alwaysVisible, ...props }) {
const context = _57Z4CQLBcjs.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 = _MZ2HG624cjs.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 = _EMYYI4CZcjs.useStoreState.call(void 0, store.disclosure, "contentElement");
_MZ2HG624cjs.useSafeLayoutEffect.call(void 0, () => {
if (!ref.current) return;
store == null ? void 0 : store.setContentElement(ref.current);
}, [store]);
_MZ2HG624cjs.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]);
_MZ2HG624cjs.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]);
_MZ2HG624cjs.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 = _MZ2HG624cjs.useWrapElement.call(void 0,
props,
(element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _5WCU5NVKcjs.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 { ...styleProp, display: "none" };
}
return styleProp;
}, [hidden, styleProp]);
props = {
id,
"data-open": open || void 0,
"data-enter": transition === "enter" || void 0,
"data-leave": transition === "leave" || void 0,
hidden,
...props,
ref: _MZ2HG624cjs.useMergeRefs.call(void 0, id ? store.setContentElement : null, ref, props.ref),
style
};
return _misc.removeUndefinedValues.call(void 0, props);
});
var DisclosureContentImpl = _WBFXWJUHcjs.forwardRef.call(void 0, function DisclosureContentImpl2(props) {
const htmlProps = useDisclosureContent(props);
return _WBFXWJUHcjs.createElement.call(void 0, TagName, htmlProps);
});
var DisclosureContent = _WBFXWJUHcjs.forwardRef.call(void 0, function DisclosureContent2({
unmountOnHide,
...props
}) {
const context = _57Z4CQLBcjs.useDisclosureProviderContext.call(void 0, );
const store = props.store || context;
const mounted = _EMYYI4CZcjs.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, { ...props });
});
exports.isHidden = isHidden; exports.useDisclosureContent = useDisclosureContent; exports.DisclosureContent = DisclosureContent;