@loke/ui
Version:
To install dependencies:
111 lines (110 loc) • 4.99 kB
JavaScript
// src/components/collapsible/collapsible.tsx
import { composeEventHandlers } from "@loke/ui/compose-events";
import { useComposedRefs } from "@loke/ui/compose-refs";
import { createContextScope } from "@loke/ui/context";
import { Presence } from "@loke/ui/presence";
import { Primitive } from "@loke/ui/primitive";
import { useControllableState } from "@loke/ui/use-controllable-state";
import { useId } from "@loke/ui/use-id";
import { useLayoutEffect } from "@loke/ui/use-layout-effect";
import {
forwardRef,
useCallback,
useEffect,
useRef,
useState
} from "react";
import { jsx } from "react/jsx-runtime";
var COLLAPSIBLE_NAME = "Collapsible", [createCollapsibleContext, createCollapsibleScope] = createContextScope(COLLAPSIBLE_NAME), [CollapsibleProvider, useCollapsibleContext] = createCollapsibleContext(COLLAPSIBLE_NAME), Collapsible = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let {
__scopeCollapsible,
open: openProp,
defaultOpen,
disabled,
onOpenChange,
...collapsibleProps
} = props, [open, setOpen] = useControllableState({
caller: COLLAPSIBLE_NAME,
defaultProp: defaultOpen ?? !1,
onChange: onOpenChange,
prop: openProp
});
return /* @__PURE__ */ jsx(CollapsibleProvider, {
contentId: useId(),
disabled,
onOpenToggle: useCallback(() => setOpen((prevOpen) => !prevOpen), [setOpen]),
open,
scope: __scopeCollapsible,
children: /* @__PURE__ */ jsx(Primitive.div, {
"data-disabled": disabled ? "" : void 0,
"data-state": getState(open),
...collapsibleProps,
ref: forwardedRef
})
});
}), { displayName: COLLAPSIBLE_NAME }), TRIGGER_NAME = "CollapsibleTrigger", CollapsibleTrigger = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let { __scopeCollapsible, ...triggerProps } = props, context = useCollapsibleContext(TRIGGER_NAME, __scopeCollapsible);
return /* @__PURE__ */ jsx(Primitive.button, {
"aria-controls": context.open ? context.contentId : void 0,
"aria-expanded": context.open,
"data-disabled": context.disabled ? "" : void 0,
"data-state": getState(context.open),
disabled: context.disabled,
...triggerProps,
onClick: composeEventHandlers(props.onClick, context.onOpenToggle),
ref: forwardedRef,
type: "button"
});
}), { displayName: TRIGGER_NAME }), CONTENT_NAME = "CollapsibleContent", CollapsibleContent = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let { forceMount, ...contentProps } = props, context = useCollapsibleContext(CONTENT_NAME, props.__scopeCollapsible);
return /* @__PURE__ */ jsx(Presence, {
present: forceMount || context.open,
children: ({ present }) => /* @__PURE__ */ jsx(CollapsibleContentImpl, {
...contentProps,
present,
ref: forwardedRef
})
});
}), { displayName: CONTENT_NAME }), CollapsibleContentImpl = /* @__PURE__ */ forwardRef((props, forwardedRef) => {
let { __scopeCollapsible, present, children, ...contentProps } = props, context = useCollapsibleContext(CONTENT_NAME, __scopeCollapsible), [isPresent, setIsPresent] = useState(present), ref = useRef(null), composedRefs = useComposedRefs(forwardedRef, ref), heightRef = useRef(0), height = heightRef.current, widthRef = useRef(0), width = widthRef.current, isOpen = context.open || isPresent, isMountAnimationPreventedRef = useRef(isOpen), originalStylesRef = useRef(void 0);
return useEffect(() => {
let rAF = requestAnimationFrame(() => {
isMountAnimationPreventedRef.current = !1;
});
return () => cancelAnimationFrame(rAF);
}, []), useLayoutEffect(() => {
let node = ref.current;
if (node) {
originalStylesRef.current = originalStylesRef.current || {
animationName: node.style.animationName,
transitionDuration: node.style.transitionDuration
}, node.style.transitionDuration = "0s", node.style.animationName = "none";
let rect = node.getBoundingClientRect();
if (heightRef.current = rect.height, widthRef.current = rect.width, !isMountAnimationPreventedRef.current)
node.style.transitionDuration = originalStylesRef.current.transitionDuration, node.style.animationName = originalStylesRef.current.animationName;
setIsPresent(present);
}
}, [context.open, present]), /* @__PURE__ */ jsx(Primitive.div, {
"data-disabled": context.disabled ? "" : void 0,
"data-state": getState(context.open),
hidden: !isOpen,
id: context.contentId,
...contentProps,
ref: composedRefs,
style: {
"--loke-collapsible-content-height": height ? `${height}px` : void 0,
"--loke-collapsible-content-width": width ? `${width}px` : void 0,
...props.style
},
children: isOpen && children
});
});
function getState(open) {
return open ? "open" : "closed";
}
export {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
createCollapsibleScope
};