UNPKG

ivt

Version:

Ivt Components Library

134 lines (131 loc) 5.61 kB
import * as React from 'react'; import { c as composeEventHandlers } from './index-Bl-WJHvp.mjs'; import { c as createContextScope } from './index-DT8WgpCS.mjs'; import { u as useControllableState } from './index-D4FMFHi9.mjs'; import { u as useLayoutEffect2 } from './index-DKOlG3mh.mjs'; import { u as useComposedRefs } from './index-1tQVI0Jh.mjs'; import { P as Primitive } from './index-DgKlJYZP.mjs'; import { P as Presence } from './index-C6s8KI_8.mjs'; import { u as useId } from './index-tkRL9Tft.mjs'; import { jsx } from 'react/jsx-runtime'; var COLLAPSIBLE_NAME = "Collapsible"; var [createCollapsibleContext, createCollapsibleScope] = createContextScope(COLLAPSIBLE_NAME); var [CollapsibleProvider, useCollapsibleContext] = createCollapsibleContext(COLLAPSIBLE_NAME); var Collapsible = React.forwardRef((props, forwardedRef)=>{ const { __scopeCollapsible, open: openProp, defaultOpen, disabled, onOpenChange, ...collapsibleProps } = props; const [open, setOpen] = useControllableState({ prop: openProp, defaultProp: defaultOpen ?? false, onChange: onOpenChange, caller: COLLAPSIBLE_NAME }); return /* @__PURE__ */ jsx(CollapsibleProvider, { scope: __scopeCollapsible, disabled, contentId: useId(), open, onOpenToggle: React.useCallback(()=>setOpen((prevOpen)=>!prevOpen), [ setOpen ]), children: /* @__PURE__ */ jsx(Primitive.div, { "data-state": getState(open), "data-disabled": disabled ? "" : void 0, ...collapsibleProps, ref: forwardedRef }) }); }); Collapsible.displayName = COLLAPSIBLE_NAME; var TRIGGER_NAME = "CollapsibleTrigger"; var CollapsibleTrigger = React.forwardRef((props, forwardedRef)=>{ const { __scopeCollapsible, ...triggerProps } = props; const context = useCollapsibleContext(TRIGGER_NAME, __scopeCollapsible); return /* @__PURE__ */ jsx(Primitive.button, { type: "button", "aria-controls": context.contentId, "aria-expanded": context.open || false, "data-state": getState(context.open), "data-disabled": context.disabled ? "" : void 0, disabled: context.disabled, ...triggerProps, ref: forwardedRef, onClick: composeEventHandlers(props.onClick, context.onOpenToggle) }); }); CollapsibleTrigger.displayName = TRIGGER_NAME; var CONTENT_NAME = "CollapsibleContent"; var CollapsibleContent = React.forwardRef((props, forwardedRef)=>{ const { forceMount, ...contentProps } = props; const context = useCollapsibleContext(CONTENT_NAME, props.__scopeCollapsible); return /* @__PURE__ */ jsx(Presence, { present: forceMount || context.open, children: ({ present })=>/* @__PURE__ */ jsx(CollapsibleContentImpl, { ...contentProps, ref: forwardedRef, present }) }); }); CollapsibleContent.displayName = CONTENT_NAME; var CollapsibleContentImpl = React.forwardRef((props, forwardedRef)=>{ const { __scopeCollapsible, present, children, ...contentProps } = props; const context = useCollapsibleContext(CONTENT_NAME, __scopeCollapsible); const [isPresent, setIsPresent] = React.useState(present); const ref = React.useRef(null); const composedRefs = useComposedRefs(forwardedRef, ref); const heightRef = React.useRef(0); const height = heightRef.current; const widthRef = React.useRef(0); const width = widthRef.current; const isOpen = context.open || isPresent; const isMountAnimationPreventedRef = React.useRef(isOpen); const originalStylesRef = React.useRef(void 0); React.useEffect(()=>{ const rAF = requestAnimationFrame(()=>isMountAnimationPreventedRef.current = false); return ()=>cancelAnimationFrame(rAF); }, []); useLayoutEffect2(()=>{ const node = ref.current; if (node) { originalStylesRef.current = originalStylesRef.current || { transitionDuration: node.style.transitionDuration, animationName: node.style.animationName }; node.style.transitionDuration = "0s"; node.style.animationName = "none"; const rect = node.getBoundingClientRect(); heightRef.current = rect.height; widthRef.current = rect.width; if (!isMountAnimationPreventedRef.current) { node.style.transitionDuration = originalStylesRef.current.transitionDuration; node.style.animationName = originalStylesRef.current.animationName; } setIsPresent(present); } }, [ context.open, present ]); return /* @__PURE__ */ jsx(Primitive.div, { "data-state": getState(context.open), "data-disabled": context.disabled ? "" : void 0, id: context.contentId, hidden: !isOpen, ...contentProps, ref: composedRefs, style: { [`--radix-collapsible-content-height`]: height ? `${height}px` : void 0, [`--radix-collapsible-content-width`]: width ? `${width}px` : void 0, ...props.style }, children: isOpen && children }); }); function getState(open) { return open ? "open" : "closed"; } var Root = Collapsible; var Trigger = CollapsibleTrigger; var Content = CollapsibleContent; export { CollapsibleTrigger as C, Root as R, Trigger as T, CollapsibleContent as a, Content as b, createCollapsibleScope as c }; //# sourceMappingURL=index-DWJ_2Xiy.mjs.map