UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

130 lines (126 loc) 3.3 kB
"use client"; import { utils_exports } from "../../utils/index.js"; import { createComponent } from "../../core/components/create-component.js"; import { collapseStyle } from "./collapse.style.js"; import { motion as motion$1 } from "../motion/factory.js"; import { createTransition } from "../motion/transition.js"; import { useEffect, useMemo, useState } from "react"; import { jsx } from "react/jsx-runtime"; import { AnimatePresence } from "motion/react"; //#region src/components/collapse/collapse.tsx const variants = { enter: ({ animationOpacity, delay, duration, endingHeight: height, enter, transition, transitionEnd } = {}) => ({ ...animationOpacity ? { opacity: 1 } : {}, height, transition: createTransition.enter(transition?.enter)(delay, duration), transitionEnd: transitionEnd?.enter, ...enter }), exit: ({ animationOpacity, delay, duration, exit, startingHeight: height, transition, transitionEnd } = {}) => ({ ...animationOpacity ? { opacity: (0, utils_exports.isNumeric)(height) ? 1 : 0 } : {}, height, transition: createTransition.exit(transition?.exit)(delay, duration), transitionEnd: transitionEnd?.exit, ...exit }) }; const { PropsContext: CollapsePropsContext, usePropsContext: useCollapsePropsContext, withContext } = createComponent("collapse", collapseStyle); /** * `Collapse` is a component that allows you to expand or collapse an element for display. * * @see https://yamada-ui.com/docs/components/collapse */ const Collapse = withContext(({ style, animationOpacity = true, delay, duration, endingHeight = "auto", open: openProp, startingHeight = 0, transition: transitionProp, transitionEnd, unmountOnExit,...rest }) => { const [mounted, setMounted] = useState(false); const animate = openProp || unmountOnExit ? "enter" : "exit"; const open = unmountOnExit ? openProp : true; const transition = useMemo(() => { if (!mounted) return { enter: { duration: 0 } }; else if (transitionProp) return transitionProp; else return { enter: { height: { duration: duration ?? .3, ease: [ .25, .1, .25, 1 ] }, opacity: { duration: duration ?? .4, ease: [ .25, .1, .25, 1 ] } }, exit: { height: { duration: duration ?? .3, ease: [ .25, .1, .25, 1 ] }, opacity: { duration: duration ?? .4, ease: [ .25, .1, .25, 1 ] } } }; }, [ mounted, duration, transitionProp ]); const custom = useMemo(() => ({ animationOpacity, delay, duration, endingHeight, startingHeight, transition, transitionEnd }), [ animationOpacity, delay, duration, endingHeight, startingHeight, transition, transitionEnd ]); useEffect(() => { if ((0, utils_exports.createdDom)()) setMounted(true); }, []); return /* @__PURE__ */ jsx(AnimatePresence, { custom, initial: false, children: open ? /* @__PURE__ */ jsx(motion$1.div, { style: { overflow: "hidden", ...style }, animate, custom, exit: "exit", initial: unmountOnExit ? "exit" : false, variants, ...rest }) : null }); })(); //#endregion export { Collapse, CollapsePropsContext, useCollapsePropsContext }; //# sourceMappingURL=collapse.js.map