@anoki/fse-ui
Version:
FSE UI components library
3 lines (2 loc) • 9.18 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index.cjs803.js");function V(n,t,g){let{reference:c,floating:d}=n;const i=e.getSideAxis(t),s=e.getAlignmentAxis(t),r=e.getAxisLength(s),u=e.getSide(t),A=i==="y",f=c.x+c.width/2-d.width/2,a=c.y+c.height/2-d.height/2,m=c[r]/2-d[r]/2;let o;switch(u){case"top":o={x:f,y:c.y-d.height};break;case"bottom":o={x:f,y:c.y+c.height};break;case"right":o={x:c.x+c.width,y:a};break;case"left":o={x:c.x-d.width,y:a};break;default:o={x:c.x,y:c.y}}switch(e.getAlignment(t)){case"start":o[s]-=m*(g&&A?-1:1);break;case"end":o[s]+=m*(g&&A?-1:1);break}return o}async function z(n,t){var g;t===void 0&&(t={});const{x:c,y:d,platform:i,rects:s,elements:r,strategy:u}=n,{boundary:A="clippingAncestors",rootBoundary:f="viewport",elementContext:a="floating",altBoundary:m=!1,padding:o=0}=e.evaluate(t,n),l=e.getPaddingObject(o),p=r[m?a==="floating"?"reference":"floating":a],v=e.rectToClientRect(await i.getClippingRect({element:(g=await(i.isElement==null?void 0:i.isElement(p)))==null||g?p:p.contextElement||await(i.getDocumentElement==null?void 0:i.getDocumentElement(r.floating)),boundary:A,rootBoundary:f,strategy:u})),y=a==="floating"?{x:c,y:d,width:s.floating.width,height:s.floating.height}:s.reference,w=await(i.getOffsetParent==null?void 0:i.getOffsetParent(r.floating)),h=await(i.isElement==null?void 0:i.isElement(w))?await(i.getScale==null?void 0:i.getScale(w))||{x:1,y:1}:{x:1,y:1},b=e.rectToClientRect(i.convertOffsetParentRelativeRectToViewportRelativeRect?await i.convertOffsetParentRelativeRectToViewportRelativeRect({elements:r,rect:y,offsetParent:w,strategy:u}):y);return{top:(v.top-b.top+l.top)/h.y,bottom:(b.bottom-v.bottom+l.bottom)/h.y,left:(v.left-b.left+l.left)/h.x,right:(b.right-v.right+l.right)/h.x}}const $=50,X=async(n,t,g)=>{const{placement:c="bottom",strategy:d="absolute",middleware:i=[],platform:s}=g,r=s.detectOverflow?s:{...s,detectOverflow:z},u=await(s.isRTL==null?void 0:s.isRTL(t));let A=await s.getElementRects({reference:n,floating:t,strategy:d}),{x:f,y:a}=V(A,c,u),m=c,o=0;const l={};for(let x=0;x<i.length;x++){const p=i[x];if(!p)continue;const{name:v,fn:y}=p,{x:w,y:h,data:b,reset:O}=await y({x:f,y:a,initialPlacement:c,placement:m,strategy:d,middlewareData:l,rects:A,platform:r,elements:{reference:n,floating:t}});f=w??f,a=h??a,l[v]={...l[v],...b},O&&o<$&&(o++,typeof O=="object"&&(O.placement&&(m=O.placement),O.rects&&(A=O.rects===!0?await s.getElementRects({reference:n,floating:t,strategy:d}):O.rects),{x:f,y:a}=V(A,m,u)),x=-1)}return{x:f,y:a,placement:m,strategy:d,middlewareData:l}},U=n=>({name:"arrow",options:n,async fn(t){const{x:g,y:c,placement:d,rects:i,platform:s,elements:r,middlewareData:u}=t,{element:A,padding:f=0}=e.evaluate(n,t)||{};if(A==null)return{};const a=e.getPaddingObject(f),m={x:g,y:c},o=e.getAlignmentAxis(d),l=e.getAxisLength(o),x=await s.getDimensions(A),p=o==="y",v=p?"top":"left",y=p?"bottom":"right",w=p?"clientHeight":"clientWidth",h=i.reference[l]+i.reference[o]-m[o]-i.floating[l],b=m[o]-i.reference[o],O=await(s.getOffsetParent==null?void 0:s.getOffsetParent(A));let C=O?O[w]:0;(!C||!await(s.isElement==null?void 0:s.isElement(O)))&&(C=r.floating[w]||i.floating[l]);const M=h/2-b/2,k=C/2-x[l]/2-1,P=e.min(a[v],k),L=e.min(a[y],k),T=P,H=C-x[l]-L,S=C/2-x[l]/2+M,F=e.clamp(T,S,H),E=!u.arrow&&e.getAlignment(d)!=null&&S!==F&&i.reference[l]/2-(S<T?P:L)-x[l]/2<0,R=E?S<T?S-T:S-H:0;return{[o]:m[o]+R,data:{[o]:F,centerOffset:S-F-R,...E&&{alignmentOffset:R}},reset:E}}}),q=function(n){return n===void 0&&(n={}),{name:"flip",options:n,async fn(t){var g,c;const{placement:d,middlewareData:i,rects:s,initialPlacement:r,platform:u,elements:A}=t,{mainAxis:f=!0,crossAxis:a=!0,fallbackPlacements:m,fallbackStrategy:o="bestFit",fallbackAxisSideDirection:l="none",flipAlignment:x=!0,...p}=e.evaluate(n,t);if((g=i.arrow)!=null&&g.alignmentOffset)return{};const v=e.getSide(d),y=e.getSideAxis(r),w=e.getSide(r)===r,h=await(u.isRTL==null?void 0:u.isRTL(A.floating)),b=m||(w||!x?[e.getOppositePlacement(r)]:e.getExpandedPlacements(r)),O=l!=="none";!m&&O&&b.push(...e.getOppositeAxisPlacements(r,x,l,h));const C=[r,...b],M=await u.detectOverflow(t,p),k=[];let P=((c=i.flip)==null?void 0:c.overflows)||[];if(f&&k.push(M[v]),a){const S=e.getAlignmentSides(d,s,h);k.push(M[S[0]],M[S[1]])}if(P=[...P,{placement:d,overflows:k}],!k.every(S=>S<=0)){var L,T;const S=(((L=i.flip)==null?void 0:L.index)||0)+1,F=C[S];if(F&&(!(a==="alignment"?y!==e.getSideAxis(F):!1)||P.every(D=>e.getSideAxis(D.placement)===y?D.overflows[0]>0:!0)))return{data:{index:S,overflows:P},reset:{placement:F}};let E=(T=P.filter(R=>R.overflows[0]<=0).sort((R,D)=>R.overflows[1]-D.overflows[1])[0])==null?void 0:T.placement;if(!E)switch(o){case"bestFit":{var H;const R=(H=P.filter(D=>{if(O){const j=e.getSideAxis(D.placement);return j===y||j==="y"}return!0}).map(D=>[D.placement,D.overflows.filter(j=>j>0).reduce((j,Y)=>j+Y,0)]).sort((D,j)=>D[1]-j[1])[0])==null?void 0:H[0];R&&(E=R);break}case"initialPlacement":E=r;break}if(d!==E)return{reset:{placement:E}}}return{}}}};function W(n,t){return{top:n.top-t.height,right:n.right-t.width,bottom:n.bottom-t.height,left:n.left-t.width}}function _(n){return e.sides.some(t=>n[t]>=0)}const I=function(n){return n===void 0&&(n={}),{name:"hide",options:n,async fn(t){const{rects:g,platform:c}=t,{strategy:d="referenceHidden",...i}=e.evaluate(n,t);switch(d){case"referenceHidden":{const s=await c.detectOverflow(t,{...i,elementContext:"reference"}),r=W(s,g.reference);return{data:{referenceHiddenOffsets:r,referenceHidden:_(r)}}}case"escaped":{const s=await c.detectOverflow(t,{...i,altBoundary:!0}),r=W(s,g.floating);return{data:{escapedOffsets:r,escaped:_(r)}}}default:return{}}}}},B=new Set(["left","top"]);async function N(n,t){const{placement:g,platform:c,elements:d}=n,i=await(c.isRTL==null?void 0:c.isRTL(d.floating)),s=e.getSide(g),r=e.getAlignment(g),u=e.getSideAxis(g)==="y",A=B.has(s)?-1:1,f=i&&u?-1:1,a=e.evaluate(t,n);let{mainAxis:m,crossAxis:o,alignmentAxis:l}=typeof a=="number"?{mainAxis:a,crossAxis:0,alignmentAxis:null}:{mainAxis:a.mainAxis||0,crossAxis:a.crossAxis||0,alignmentAxis:a.alignmentAxis};return r&&typeof l=="number"&&(o=r==="end"?l*-1:l),u?{x:o*f,y:m*A}:{x:m*A,y:o*f}}const G=function(n){return n===void 0&&(n=0),{name:"offset",options:n,async fn(t){var g,c;const{x:d,y:i,placement:s,middlewareData:r}=t,u=await N(t,n);return s===((g=r.offset)==null?void 0:g.placement)&&(c=r.arrow)!=null&&c.alignmentOffset?{}:{x:d+u.x,y:i+u.y,data:{...u,placement:s}}}}},J=function(n){return n===void 0&&(n={}),{name:"shift",options:n,async fn(t){const{x:g,y:c,placement:d,platform:i}=t,{mainAxis:s=!0,crossAxis:r=!1,limiter:u={fn:v=>{let{x:y,y:w}=v;return{x:y,y:w}}},...A}=e.evaluate(n,t),f={x:g,y:c},a=await i.detectOverflow(t,A),m=e.getSideAxis(e.getSide(d)),o=e.getOppositeAxis(m);let l=f[o],x=f[m];if(s){const v=o==="y"?"top":"left",y=o==="y"?"bottom":"right",w=l+a[v],h=l-a[y];l=e.clamp(w,l,h)}if(r){const v=m==="y"?"top":"left",y=m==="y"?"bottom":"right",w=x+a[v],h=x-a[y];x=e.clamp(w,x,h)}const p=u.fn({...t,[o]:l,[m]:x});return{...p,data:{x:p.x-g,y:p.y-c,enabled:{[o]:s,[m]:r}}}}}},K=function(n){return n===void 0&&(n={}),{options:n,fn(t){const{x:g,y:c,placement:d,rects:i,middlewareData:s}=t,{offset:r=0,mainAxis:u=!0,crossAxis:A=!0}=e.evaluate(n,t),f={x:g,y:c},a=e.getSideAxis(d),m=e.getOppositeAxis(a);let o=f[m],l=f[a];const x=e.evaluate(r,t),p=typeof x=="number"?{mainAxis:x,crossAxis:0}:{mainAxis:0,crossAxis:0,...x};if(u){const w=m==="y"?"height":"width",h=i.reference[m]-i.floating[w]+p.mainAxis,b=i.reference[m]+i.reference[w]-p.mainAxis;o<h?o=h:o>b&&(o=b)}if(A){var v,y;const w=m==="y"?"width":"height",h=B.has(e.getSide(d)),b=i.reference[a]-i.floating[w]+(h&&((v=s.offset)==null?void 0:v[a])||0)+(h?0:p.crossAxis),O=i.reference[a]+i.reference[w]+(h?0:((y=s.offset)==null?void 0:y[a])||0)-(h?p.crossAxis:0);l<b?l=b:l>O&&(l=O)}return{[m]:o,[a]:l}}}},Q=function(n){return n===void 0&&(n={}),{name:"size",options:n,async fn(t){var g,c;const{placement:d,rects:i,platform:s,elements:r}=t,{apply:u=()=>{},...A}=e.evaluate(n,t),f=await s.detectOverflow(t,A),a=e.getSide(d),m=e.getAlignment(d),o=e.getSideAxis(d)==="y",{width:l,height:x}=i.floating;let p,v;a==="top"||a==="bottom"?(p=a,v=m===(await(s.isRTL==null?void 0:s.isRTL(r.floating))?"start":"end")?"left":"right"):(v=a,p=m==="end"?"top":"bottom");const y=x-f.top-f.bottom,w=l-f.left-f.right,h=e.min(x-f[p],y),b=e.min(l-f[v],w),O=!t.middlewareData.shift;let C=h,M=b;if((g=t.middlewareData.shift)!=null&&g.enabled.x&&(M=w),(c=t.middlewareData.shift)!=null&&c.enabled.y&&(C=y),O&&!m){const P=e.max(f.left,0),L=e.max(f.right,0),T=e.max(f.top,0),H=e.max(f.bottom,0);o?M=l-2*(P!==0||L!==0?P+L:e.max(f.left,f.right)):C=x-2*(T!==0||H!==0?T+H:e.max(f.top,f.bottom))}await u({...t,availableWidth:M,availableHeight:C});const k=await s.getDimensions(r.floating);return l!==k.width||x!==k.height?{reset:{rects:!0}}:{}}}};exports.rectToClientRect=e.rectToClientRect;exports.arrow=U;exports.computePosition=X;exports.detectOverflow=z;exports.flip=q;exports.hide=I;exports.limitShift=K;exports.offset=G;exports.shift=J;exports.size=Q;
//# sourceMappingURL=index.cjs802.js.map