react-uni-popper
Version:
A universal, headless React popper component powered by Floating UI. Build tooltips, dropdowns, selects, comboboxes, and more — all with one flexible component.
2 lines (1 loc) • 21.1 kB
JavaScript
import*as t from"react";import e,{useLayoutEffect as n,useEffect as o,useRef as r}from"react";import*as i from"react-dom";import{createPortal as l}from"react-dom";var c=function(){return c=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},c.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;const s=Math.min,a=Math.max,f=Math.round,u=Math.floor,d=t=>({x:t,y:t}),p={left:"right",right:"left",bottom:"top",top:"bottom"},m={start:"end",end:"start"};function h(t,e,n){return a(t,s(e,n))}function g(t,e){return"function"==typeof t?t(e):t}function y(t){return t.split("-")[0]}function w(t){return t.split("-")[1]}function x(t){return"x"===t?"y":"x"}function v(t){return"y"===t?"height":"width"}function b(t){return["top","bottom"].includes(y(t))?"y":"x"}function R(t){return x(b(t))}function O(t){return t.replace(/start|end/g,(t=>m[t]))}function S(t){return t.replace(/left|right|bottom|top/g,(t=>p[t]))}function E(t){return"number"!=typeof t?function(t){return{top:0,right:0,bottom:0,left:0,...t}}(t):{top:t,right:t,bottom:t,left:t}}function T(t){const{x:e,y:n,width:o,height:r}=t;return{width:o,height:r,top:n,left:e,right:e+o,bottom:n+r,x:e,y:n}}function L(t,e,n){let{reference:o,floating:r}=t;const i=b(e),l=R(e),c=v(l),s=y(e),a="y"===i,f=o.x+o.width/2-r.width/2,u=o.y+o.height/2-r.height/2,d=o[c]/2-r[c]/2;let p;switch(s){case"top":p={x:f,y:o.y-r.height};break;case"bottom":p={x:f,y:o.y+o.height};break;case"right":p={x:o.x+o.width,y:u};break;case"left":p={x:o.x-r.width,y:u};break;default:p={x:o.x,y:o.y}}switch(w(e)){case"start":p[l]-=d*(n&&a?-1:1);break;case"end":p[l]+=d*(n&&a?-1:1)}return p}async function P(t,e){var n;void 0===e&&(e={});const{x:o,y:r,platform:i,rects:l,elements:c,strategy:s}=t,{boundary:a="clippingAncestors",rootBoundary:f="viewport",elementContext:u="floating",altBoundary:d=!1,padding:p=0}=g(e,t),m=E(p),h=c[d?"floating"===u?"reference":"floating":u],y=T(await i.getClippingRect({element:null==(n=await(null==i.isElement?void 0:i.isElement(h)))||n?h:h.contextElement||await(null==i.getDocumentElement?void 0:i.getDocumentElement(c.floating)),boundary:a,rootBoundary:f,strategy:s})),w="floating"===u?{x:o,y:r,width:l.floating.width,height:l.floating.height}:l.reference,x=await(null==i.getOffsetParent?void 0:i.getOffsetParent(c.floating)),v=await(null==i.isElement?void 0:i.isElement(x))&&await(null==i.getScale?void 0:i.getScale(x))||{x:1,y:1},b=T(i.convertOffsetParentRelativeRectToViewportRelativeRect?await i.convertOffsetParentRelativeRectToViewportRelativeRect({elements:c,rect:w,offsetParent:x,strategy:s}):w);return{top:(y.top-b.top+m.top)/v.y,bottom:(b.bottom-y.bottom+m.bottom)/v.y,left:(y.left-b.left+m.left)/v.x,right:(b.right-y.right+m.right)/v.x}}function A(){return"undefined"!=typeof window}function C(t){return F(t)?(t.nodeName||"").toLowerCase():"#document"}function D(t){var e;return(null==t||null==(e=t.ownerDocument)?void 0:e.defaultView)||window}function k(t){var e;return null==(e=(F(t)?t.ownerDocument:t.document)||window.document)?void 0:e.documentElement}function F(t){return!!A()&&(t instanceof Node||t instanceof D(t).Node)}function M(t){return!!A()&&(t instanceof Element||t instanceof D(t).Element)}function j(t){return!!A()&&(t instanceof HTMLElement||t instanceof D(t).HTMLElement)}function z(t){return!(!A()||"undefined"==typeof ShadowRoot)&&(t instanceof ShadowRoot||t instanceof D(t).ShadowRoot)}function B(t){const{overflow:e,overflowX:n,overflowY:o,display:r}=$(t);return/auto|scroll|overlay|hidden|clip/.test(e+o+n)&&!["inline","contents"].includes(r)}function H(t){return["table","td","th"].includes(C(t))}function W(t){return[":popover-open",":modal"].some((e=>{try{return t.matches(e)}catch(t){return!1}}))}function V(t){const e=I(),n=M(t)?$(t):t;return["transform","translate","scale","rotate","perspective"].some((t=>!!n[t]&&"none"!==n[t]))||!!n.containerType&&"normal"!==n.containerType||!e&&!!n.backdropFilter&&"none"!==n.backdropFilter||!e&&!!n.filter&&"none"!==n.filter||["transform","translate","scale","rotate","perspective","filter"].some((t=>(n.willChange||"").includes(t)))||["paint","layout","strict","content"].some((t=>(n.contain||"").includes(t)))}function I(){return!("undefined"==typeof CSS||!CSS.supports)&&CSS.supports("-webkit-backdrop-filter","none")}function N(t){return["html","body","#document"].includes(C(t))}function $(t){return D(t).getComputedStyle(t)}function q(t){return M(t)?{scrollLeft:t.scrollLeft,scrollTop:t.scrollTop}:{scrollLeft:t.scrollX,scrollTop:t.scrollY}}function _(t){if("html"===C(t))return t;const e=t.assignedSlot||t.parentNode||z(t)&&t.host||k(t);return z(e)?e.host:e}function X(t){const e=_(t);return N(e)?t.ownerDocument?t.ownerDocument.body:t.body:j(e)&&B(e)?e:X(e)}function Y(t,e,n){var o;void 0===e&&(e=[]),void 0===n&&(n=!0);const r=X(t),i=r===(null==(o=t.ownerDocument)?void 0:o.body),l=D(r);if(i){const t=G(l);return e.concat(l,l.visualViewport||[],B(r)?r:[],t&&n?Y(t):[])}return e.concat(r,Y(r,[],n))}function G(t){return t.parent&&Object.getPrototypeOf(t.parent)?t.frameElement:null}function J(t){const e=$(t);let n=parseFloat(e.width)||0,o=parseFloat(e.height)||0;const r=j(t),i=r?t.offsetWidth:n,l=r?t.offsetHeight:o,c=f(n)!==i||f(o)!==l;return c&&(n=i,o=l),{width:n,height:o,$:c}}function K(t){return M(t)?t:t.contextElement}function Q(t){const e=K(t);if(!j(e))return d(1);const n=e.getBoundingClientRect(),{width:o,height:r,$:i}=J(e);let l=(i?f(n.width):n.width)/o,c=(i?f(n.height):n.height)/r;return l&&Number.isFinite(l)||(l=1),c&&Number.isFinite(c)||(c=1),{x:l,y:c}}const U=d(0);function Z(t){const e=D(t);return I()&&e.visualViewport?{x:e.visualViewport.offsetLeft,y:e.visualViewport.offsetTop}:U}function tt(t,e,n,o){void 0===e&&(e=!1),void 0===n&&(n=!1);const r=t.getBoundingClientRect(),i=K(t);let l=d(1);e&&(o?M(o)&&(l=Q(o)):l=Q(t));const c=function(t,e,n){return void 0===e&&(e=!1),!(!n||e&&n!==D(t))&&e}(i,n,o)?Z(i):d(0);let s=(r.left+c.x)/l.x,a=(r.top+c.y)/l.y,f=r.width/l.x,u=r.height/l.y;if(i){const t=D(i),e=o&&M(o)?D(o):o;let n=t,r=G(n);for(;r&&o&&e!==n;){const t=Q(r),e=r.getBoundingClientRect(),o=$(r),i=e.left+(r.clientLeft+parseFloat(o.paddingLeft))*t.x,l=e.top+(r.clientTop+parseFloat(o.paddingTop))*t.y;s*=t.x,a*=t.y,f*=t.x,u*=t.y,s+=i,a+=l,n=D(r),r=G(n)}}return T({width:f,height:u,x:s,y:a})}function et(t,e){const n=q(t).scrollLeft;return e?e.left+n:tt(k(t)).left+n}function nt(t,e,n){void 0===n&&(n=!1);const o=t.getBoundingClientRect();return{x:o.left+e.scrollLeft-(n?0:et(t,o)),y:o.top+e.scrollTop}}function ot(t,e,n){let o;if("viewport"===e)o=function(t,e){const n=D(t),o=k(t),r=n.visualViewport;let i=o.clientWidth,l=o.clientHeight,c=0,s=0;if(r){i=r.width,l=r.height;const t=I();(!t||t&&"fixed"===e)&&(c=r.offsetLeft,s=r.offsetTop)}return{width:i,height:l,x:c,y:s}}(t,n);else if("document"===e)o=function(t){const e=k(t),n=q(t),o=t.ownerDocument.body,r=a(e.scrollWidth,e.clientWidth,o.scrollWidth,o.clientWidth),i=a(e.scrollHeight,e.clientHeight,o.scrollHeight,o.clientHeight);let l=-n.scrollLeft+et(t);const c=-n.scrollTop;return"rtl"===$(o).direction&&(l+=a(e.clientWidth,o.clientWidth)-r),{width:r,height:i,x:l,y:c}}(k(t));else if(M(e))o=function(t,e){const n=tt(t,!0,"fixed"===e),o=n.top+t.clientTop,r=n.left+t.clientLeft,i=j(t)?Q(t):d(1);return{width:t.clientWidth*i.x,height:t.clientHeight*i.y,x:r*i.x,y:o*i.y}}(e,n);else{const n=Z(t);o={x:e.x-n.x,y:e.y-n.y,width:e.width,height:e.height}}return T(o)}function rt(t,e){const n=_(t);return!(n===e||!M(n)||N(n))&&("fixed"===$(n).position||rt(n,e))}function it(t,e,n){const o=j(e),r=k(e),i="fixed"===n,l=tt(t,!0,i,e);let c={scrollLeft:0,scrollTop:0};const s=d(0);if(o||!o&&!i)if(("body"!==C(e)||B(r))&&(c=q(e)),o){const t=tt(e,!0,i,e);s.x=t.x+e.clientLeft,s.y=t.y+e.clientTop}else r&&(s.x=et(r));const a=!r||o||i?d(0):nt(r,c);return{x:l.left+c.scrollLeft-s.x-a.x,y:l.top+c.scrollTop-s.y-a.y,width:l.width,height:l.height}}function lt(t){return"static"===$(t).position}function ct(t,e){if(!j(t)||"fixed"===$(t).position)return null;if(e)return e(t);let n=t.offsetParent;return k(t)===n&&(n=n.ownerDocument.body),n}function st(t,e){const n=D(t);if(W(t))return n;if(!j(t)){let e=_(t);for(;e&&!N(e);){if(M(e)&&!lt(e))return e;e=_(e)}return n}let o=ct(t,e);for(;o&&H(o)&<(o);)o=ct(o,e);return o&&N(o)&<(o)&&!V(o)?n:o||function(t){let e=_(t);for(;j(e)&&!N(e);){if(V(e))return e;if(W(e))return null;e=_(e)}return null}(t)||n}const at={convertOffsetParentRelativeRectToViewportRelativeRect:function(t){let{elements:e,rect:n,offsetParent:o,strategy:r}=t;const i="fixed"===r,l=k(o),c=!!e&&W(e.floating);if(o===l||c&&i)return n;let s={scrollLeft:0,scrollTop:0},a=d(1);const f=d(0),u=j(o);if((u||!u&&!i)&&(("body"!==C(o)||B(l))&&(s=q(o)),j(o))){const t=tt(o);a=Q(o),f.x=t.x+o.clientLeft,f.y=t.y+o.clientTop}const p=!l||u||i?d(0):nt(l,s,!0);return{width:n.width*a.x,height:n.height*a.y,x:n.x*a.x-s.scrollLeft*a.x+f.x+p.x,y:n.y*a.y-s.scrollTop*a.y+f.y+p.y}},getDocumentElement:k,getClippingRect:function(t){let{element:e,boundary:n,rootBoundary:o,strategy:r}=t;const i=[..."clippingAncestors"===n?W(e)?[]:function(t,e){const n=e.get(t);if(n)return n;let o=Y(t,[],!1).filter((t=>M(t)&&"body"!==C(t))),r=null;const i="fixed"===$(t).position;let l=i?_(t):t;for(;M(l)&&!N(l);){const e=$(l),n=V(l);n||"fixed"!==e.position||(r=null),(i?!n&&!r:!n&&"static"===e.position&&r&&["absolute","fixed"].includes(r.position)||B(l)&&!n&&rt(t,l))?o=o.filter((t=>t!==l)):r=e,l=_(l)}return e.set(t,o),o}(e,this._c):[].concat(n),o],l=i[0],c=i.reduce(((t,n)=>{const o=ot(e,n,r);return t.top=a(o.top,t.top),t.right=s(o.right,t.right),t.bottom=s(o.bottom,t.bottom),t.left=a(o.left,t.left),t}),ot(e,l,r));return{width:c.right-c.left,height:c.bottom-c.top,x:c.left,y:c.top}},getOffsetParent:st,getElementRects:async function(t){const e=this.getOffsetParent||st,n=this.getDimensions,o=await n(t.floating);return{reference:it(t.reference,await e(t.floating),t.strategy),floating:{x:0,y:0,width:o.width,height:o.height}}},getClientRects:function(t){return Array.from(t.getClientRects())},getDimensions:function(t){const{width:e,height:n}=J(t);return{width:e,height:n}},getScale:Q,isElement:M,isRTL:function(t){return"rtl"===$(t).direction}};function ft(t,e){return t.x===e.x&&t.y===e.y&&t.width===e.width&&t.height===e.height}function ut(t,e,n,o){void 0===o&&(o={});const{ancestorScroll:r=!0,ancestorResize:i=!0,elementResize:l="function"==typeof ResizeObserver,layoutShift:c="function"==typeof IntersectionObserver,animationFrame:f=!1}=o,d=K(t),p=r||i?[...d?Y(d):[],...Y(e)]:[];p.forEach((t=>{r&&t.addEventListener("scroll",n,{passive:!0}),i&&t.addEventListener("resize",n)}));const m=d&&c?function(t,e){let n,o=null;const r=k(t);function i(){var t;clearTimeout(n),null==(t=o)||t.disconnect(),o=null}return function l(c,f){void 0===c&&(c=!1),void 0===f&&(f=1),i();const d=t.getBoundingClientRect(),{left:p,top:m,width:h,height:g}=d;if(c||e(),!h||!g)return;const y={rootMargin:-u(m)+"px "+-u(r.clientWidth-(p+h))+"px "+-u(r.clientHeight-(m+g))+"px "+-u(p)+"px",threshold:a(0,s(1,f))||1};let w=!0;function x(e){const o=e[0].intersectionRatio;if(o!==f){if(!w)return l();o?l(!1,o):n=setTimeout((()=>{l(!1,1e-7)}),1e3)}1!==o||ft(d,t.getBoundingClientRect())||l(),w=!1}try{o=new IntersectionObserver(x,{...y,root:r.ownerDocument})}catch(t){o=new IntersectionObserver(x,y)}o.observe(t)}(!0),i}(d,n):null;let h,g=-1,y=null;l&&(y=new ResizeObserver((t=>{let[o]=t;o&&o.target===d&&y&&(y.unobserve(e),cancelAnimationFrame(g),g=requestAnimationFrame((()=>{var t;null==(t=y)||t.observe(e)}))),n()})),d&&!f&&y.observe(d),y.observe(e));let w=f?tt(t):null;return f&&function e(){const o=tt(t);w&&!ft(w,o)&&n();w=o,h=requestAnimationFrame(e)}(),n(),()=>{var t;p.forEach((t=>{r&&t.removeEventListener("scroll",n),i&&t.removeEventListener("resize",n)})),null==m||m(),null==(t=y)||t.disconnect(),y=null,f&&cancelAnimationFrame(h)}}const dt=function(t){return void 0===t&&(t=0),{name:"offset",options:t,async fn(e){var n,o;const{x:r,y:i,placement:l,middlewareData:c}=e,s=await async function(t,e){const{placement:n,platform:o,elements:r}=t,i=await(null==o.isRTL?void 0:o.isRTL(r.floating)),l=y(n),c=w(n),s="y"===b(n),a=["left","top"].includes(l)?-1:1,f=i&&s?-1:1,u=g(e,t);let{mainAxis:d,crossAxis:p,alignmentAxis:m}="number"==typeof u?{mainAxis:u,crossAxis:0,alignmentAxis:null}:{mainAxis:u.mainAxis||0,crossAxis:u.crossAxis||0,alignmentAxis:u.alignmentAxis};return c&&"number"==typeof m&&(p="end"===c?-1*m:m),s?{x:p*f,y:d*a}:{x:d*a,y:p*f}}(e,t);return l===(null==(n=c.offset)?void 0:n.placement)&&null!=(o=c.arrow)&&o.alignmentOffset?{}:{x:r+s.x,y:i+s.y,data:{...s,placement:l}}}}},pt=function(t){return void 0===t&&(t={}),{name:"shift",options:t,async fn(e){const{x:n,y:o,placement:r}=e,{mainAxis:i=!0,crossAxis:l=!1,limiter:c={fn:t=>{let{x:e,y:n}=t;return{x:e,y:n}}},...s}=g(t,e),a={x:n,y:o},f=await P(e,s),u=b(y(r)),d=x(u);let p=a[d],m=a[u];if(i){const t="y"===d?"bottom":"right";p=h(p+f["y"===d?"top":"left"],p,p-f[t])}if(l){const t="y"===u?"bottom":"right";m=h(m+f["y"===u?"top":"left"],m,m-f[t])}const w=c.fn({...e,[d]:p,[u]:m});return{...w,data:{x:w.x-n,y:w.y-o,enabled:{[d]:i,[u]:l}}}}}},mt=function(t){return void 0===t&&(t={}),{name:"flip",options:t,async fn(e){var n,o;const{placement:r,middlewareData:i,rects:l,initialPlacement:c,platform:s,elements:a}=e,{mainAxis:f=!0,crossAxis:u=!0,fallbackPlacements:d,fallbackStrategy:p="bestFit",fallbackAxisSideDirection:m="none",flipAlignment:h=!0,...x}=g(t,e);if(null!=(n=i.arrow)&&n.alignmentOffset)return{};const E=y(r),T=b(c),L=y(c)===c,A=await(null==s.isRTL?void 0:s.isRTL(a.floating)),C=d||(L||!h?[S(c)]:function(t){const e=S(t);return[O(t),e,O(e)]}(c)),D="none"!==m;!d&&D&&C.push(...function(t,e,n,o){const r=w(t);let i=function(t,e,n){const o=["left","right"],r=["right","left"],i=["top","bottom"],l=["bottom","top"];switch(t){case"top":case"bottom":return n?e?r:o:e?o:r;case"left":case"right":return e?i:l;default:return[]}}(y(t),"start"===n,o);return r&&(i=i.map((t=>t+"-"+r)),e&&(i=i.concat(i.map(O)))),i}(c,h,m,A));const k=[c,...C],F=await P(e,x),M=[];let j=(null==(o=i.flip)?void 0:o.overflows)||[];if(f&&M.push(F[E]),u){const t=function(t,e,n){void 0===n&&(n=!1);const o=w(t),r=R(t),i=v(r);let l="x"===r?o===(n?"end":"start")?"right":"left":"start"===o?"bottom":"top";return e.reference[i]>e.floating[i]&&(l=S(l)),[l,S(l)]}(r,l,A);M.push(F[t[0]],F[t[1]])}if(j=[...j,{placement:r,overflows:M}],!M.every((t=>t<=0))){var z,B;const t=((null==(z=i.flip)?void 0:z.index)||0)+1,e=k[t];if(e)return{data:{index:t,overflows:j},reset:{placement:e}};let n=null==(B=j.filter((t=>t.overflows[0]<=0)).sort(((t,e)=>t.overflows[1]-e.overflows[1]))[0])?void 0:B.placement;if(!n)switch(p){case"bestFit":{var H;const t=null==(H=j.filter((t=>{if(D){const e=b(t.placement);return e===T||"y"===e}return!0})).map((t=>[t.placement,t.overflows.filter((t=>t>0)).reduce(((t,e)=>t+e),0)])).sort(((t,e)=>t[1]-e[1]))[0])?void 0:H[0];t&&(n=t);break}case"initialPlacement":n=c}if(r!==n)return{reset:{placement:n}}}return{}}}},ht=t=>({name:"arrow",options:t,async fn(e){const{x:n,y:o,placement:r,rects:i,platform:l,elements:c,middlewareData:a}=e,{element:f,padding:u=0}=g(t,e)||{};if(null==f)return{};const d=E(u),p={x:n,y:o},m=R(r),y=v(m),x=await l.getDimensions(f),b="y"===m,O=b?"top":"left",S=b?"bottom":"right",T=b?"clientHeight":"clientWidth",L=i.reference[y]+i.reference[m]-p[m]-i.floating[y],P=p[m]-i.reference[m],A=await(null==l.getOffsetParent?void 0:l.getOffsetParent(f));let C=A?A[T]:0;C&&await(null==l.isElement?void 0:l.isElement(A))||(C=c.floating[T]||i.floating[y]);const D=L/2-P/2,k=C/2-x[y]/2-1,F=s(d[O],k),M=s(d[S],k),j=F,z=C-x[y]-M,B=C/2-x[y]/2+D,H=h(j,B,z),W=!a.arrow&&null!=w(r)&&B!==H&&i.reference[y]/2-(B<j?F:M)-x[y]/2<0,V=W?B<j?B-j:B-z:0;return{[m]:p[m]+V,data:{[m]:H,centerOffset:B-H-V,...W&&{alignmentOffset:V}},reset:W}}}),gt=(t,e,n)=>{const o=new Map,r={platform:at,...n},i={...r.platform,_c:o};return(async(t,e,n)=>{const{placement:o="bottom",strategy:r="absolute",middleware:i=[],platform:l}=n,c=i.filter(Boolean),s=await(null==l.isRTL?void 0:l.isRTL(e));let a=await l.getElementRects({reference:t,floating:e,strategy:r}),{x:f,y:u}=L(a,o,s),d=o,p={},m=0;for(let n=0;n<c.length;n++){const{name:i,fn:h}=c[n],{x:g,y:y,data:w,reset:x}=await h({x:f,y:u,initialPlacement:o,placement:d,strategy:r,middlewareData:p,rects:a,platform:l,elements:{reference:t,floating:e}});f=null!=g?g:f,u=null!=y?y:u,p={...p,[i]:{...p[i],...w}},x&&m<=50&&(m++,"object"==typeof x&&(x.placement&&(d=x.placement),x.rects&&(a=!0===x.rects?await l.getElementRects({reference:t,floating:e,strategy:r}):x.rects),({x:f,y:u}=L(a,d,s))),n=-1)}return{x:f,y:u,placement:d,strategy:r,middlewareData:p}})(t,e,{...r,platform:i})};var yt="undefined"!=typeof document?n:o;function wt(t,e){if(t===e)return!0;if(typeof t!=typeof e)return!1;if("function"==typeof t&&t.toString()===e.toString())return!0;let n,o,r;if(t&&e&&"object"==typeof t){if(Array.isArray(t)){if(n=t.length,n!==e.length)return!1;for(o=n;0!=o--;)if(!wt(t[o],e[o]))return!1;return!0}if(r=Object.keys(t),n=r.length,n!==Object.keys(e).length)return!1;for(o=n;0!=o--;)if(!{}.hasOwnProperty.call(e,r[o]))return!1;for(o=n;0!=o--;){const n=r[o];if(("_owner"!==n||!t.$$typeof)&&!wt(t[n],e[n]))return!1}return!0}return t!=t&&e!=e}function xt(t){if("undefined"==typeof window)return 1;return(t.ownerDocument.defaultView||window).devicePixelRatio||1}function vt(t,e){const n=xt(t);return Math.round(e*n)/n}function bt(e){const n=t.useRef(e);return yt((()=>{n.current=e})),n}const Rt=t=>({name:"arrow",options:t,fn(e){const{element:n,padding:o}="function"==typeof t?t(e):t;return n&&(r=n,{}.hasOwnProperty.call(r,"current"))?null!=n.current?ht({element:n.current,padding:o}).fn(e):{}:n?ht({element:n,padding:o}).fn(e):{};var r}}),Ot=(t,e)=>({...dt(t),options:[t,e]}),St=(t,e)=>({...pt(t),options:[t,e]}),Et=(t,e)=>({...Rt(t),options:[t,e]});function Tt(t){var e=t.children,n=t.container;return l(e,n||document.body)}function Lt(o){var l,s,a=o.portalContainer,f=o.reference,u=o.children,d=o.placement,p=void 0===d?"bottom":d,m=o.offset,h=void 0===m?4:m,g=o.zIndex,y=o.arrow,w=o.arrowSize,x=function(t,e){var n={};for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(n[o]=t[o]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(o=Object.getOwnPropertySymbols(t);r<o.length;r++)e.indexOf(o[r])<0&&Object.prototype.propertyIsEnumerable.call(t,o[r])&&(n[o[r]]=t[o[r]])}return n}(o,["portalContainer","reference","children","placement","offset","zIndex","arrow","arrowSize"]),v=r(null),b=function(e){void 0===e&&(e={});const{placement:n="bottom",strategy:o="absolute",middleware:r=[],platform:l,elements:{reference:c,floating:s}={},transform:a=!0,whileElementsMounted:f,open:u}=e,[d,p]=t.useState({x:0,y:0,strategy:o,placement:n,middlewareData:{},isPositioned:!1}),[m,h]=t.useState(r);wt(m,r)||h(r);const[g,y]=t.useState(null),[w,x]=t.useState(null),v=t.useCallback((t=>{t!==S.current&&(S.current=t,y(t))}),[]),b=t.useCallback((t=>{t!==E.current&&(E.current=t,x(t))}),[]),R=c||g,O=s||w,S=t.useRef(null),E=t.useRef(null),T=t.useRef(d),L=null!=f,P=bt(f),A=bt(l),C=bt(u),D=t.useCallback((()=>{if(!S.current||!E.current)return;const t={placement:n,strategy:o,middleware:m};A.current&&(t.platform=A.current),gt(S.current,E.current,t).then((t=>{const e={...t,isPositioned:!1!==C.current};k.current&&!wt(T.current,e)&&(T.current=e,i.flushSync((()=>{p(e)})))}))}),[m,n,o,A,C]);yt((()=>{!1===u&&T.current.isPositioned&&(T.current.isPositioned=!1,p((t=>({...t,isPositioned:!1}))))}),[u]);const k=t.useRef(!1);yt((()=>(k.current=!0,()=>{k.current=!1})),[]),yt((()=>{if(R&&(S.current=R),O&&(E.current=O),R&&O){if(P.current)return P.current(R,O,D);D()}}),[R,O,D,P,L]);const F=t.useMemo((()=>({reference:S,floating:E,setReference:v,setFloating:b})),[v,b]),M=t.useMemo((()=>({reference:R,floating:O})),[R,O]),j=t.useMemo((()=>{const t={position:o,left:0,top:0};if(!M.floating)return t;const e=vt(M.floating,d.x),n=vt(M.floating,d.y);return a?{...t,transform:"translate("+e+"px, "+n+"px)",...xt(M.floating)>=1.5&&{willChange:"transform"}}:{position:o,left:e,top:n}}),[o,a,M.floating,d.x,d.y]);return t.useMemo((()=>({...d,update:D,refs:F,elements:M,floatingStyles:j})),[d,D,F,M,j])}({placement:p,strategy:"absolute",whileElementsMounted:ut,middleware:[{...mt(l),options:[l,s]},St(),y?Et({element:v}):void 0,Ot(y?h+Math.sqrt(2*Math.pow(w,2))/2:h)]}),R=b.floatingStyles,O=b.refs,S=b.middlewareData,E=b.placement;return n((function(){f&&O.setReference(f)}),[f,O]),e.createElement(Tt,{container:a},e.createElement("div",c({className:"headless-popper",ref:O.setFloating,style:c({zIndex:g,position:"absolute",top:0,left:0},R)},x),"function"==typeof u?u({placement:E,arrowRef:v,floatingStyles:R,arrowStyles:function(t,e,n){var o;void 0===e&&(e={});var r=t.split("-")[0];return(o={left:null!=e.x?"".concat(e.x,"px"):"",top:null!=e.y?"".concat(e.y,"px"):"",right:"",bottom:""})[{top:"bottom",right:"left",bottom:"top",left:"right"}[r]]="".concat(-n/2,"px"),o}(E,S.arrow,w)}):u))}export{Lt as default};