"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("react-dom"),t=require("../../hooks/use-disposables.js"),u=require("../../hooks/use-on-unmount.js"),n=require("../../hooks/use-owner.js"),o=require("../../hooks/use-server-handoff-complete.js"),l=require("../../hooks/use-sync-refs.js"),s=require("../../internal/portal-force-root.js"),a=require("../../utils/env.js"),i=require("../../utils/render.js");function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var f=d(e);let c=e.Fragment,p=i.forwardRefWithAs((function(d,p){let{ownerDocument:m=null,...P}=d,R=e.useRef(null),j=l.useSyncRefs(l.optionalRef((e=>{R.current=e})),p),b=n.useOwnerDocument(R.current),g=function(r){let t=s.usePortalRoot(),u=e.useContext(h),[n,o]=e.useState((()=>{var e;if(!t&&null!==u)return null!=(e=u.current)?e:null;if(a.env.isServer)return null;let n=null==r?void 0:r.getElementById("headlessui-portal-root");if(n)return n;if(null===r)return null;let o=r.createElement("div");return o.setAttribute("id","headlessui-portal-root"),r.body.appendChild(o)}));return e.useEffect((()=>{null!==n&&(null!=r&&r.body.contains(n)||null==r||r.body.appendChild(n))}),[n,r]),e.useEffect((()=>{t||null!==u&&o(u.current)}),[u,o,t]),n}(null!=m?m:b),q=e.useContext(v),y=t.useDisposables(),w=o.useServerHandoffComplete(),C=i.useRender();return u.useOnUnmount((()=>{var e;g&&g.childNodes.length<=0&&(null==(e=g.parentElement)||e.removeChild(g))})),g&&w?r.createPortal(f.default.createElement("div",{"data-headlessui-portal":"",ref:e=>{y.dispose(),q&&e&&y.add(q.register(e))}},C({ourProps:{ref:j},theirProps:P,slot:{},defaultTag:c,name:"Portal"})),g):null}));let m=e.Fragment,h=e.createContext(null);let v=e.createContext(null),P=i.forwardRefWithAs((function(e,r){let t=l.useSyncRefs(r),{enabled:u=!0,ownerDocument:n,...o}=e,s=i.useRender();return u?f.default.createElement(p,{...o,ownerDocument:n,ref:t}):s({ourProps:{ref:t},theirProps:o,slot:{},defaultTag:c,name:"Portal"})})),R=i.forwardRefWithAs((function(e,r){let{target:t,...u}=e,n={ref:l.useSyncRefs(r)},o=i.useRender();return f.default.createElement(h.Provider,{value:t},o({ourProps:n,theirProps:u,defaultTag:m,name:"Popover.Group"}))})),j=Object.assign(P,{Group:R});exports.Portal=j,exports.PortalGroup=R;
//# sourceMappingURL=portal.js.map
