@stanfordspezi/spezi-web-design-system
Version:
Stanford Biodesign Digital Health Spezi Web Design System
2 lines (1 loc) • 2.48 kB
JavaScript
;const E=require("react"),P=require("./index-UhBwhhTY.js"),g=require("./index-CLdeiAAB.js");function R(n){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const t in n)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(e,t,r.get?r:{enumerable:!0,get:()=>n[t]})}}return e.default=n,Object.freeze(e)}const a=R(E);function y(n,e){return a.useReducer((t,r)=>e[t][r]??t,n)}var A=n=>{const{present:e,children:t}=n,r=v(e),o=typeof t=="function"?t({present:r.isPresent}):a.Children.only(t),u=P.useComposedRefs(r.ref,w(o));return typeof t=="function"||r.isPresent?a.cloneElement(o,{ref:u}):null};A.displayName="Presence";function v(n){const[e,t]=a.useState(),r=a.useRef({}),o=a.useRef(n),u=a.useRef("none"),p=n?"mounted":"unmounted",[N,c]=y(p,{mounted:{UNMOUNT:"unmounted",ANIMATION_OUT:"unmountSuspended"},unmountSuspended:{MOUNT:"mounted",ANIMATION_END:"unmounted"},unmounted:{MOUNT:"mounted"}});return a.useEffect(()=>{const i=l(r.current);u.current=N==="mounted"?i:"none"},[N]),g.useLayoutEffect2(()=>{const i=r.current,d=o.current;if(d!==n){const f=u.current,s=l(i);n?c("MOUNT"):s==="none"||(i==null?void 0:i.display)==="none"?c("UNMOUNT"):c(d&&f!==s?"ANIMATION_OUT":"UNMOUNT"),o.current=n}},[n,c]),g.useLayoutEffect2(()=>{if(e){let i;const d=e.ownerDocument.defaultView??window,m=s=>{const M=l(r.current).includes(s.animationName);if(s.target===e&&M&&(c("ANIMATION_END"),!o.current)){const T=e.style.animationFillMode;e.style.animationFillMode="forwards",i=d.setTimeout(()=>{e.style.animationFillMode==="forwards"&&(e.style.animationFillMode=T)})}},f=s=>{s.target===e&&(u.current=l(r.current))};return e.addEventListener("animationstart",f),e.addEventListener("animationcancel",m),e.addEventListener("animationend",m),()=>{d.clearTimeout(i),e.removeEventListener("animationstart",f),e.removeEventListener("animationcancel",m),e.removeEventListener("animationend",m)}}else c("ANIMATION_END")},[e,c]),{isPresent:["mounted","unmountSuspended"].includes(N),ref:a.useCallback(i=>{i&&(r.current=getComputedStyle(i)),t(i)},[])}}function l(n){return(n==null?void 0:n.animationName)||"none"}function w(n){var r,o;let e=(r=Object.getOwnPropertyDescriptor(n.props,"ref"))==null?void 0:r.get,t=e&&"isReactWarning"in e&&e.isReactWarning;return t?n.ref:(e=(o=Object.getOwnPropertyDescriptor(n,"ref"))==null?void 0:o.get,t=e&&"isReactWarning"in e&&e.isReactWarning,t?n.props.ref:n.props.ref||n.ref)}exports.Presence=A;