/** * @collapsed/react v5.0.0 * * Copyright (c) 2019-2024, Rogin Farrer * * This source code is licensed under the MIT license found in the * LICENSE.md file in the root directory of this source tree. * * @license MIT */ "use strict";var $=Object.create;var m=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var N=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var j=(e,n)=>{for(var t in n)m(e,t,{get:n[t],enumerable:!0})},P=(e,n,t,u)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of F(n))!W.call(e,a)&&a!==t&&m(e,a,{get:()=>n[a],enumerable:!(u=z(n,a))||u.enumerable});return e};var E=(e,n,t)=>(t=e!=null?$(N(e)):{},P(n||!e||!e.__esModule?m(t,"default",{value:e,enumerable:!0}):t,e)),q=e=>P(m({},"__esModule",{value:!0}),e);var Y={};j(Y,{useCollapse:()=>X});module.exports=q(Y);var l=E(require("react"),1),H=require("@collapsed/core");var G=E(require("react"),1),r=require("react"),C=E(require("tiny-warning"),1);function J(e,n){if(e!=null)if(typeof e=="function")e(n);else try{e.current=n}catch{throw new Error(`Cannot assign value "${n}" to ref "${e}"`)}}function h(...e){return e.every(n=>n==null)?null:n=>{e.forEach(t=>{J(t,n)})}}function Q(e){let n=(0,r.useRef)(e);return(0,r.useEffect)(()=>{n.current=e}),(0,r.useCallback)((...t)=>n.current?.(...t),[])}function D(e,n,t){let[u,a]=(0,r.useState)(n),c=(0,r.useRef)(typeof e<"u"),f=c.current?e:u,g=Q(t),x=(0,r.useCallback)(s=>{let d=typeof s=="function"?s(f):s;c.current||a(d),g?.(d)},[g,f]);return(0,r.useEffect)(()=>{(0,C.default)(!(c.current&&e==null),"useCollapse is changing from controlled to uncontrolled. useCollapse should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled collapse for the lifetime of the component. Check the `isExpanded` prop."),(0,C.default)(!(!c.current&&e!=null),"useCollapse is changing from uncontrolled to controlled. useCollapse should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled collapse for the lifetime of the component. Check the `isExpanded` prop.")},[e]),[f,x]}function X({isExpanded:e,defaultExpanded:n=!1,onExpandedChange:t,easing:u="cubic-bezier(0.4, 0, 0.2, 1)",duration:a="auto",collapsedHeight:c=0,onTransitionStateChange:f=()=>{},id:g}={}){let x=l.useId(),[s,R]=D(e,n,t),d=l.useRef(s),[I,A]=l.useState(!1),[K,L]=l.useState(null),w=l.useRef(null),S={easing:u,duration:a,collapsedHeight:c,getDisclosureElement:()=>w.current,onExpandedChange(o){R(o),t?.(o)},onTransitionStateChange(o){switch(o){case"collapseEnd":case"expandEnd":A(!1);break}f?.(o)}},[p]=l.useState(()=>new H.Collapse(S));p.setOptions(S),l.useLayoutEffect(()=>{s!==d.current&&(d.current=s,A(!0),s?p.open():p.close())},[p,s]);let v=g??`collapsed-disclosure-${x}`;return{isExpanded:s,setExpanded:R,getToggleProps(o){let{disabled:i,refKey:T,...y}={refKey:"ref",disabled:!1,...o},k=K?K.tagName==="BUTTON":void 0,M=o?.[T||"ref"],b={"aria-controls":v,"aria-expanded":s,onClick(B){i||(o?.onClick?.(B),R(U=>!U))},[T||"ref"]:h(M,L)},O={type:"button",disabled:i?!0:void 0},V={"aria-disabled":i?!0:void 0,role:"button",tabIndex:i?-1:0};return k===!1?{...b,...V,...y}:k===!0?{...b,...O,...y}:{...b,...O,...V,...y}},getCollapseProps(o){let{refKey:i,style:T}={refKey:"ref",style:{},...o},y=o?.[i||"ref"];return{id:v,...o,style:{boxSizing:"border-box",...!I&&!s?p.getCollapsedStyles():{},...T},[i||"ref"]:h(w,y)}}}}0&&(module.exports={useCollapse});