1 |
- const t=["top","right","bottom","left"],e=["start","end"],n=t.reduce(((t,n)=>t.concat(n,n+"-"+e[0],n+"-"+e[1])),[]),o=Math.min,r=Math.max,i={left:"right",right:"left",bottom:"top",top:"bottom"},a={start:"end",end:"start"};function l(t,e,n){return r(t,o(e,n))}function s(t,e){return"function"==typeof t?t(e):t}function c(t){return t.split("-")[0]}function f(t){return t.split("-")[1]}function m(t){return"x"===t?"y":"x"}function u(t){return"y"===t?"height":"width"}function g(t){return["top","bottom"].includes(c(t))?"y":"x"}function d(t){return m(g(t))}function p(t,e,n){void 0===n&&(n=!1);const o=f(t),r=d(t),i=u(r);let a="x"===r?o===(n?"end":"start")?"right":"left":"start"===o?"bottom":"top";return e.reference[i]>e.floating[i]&&(a=y(a)),[a,y(a)]}function h(t){return t.replace(/start|end/g,(t=>a[t]))}function y(t){return t.replace(/left|right|bottom|top/g,(t=>i[t]))}function w(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 x(t){return{...t,top:t.y,left:t.x,right:t.x+t.width,bottom:t.y+t.height}}function v(t,e,n){let{reference:o,floating:r}=t;const i=g(e),a=d(e),l=u(a),s=c(e),m="y"===i,p=o.x+o.width/2-r.width/2,h=o.y+o.height/2-r.height/2,y=o[l]/2-r[l]/2;let w;switch(s){case"top":w={x:p,y:o.y-r.height};break;case"bottom":w={x:p,y:o.y+o.height};break;case"right":w={x:o.x+o.width,y:h};break;case"left":w={x:o.x-r.width,y:h};break;default:w={x:o.x,y:o.y}}switch(f(e)){case"start":w[a]-=y*(n&&m?-1:1);break;case"end":w[a]+=y*(n&&m?-1:1)}return w}const b=async(t,e,n)=>{const{placement:o="bottom",strategy:r="absolute",middleware:i=[],platform:a}=n,l=i.filter(Boolean),s=await(null==a.isRTL?void 0:a.isRTL(e));let c=await a.getElementRects({reference:t,floating:e,strategy:r}),{x:f,y:m}=v(c,o,s),u=o,g={},d=0;for(let n=0;n<l.length;n++){const{name:i,fn:p}=l[n],{x:h,y:y,data:w,reset:x}=await p({x:f,y:m,initialPlacement:o,placement:u,strategy:r,middlewareData:g,rects:c,platform:a,elements:{reference:t,floating:e}});f=null!=h?h:f,m=null!=y?y:m,g={...g,[i]:{...g[i],...w}},x&&d<=50&&(d++,"object"==typeof x&&(x.placement&&(u=x.placement),x.rects&&(c=!0===x.rects?await a.getElementRects({reference:t,floating:e,strategy:r}):x.rects),({x:f,y:m}=v(c,u,s))),n=-1)}return{x:f,y:m,placement:u,strategy:r,middlewareData:g}};async function A(t,e){var n;void 0===e&&(e={});const{x:o,y:r,platform:i,rects:a,elements:l,strategy:c}=t,{boundary:f="clippingAncestors",rootBoundary:m="viewport",elementContext:u="floating",altBoundary:g=!1,padding:d=0}=s(e,t),p=w(d),h=l[g?"floating"===u?"reference":"floating":u],y=x(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(l.floating)),boundary:f,rootBoundary:m,strategy:c})),v="floating"===u?{...a.floating,x:o,y:r}:a.reference,b=await(null==i.getOffsetParent?void 0:i.getOffsetParent(l.floating)),A=await(null==i.isElement?void 0:i.isElement(b))&&await(null==i.getScale?void 0:i.getScale(b))||{x:1,y:1},R=x(i.convertOffsetParentRelativeRectToViewportRelativeRect?await i.convertOffsetParentRelativeRectToViewportRelativeRect({elements:l,rect:v,offsetParent:b,strategy:c}):v);return{top:(y.top-R.top+p.top)/A.y,bottom:(R.bottom-y.bottom+p.bottom)/A.y,left:(y.left-R.left+p.left)/A.x,right:(R.right-y.right+p.right)/A.x}}const R=t=>({name:"arrow",options:t,async fn(e){const{x:n,y:r,placement:i,rects:a,platform:c,elements:m,middlewareData:g}=e,{element:p,padding:h=0}=s(t,e)||{};if(null==p)return{};const y=w(h),x={x:n,y:r},v=d(i),b=u(v),A=await c.getDimensions(p),R="y"===v,P=R?"top":"left",D=R?"bottom":"right",E=R?"clientHeight":"clientWidth",O=a.reference[b]+a.reference[v]-x[v]-a.floating[b],T=x[v]-a.reference[v],L=await(null==c.getOffsetParent?void 0:c.getOffsetParent(p));let k=L?L[E]:0;k&&await(null==c.isElement?void 0:c.isElement(L))||(k=m.floating[E]||a.floating[b]);const B=O/2-T/2,C=k/2-A[b]/2-1,H=o(y[P],C),S=o(y[D],C),F=H,M=k-A[b]-S,V=k/2-A[b]/2+B,W=l(F,V,M),j=!g.arrow&&null!=f(i)&&V!==W&&a.reference[b]/2-(V<F?H:S)-A[b]/2<0,z=j?V<F?V-F:V-M:0;return{[v]:x[v]+z,data:{[v]:W,centerOffset:V-W-z,...j&&{alignmentOffset:z}},reset:j}}});const P=function(t){return void 0===t&&(t={}),{name:"autoPlacement",options:t,async fn(e){var o,r,i;const{rects:a,middlewareData:l,placement:m,platform:u,elements:g}=e,{crossAxis:d=!1,alignment:y,allowedPlacements:w=n,autoAlignment:x=!0,...v}=s(t,e),b=void 0!==y||w===n?function(t,e,n){return(t?[...n.filter((e=>f(e)===t)),...n.filter((e=>f(e)!==t))]:n.filter((t=>c(t)===t))).filter((n=>!t||f(n)===t||!!e&&h(n)!==n))}(y||null,x,w):w,R=await A(e,v),P=(null==(o=l.autoPlacement)?void 0:o.index)||0,D=b[P];if(null==D)return{};const E=p(D,a,await(null==u.isRTL?void 0:u.isRTL(g.floating)));if(m!==D)return{reset:{placement:b[0]}};const O=[R[c(D)],R[E[0]],R[E[1]]],T=[...(null==(r=l.autoPlacement)?void 0:r.overflows)||[],{placement:D,overflows:O}],L=b[P+1];if(L)return{data:{index:P+1,overflows:T},reset:{placement:L}};const k=T.map((t=>{const e=f(t.placement);return[t.placement,e&&d?t.overflows.slice(0,2).reduce(((t,e)=>t+e),0):t.overflows[0],t.overflows]})).sort(((t,e)=>t[1]-e[1])),B=(null==(i=k.filter((t=>t[2].slice(0,f(t[0])?2:3).every((t=>t<=0))))[0])?void 0:i[0])||k[0][0];return B!==m?{data:{index:P+1,overflows:T},reset:{placement:B}}:{}}}},D=function(t){return void 0===t&&(t={}),{name:"flip",options:t,async fn(e){var n,o;const{placement:r,middlewareData:i,rects:a,initialPlacement:l,platform:m,elements:u}=e,{mainAxis:g=!0,crossAxis:d=!0,fallbackPlacements:w,fallbackStrategy:x="bestFit",fallbackAxisSideDirection:v="none",flipAlignment:b=!0,...R}=s(t,e);if(null!=(n=i.arrow)&&n.alignmentOffset)return{};const P=c(r),D=c(l)===l,E=await(null==m.isRTL?void 0:m.isRTL(u.floating)),O=w||(D||!b?[y(l)]:function(t){const e=y(t);return[h(t),e,h(e)]}(l));w||"none"===v||O.push(...function(t,e,n,o){const r=f(t);let i=function(t,e,n){const o=["left","right"],r=["right","left"],i=["top","bottom"],a=["bottom","top"];switch(t){case"top":case"bottom":return n?e?r:o:e?o:r;case"left":case"right":return e?i:a;default:return[]}}(c(t),"start"===n,o);return r&&(i=i.map((t=>t+"-"+r)),e&&(i=i.concat(i.map(h)))),i}(l,b,v,E));const T=[l,...O],L=await A(e,R),k=[];let B=(null==(o=i.flip)?void 0:o.overflows)||[];if(g&&k.push(L[P]),d){const t=p(r,a,E);k.push(L[t[0]],L[t[1]])}if(B=[...B,{placement:r,overflows:k}],!k.every((t=>t<=0))){var C,H;const t=((null==(C=i.flip)?void 0:C.index)||0)+1,e=T[t];if(e)return{data:{index:t,overflows:B},reset:{placement:e}};let n=null==(H=B.filter((t=>t.overflows[0]<=0)).sort(((t,e)=>t.overflows[1]-e.overflows[1]))[0])?void 0:H.placement;if(!n)switch(x){case"bestFit":{var S;const t=null==(S=B.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:S[0];t&&(n=t);break}case"initialPlacement":n=l}if(r!==n)return{reset:{placement:n}}}return{}}}};function E(t,e){return{top:t.top-e.height,right:t.right-e.width,bottom:t.bottom-e.height,left:t.left-e.width}}function O(e){return t.some((t=>e[t]>=0))}const T=function(t){return void 0===t&&(t={}),{name:"hide",options:t,async fn(e){const{rects:n}=e,{strategy:o="referenceHidden",...r}=s(t,e);switch(o){case"referenceHidden":{const t=E(await A(e,{...r,elementContext:"reference"}),n.reference);return{data:{referenceHiddenOffsets:t,referenceHidden:O(t)}}}case"escaped":{const t=E(await A(e,{...r,altBoundary:!0}),n.floating);return{data:{escapedOffsets:t,escaped:O(t)}}}default:return{}}}}};function L(t){const e=o(...t.map((t=>t.left))),n=o(...t.map((t=>t.top)));return{x:e,y:n,width:r(...t.map((t=>t.right)))-e,height:r(...t.map((t=>t.bottom)))-n}}const k=function(t){return void 0===t&&(t={}),{name:"inline",options:t,async fn(e){const{placement:n,elements:i,rects:a,platform:l,strategy:f}=e,{padding:m=2,x:u,y:d}=s(t,e),p=Array.from(await(null==l.getClientRects?void 0:l.getClientRects(i.reference))||[]),h=function(t){const e=t.slice().sort(((t,e)=>t.y-e.y)),n=[];let o=null;for(let t=0;t<e.length;t++){const r=e[t];!o||r.y-o.y>o.height/2?n.push([r]):n[n.length-1].push(r),o=r}return n.map((t=>x(L(t))))}(p),y=x(L(p)),v=w(m);const b=await l.getElementRects({reference:{getBoundingClientRect:function(){if(2===h.length&&h[0].left>h[1].right&&null!=u&&null!=d)return h.find((t=>u>t.left-v.left&&u<t.right+v.right&&d>t.top-v.top&&d<t.bottom+v.bottom))||y;if(h.length>=2){if("y"===g(n)){const t=h[0],e=h[h.length-1],o="top"===c(n),r=t.top,i=e.bottom,a=o?t.left:e.left,l=o?t.right:e.right;return{top:r,bottom:i,left:a,right:l,width:l-a,height:i-r,x:a,y:r}}const t="left"===c(n),e=r(...h.map((t=>t.right))),i=o(...h.map((t=>t.left))),a=h.filter((n=>t?n.left===i:n.right===e)),l=a[0].top,s=a[a.length-1].bottom;return{top:l,bottom:s,left:i,right:e,width:e-i,height:s-l,x:i,y:l}}return y}},floating:i.floating,strategy:f});return a.reference.x!==b.reference.x||a.reference.y!==b.reference.y||a.reference.width!==b.reference.width||a.reference.height!==b.reference.height?{reset:{rects:b}}:{}}}};const B=function(t){return void 0===t&&(t=0),{name:"offset",options:t,async fn(e){var n,o;const{x:r,y:i,placement:a,middlewareData:l}=e,m=await async function(t,e){const{placement:n,platform:o,elements:r}=t,i=await(null==o.isRTL?void 0:o.isRTL(r.floating)),a=c(n),l=f(n),m="y"===g(n),u=["left","top"].includes(a)?-1:1,d=i&&m?-1:1,p=s(e,t);let{mainAxis:h,crossAxis:y,alignmentAxis:w}="number"==typeof p?{mainAxis:p,crossAxis:0,alignmentAxis:null}:{mainAxis:0,crossAxis:0,alignmentAxis:null,...p};return l&&"number"==typeof w&&(y="end"===l?-1*w:w),m?{x:y*d,y:h*u}:{x:h*u,y:y*d}}(e,t);return a===(null==(n=l.offset)?void 0:n.placement)&&null!=(o=l.arrow)&&o.alignmentOffset?{}:{x:r+m.x,y:i+m.y,data:{...m,placement:a}}}}},C=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:a=!1,limiter:f={fn:t=>{let{x:e,y:n}=t;return{x:e,y:n}}},...u}=s(t,e),d={x:n,y:o},p=await A(e,u),h=g(c(r)),y=m(h);let w=d[y],x=d[h];if(i){const t="y"===y?"bottom":"right";w=l(w+p["y"===y?"top":"left"],w,w-p[t])}if(a){const t="y"===h?"bottom":"right";x=l(x+p["y"===h?"top":"left"],x,x-p[t])}const v=f.fn({...e,[y]:w,[h]:x});return{...v,data:{x:v.x-n,y:v.y-o}}}}},H=function(t){return void 0===t&&(t={}),{options:t,fn(e){const{x:n,y:o,placement:r,rects:i,middlewareData:a}=e,{offset:l=0,mainAxis:f=!0,crossAxis:u=!0}=s(t,e),d={x:n,y:o},p=g(r),h=m(p);let y=d[h],w=d[p];const x=s(l,e),v="number"==typeof x?{mainAxis:x,crossAxis:0}:{mainAxis:0,crossAxis:0,...x};if(f){const t="y"===h?"height":"width",e=i.reference[h]-i.floating[t]+v.mainAxis,n=i.reference[h]+i.reference[t]-v.mainAxis;y<e?y=e:y>n&&(y=n)}if(u){var b,A;const t="y"===h?"width":"height",e=["top","left"].includes(c(r)),n=i.reference[p]-i.floating[t]+(e&&(null==(b=a.offset)?void 0:b[p])||0)+(e?0:v.crossAxis),o=i.reference[p]+i.reference[t]+(e?0:(null==(A=a.offset)?void 0:A[p])||0)-(e?v.crossAxis:0);w<n?w=n:w>o&&(w=o)}return{[h]:y,[p]:w}}}},S=function(t){return void 0===t&&(t={}),{name:"size",options:t,async fn(e){const{placement:n,rects:i,platform:a,elements:l}=e,{apply:m=(()=>{}),...u}=s(t,e),d=await A(e,u),p=c(n),h=f(n),y="y"===g(n),{width:w,height:x}=i.floating;let v,b;"top"===p||"bottom"===p?(v=p,b=h===(await(null==a.isRTL?void 0:a.isRTL(l.floating))?"start":"end")?"left":"right"):(b=p,v="end"===h?"top":"bottom");const R=x-d[v],P=w-d[b],D=!e.middlewareData.shift;let E=R,O=P;if(y){const t=w-d.left-d.right;O=h||D?o(P,t):t}else{const t=x-d.top-d.bottom;E=h||D?o(R,t):t}if(D&&!h){const t=r(d.left,0),e=r(d.right,0),n=r(d.top,0),o=r(d.bottom,0);y?O=w-2*(0!==t||0!==e?t+e:r(d.left,d.right)):E=x-2*(0!==n||0!==o?n+o:r(d.top,d.bottom))}await m({...e,availableWidth:O,availableHeight:E});const T=await a.getDimensions(l.floating);return w!==T.width||x!==T.height?{reset:{rects:!0}}:{}}}};export{R as arrow,P as autoPlacement,b as computePosition,A as detectOverflow,D as flip,T as hide,k as inline,H as limitShift,B as offset,x as rectToClientRect,C as shift,S as size};
|