@sandlada/mdc
Version:
@sandlada/mdc is an open source component library that follows the Material Design 3 design specifications.
2 lines • 5.23 kB
JavaScript
import{clamp as e,evaluate as t,getAlignment as n,getAlignmentAxis as r,getAlignmentSides as i,getAxisLength as a,getExpandedPlacements as o,getOppositeAxis as s,getOppositeAxisPlacements as c,getOppositePlacement as l,getPaddingObject as u,getSide as d,getSideAxis as f,rectToClientRect as p}from"../../utils/dist/floating-ui.utils.js";function m(e,t,i){let{reference:o,floating:s}=e,c=f(t),l=r(t),u=a(l),p=d(t),m=c===`y`,h=o.x+o.width/2-s.width/2,g=o.y+o.height/2-s.height/2,_=o[u]/2-s[u]/2,v;switch(p){case`top`:v={x:h,y:o.y-s.height};break;case`bottom`:v={x:h,y:o.y+o.height};break;case`right`:v={x:o.x+o.width,y:g};break;case`left`:v={x:o.x-s.width,y:g};break;default:v={x:o.x,y:o.y}}let y=n(t);return y&&(v[l]+=_*(y===`end`?1:-1)*(i&&m?-1:1)),v}async function h(e,n){n===void 0&&(n={});let{x:r,y:i,platform:a,rects:o,elements:s,strategy:c}=e,{boundary:l=`clippingAncestors`,rootBoundary:d=`viewport`,elementContext:f=`floating`,altBoundary:m=!1,padding:h=0}=t(n,e),g=u(h),_=s[m?f===`floating`?`reference`:`floating`:f],v=p(await a.getClippingRect({element:await(a.isElement==null?void 0:a.isElement(_))??!0?_:_.contextElement||await(a.getDocumentElement==null?void 0:a.getDocumentElement(s.floating)),boundary:l,rootBoundary:d,strategy:c})),y=f===`floating`?{x:r,y:i,width:o.floating.width,height:o.floating.height}:o.reference,b=await(a.getOffsetParent==null?void 0:a.getOffsetParent(s.floating)),x=await(a.isElement==null?void 0:a.isElement(b))&&await(a.getScale==null?void 0:a.getScale(b))||{x:1,y:1},S=p(a.convertOffsetParentRelativeRectToViewportRelativeRect?await a.convertOffsetParentRelativeRectToViewportRelativeRect({elements:s,rect:y,offsetParent:b,strategy:c}):y);return{top:(v.top-S.top+g.top)/x.y,bottom:(S.bottom-v.bottom+g.bottom)/x.y,left:(v.left-S.left+g.left)/x.x,right:(S.right-v.right+g.right)/x.x}}const g=async(e,t,n)=>{let{placement:r=`bottom`,strategy:i=`absolute`,middleware:a=[],platform:o}=n,s=o.detectOverflow?o:{...o,detectOverflow:h},c=await(o.isRTL==null?void 0:o.isRTL(t)),l=await o.getElementRects({reference:e,floating:t,strategy:i}),{x:u,y:d}=m(l,r,c),f=r,p=0,g={};for(let n=0;n<a.length;n++){let h=a[n];if(!h)continue;let{name:_,fn:v}=h,{x:y,y:b,data:x,reset:S}=await v({x:u,y:d,initialPlacement:r,placement:f,strategy:i,middlewareData:g,rects:l,platform:s,elements:{reference:e,floating:t}});u=y??u,d=b??d,g[_]={...g[_],...x},S&&p<50&&(p++,typeof S==`object`&&(S.placement&&(f=S.placement),S.rects&&(l=S.rects===!0?await o.getElementRects({reference:e,floating:t,strategy:i}):S.rects),{x:u,y:d}=m(l,f,c)),n=-1)}return{x:u,y:d,placement:f,strategy:i,middlewareData:g}},_=function(e){return e===void 0&&(e={}),{name:`flip`,options:e,async fn(n){var r;let{placement:a,middlewareData:s,rects:u,initialPlacement:p,platform:m,elements:h}=n,{mainAxis:g=!0,crossAxis:_=!0,fallbackPlacements:v,fallbackStrategy:y=`bestFit`,fallbackAxisSideDirection:b=`none`,flipAlignment:x=!0,...S}=t(e,n);if((r=s.arrow)!=null&&r.alignmentOffset)return{};let C=d(a),w=f(p),T=d(p)===p,E=await(m.isRTL==null?void 0:m.isRTL(h.floating)),D=v||(T||!x?[l(p)]:o(p)),O=b!==`none`;!v&&O&&D.push(...c(p,x,b,E));let k=[p,...D],A=await m.detectOverflow(n,S),j=[],M=s.flip?.overflows||[];if(g&&j.push(A[C]),_){let e=i(a,u,E);j.push(A[e[0]],A[e[1]])}if(M=[...M,{placement:a,overflows:j}],!j.every(e=>e<=0)){let e=(s.flip?.index||0)+1,t=k[e];if(t&&(_!==`alignment`||w===f(t)||M.every(e=>f(e.placement)!==w||e.overflows[0]>0)))return{data:{index:e,overflows:M},reset:{placement:t}};let n=M.filter(e=>e.overflows[0]<=0).sort((e,t)=>e.overflows[1]-t.overflows[1])[0]?.placement;if(!n)switch(y){case`bestFit`:{let e=M.filter(e=>{if(O){let t=f(e.placement);return t===w||t===`y`}return!0}).map(e=>[e.placement,e.overflows.filter(e=>e>0).reduce((e,t)=>e+t,0)]).sort((e,t)=>e[1]-t[1])[0]?.[0];e&&(n=e);break}case`initialPlacement`:n=p}if(a!==n)return{reset:{placement:n}}}return{}}}},v=new Set([`left`,`top`]);async function y(e,r){let{placement:i,platform:a,elements:o}=e,s=await(a.isRTL==null?void 0:a.isRTL(o.floating)),c=d(i),l=n(i),u=f(i)===`y`,p=v.has(c)?-1:1,m=s&&u?-1:1,h=t(r,e),{mainAxis:g,crossAxis:_,alignmentAxis:y}=typeof h==`number`?{mainAxis:h,crossAxis:0,alignmentAxis:null}:{mainAxis:h.mainAxis||0,crossAxis:h.crossAxis||0,alignmentAxis:h.alignmentAxis};return l&&typeof y==`number`&&(_=l===`end`?y*-1:y),u?{x:_*m,y:g*p}:{x:g*p,y:_*m}}const b=function(e){return e===void 0&&(e=0),{name:`offset`,options:e,async fn(t){var n;let{x:r,y:i,placement:a,middlewareData:o}=t,s=await y(t,e);return a===o.offset?.placement&&(n=o.arrow)!=null&&n.alignmentOffset?{}:{x:r+s.x,y:i+s.y,data:{...s,placement:a}}}}},x=function(n){return n===void 0&&(n={}),{name:`shift`,options:n,async fn(r){let{x:i,y:a,placement:o,platform:c}=r,{mainAxis:l=!0,crossAxis:u=!1,limiter:d={fn:e=>{let{x:t,y:n}=e;return{x:t,y:n}}},...p}=t(n,r),m={x:i,y:a},h=await c.detectOverflow(r,p),g=f(o),_=s(g),v=m[_],y=m[g],b=(t,n)=>e(n+h[t===`y`?`top`:`left`],n,n-h[t===`y`?`bottom`:`right`]);l&&(v=b(_,v)),u&&(y=b(g,y));let x=d.fn({...r,[_]:v,[g]:y});return{...x,data:{x:x.x-i,y:x.y-a,enabled:{[_]:l,[g]:u}}}}}};export{g as computePosition,h as detectOverflow,_ as flip,b as offset,p as rectToClientRect,x as shift};
//# sourceMappingURL=floating-ui.core.js.map