UNPKG

8.98 kBJavaScriptView Raw
1import{rectToClientRect as t,autoPlacement as e,shift as n,flip as o,size as i,hide as r,arrow as c,inline as l,limitShift as s,computePosition as f}from"@floating-ui/core";export{detectOverflow,offset}from"@floating-ui/core";const u=Math.min,a=Math.max,d=Math.round,h=Math.floor,p=t=>({x:t,y:t});function m(t){return w(t)?(t.nodeName||"").toLowerCase():"#document"}function g(t){var e;return(null==t||null==(e=t.ownerDocument)?void 0:e.defaultView)||window}function y(t){var e;return null==(e=(w(t)?t.ownerDocument:t.document)||window.document)?void 0:e.documentElement}function w(t){return t instanceof Node||t instanceof g(t).Node}function x(t){return t instanceof Element||t instanceof g(t).Element}function v(t){return t instanceof HTMLElement||t instanceof g(t).HTMLElement}function b(t){return"undefined"!=typeof ShadowRoot&&(t instanceof ShadowRoot||t instanceof g(t).ShadowRoot)}function L(t){const{overflow:e,overflowX:n,overflowY:o,display:i}=C(t);return/auto|scroll|overlay|hidden|clip/.test(e+o+n)&&!["inline","contents"].includes(i)}function T(t){return["table","td","th"].includes(m(t))}function R(t){const e=E(),n=C(t);return"none"!==n.transform||"none"!==n.perspective||!!n.containerType&&"normal"!==n.containerType||!e&&!!n.backdropFilter&&"none"!==n.backdropFilter||!e&&!!n.filter&&"none"!==n.filter||["transform","perspective","filter"].some((t=>(n.willChange||"").includes(t)))||["paint","layout","strict","content"].some((t=>(n.contain||"").includes(t)))}function E(){return!("undefined"==typeof CSS||!CSS.supports)&&CSS.supports("-webkit-backdrop-filter","none")}function S(t){return["html","body","#document"].includes(m(t))}function C(t){return g(t).getComputedStyle(t)}function F(t){return x(t)?{scrollLeft:t.scrollLeft,scrollTop:t.scrollTop}:{scrollLeft:t.pageXOffset,scrollTop:t.pageYOffset}}function O(t){if("html"===m(t))return t;const e=t.assignedSlot||t.parentNode||b(t)&&t.host||y(t);return b(e)?e.host:e}function D(t){const e=O(t);return S(e)?t.ownerDocument?t.ownerDocument.body:t.body:v(e)&&L(e)?e:D(e)}function H(t,e,n){var o;void 0===e&&(e=[]),void 0===n&&(n=!0);const i=D(t),r=i===(null==(o=t.ownerDocument)?void 0:o.body),c=g(i);return r?e.concat(c,c.visualViewport||[],L(i)?i:[],c.frameElement&&n?H(c.frameElement):[]):e.concat(i,H(i,[],n))}function W(t){const e=C(t);let n=parseFloat(e.width)||0,o=parseFloat(e.height)||0;const i=v(t),r=i?t.offsetWidth:n,c=i?t.offsetHeight:o,l=d(n)!==r||d(o)!==c;return l&&(n=r,o=c),{width:n,height:o,$:l}}function M(t){return x(t)?t:t.contextElement}function z(t){const e=M(t);if(!v(e))return p(1);const n=e.getBoundingClientRect(),{width:o,height:i,$:r}=W(e);let c=(r?d(n.width):n.width)/o,l=(r?d(n.height):n.height)/i;return c&&Number.isFinite(c)||(c=1),l&&Number.isFinite(l)||(l=1),{x:c,y:l}}const P=p(0);function V(t){const e=g(t);return E()&&e.visualViewport?{x:e.visualViewport.offsetLeft,y:e.visualViewport.offsetTop}:P}function A(e,n,o,i){void 0===n&&(n=!1),void 0===o&&(o=!1);const r=e.getBoundingClientRect(),c=M(e);let l=p(1);n&&(i?x(i)&&(l=z(i)):l=z(e));const s=function(t,e,n){return void 0===e&&(e=!1),!(!n||e&&n!==g(t))&&e}(c,o,i)?V(c):p(0);let f=(r.left+s.x)/l.x,u=(r.top+s.y)/l.y,a=r.width/l.x,d=r.height/l.y;if(c){const t=g(c),e=i&&x(i)?g(i):i;let n=t,o=n.frameElement;for(;o&&i&&e!==n;){const t=z(o),e=o.getBoundingClientRect(),i=C(o),r=e.left+(o.clientLeft+parseFloat(i.paddingLeft))*t.x,c=e.top+(o.clientTop+parseFloat(i.paddingTop))*t.y;f*=t.x,u*=t.y,a*=t.x,d*=t.y,f+=r,u+=c,n=g(o),o=n.frameElement}}return t({width:a,height:d,x:f,y:u})}const N=[":popover-open",":modal"];function B(t){return N.some((e=>{try{return t.matches(e)}catch(t){return!1}}))}function k(t){return A(y(t)).left+F(t).scrollLeft}function I(e,n,o){let i;if("viewport"===n)i=function(t,e){const n=g(t),o=y(t),i=n.visualViewport;let r=o.clientWidth,c=o.clientHeight,l=0,s=0;if(i){r=i.width,c=i.height;const t=E();(!t||t&&"fixed"===e)&&(l=i.offsetLeft,s=i.offsetTop)}return{width:r,height:c,x:l,y:s}}(e,o);else if("document"===n)i=function(t){const e=y(t),n=F(t),o=t.ownerDocument.body,i=a(e.scrollWidth,e.clientWidth,o.scrollWidth,o.clientWidth),r=a(e.scrollHeight,e.clientHeight,o.scrollHeight,o.clientHeight);let c=-n.scrollLeft+k(t);const l=-n.scrollTop;return"rtl"===C(o).direction&&(c+=a(e.clientWidth,o.clientWidth)-i),{width:i,height:r,x:c,y:l}}(y(e));else if(x(n))i=function(t,e){const n=A(t,!0,"fixed"===e),o=n.top+t.clientTop,i=n.left+t.clientLeft,r=v(t)?z(t):p(1);return{width:t.clientWidth*r.x,height:t.clientHeight*r.y,x:i*r.x,y:o*r.y}}(n,o);else{const t=V(e);i={...n,x:n.x-t.x,y:n.y-t.y}}return t(i)}function q(t,e){const n=O(t);return!(n===e||!x(n)||S(n))&&("fixed"===C(n).position||q(n,e))}function X(t,e,n){const o=v(e),i=y(e),r="fixed"===n,c=A(t,!0,r,e);let l={scrollLeft:0,scrollTop:0};const s=p(0);if(o||!o&&!r)if(("body"!==m(e)||L(i))&&(l=F(e)),o){const t=A(e,!0,r,e);s.x=t.x+e.clientLeft,s.y=t.y+e.clientTop}else i&&(s.x=k(i));return{x:c.left+l.scrollLeft-s.x,y:c.top+l.scrollTop-s.y,width:c.width,height:c.height}}function Y(t,e){return v(t)&&"fixed"!==C(t).position?e?e(t):t.offsetParent:null}function $(t,e){const n=g(t);if(!v(t)||B(t))return n;let o=Y(t,e);for(;o&&T(o)&&"static"===C(o).position;)o=Y(o,e);return o&&("html"===m(o)||"body"===m(o)&&"static"===C(o).position&&!R(o))?n:o||function(t){let e=O(t);for(;v(e)&&!S(e);){if(R(e))return e;e=O(e)}return null}(t)||n}const _={convertOffsetParentRelativeRectToViewportRelativeRect:function(t){let{elements:e,rect:n,offsetParent:o,strategy:i}=t;const r="fixed"===i,c=y(o),l=!!e&&B(e.floating);if(o===c||l&&r)return n;let s={scrollLeft:0,scrollTop:0},f=p(1);const u=p(0),a=v(o);if((a||!a&&!r)&&(("body"!==m(o)||L(c))&&(s=F(o)),v(o))){const t=A(o);f=z(o),u.x=t.x+o.clientLeft,u.y=t.y+o.clientTop}return{width:n.width*f.x,height:n.height*f.y,x:n.x*f.x-s.scrollLeft*f.x+u.x,y:n.y*f.y-s.scrollTop*f.y+u.y}},getDocumentElement:y,getClippingRect:function(t){let{element:e,boundary:n,rootBoundary:o,strategy:i}=t;const r=[..."clippingAncestors"===n?function(t,e){const n=e.get(t);if(n)return n;let o=H(t,[],!1).filter((t=>x(t)&&"body"!==m(t))),i=null;const r="fixed"===C(t).position;let c=r?O(t):t;for(;x(c)&&!S(c);){const e=C(c),n=R(c);n||"fixed"!==e.position||(i=null),(r?!n&&!i:!n&&"static"===e.position&&i&&["absolute","fixed"].includes(i.position)||L(c)&&!n&&q(t,c))?o=o.filter((t=>t!==c)):i=e,c=O(c)}return e.set(t,o),o}(e,this._c):[].concat(n),o],c=r[0],l=r.reduce(((t,n)=>{const o=I(e,n,i);return t.top=a(o.top,t.top),t.right=u(o.right,t.right),t.bottom=u(o.bottom,t.bottom),t.left=a(o.left,t.left),t}),I(e,c,i));return{width:l.right-l.left,height:l.bottom-l.top,x:l.left,y:l.top}},getOffsetParent:$,getElementRects:async function(t){const e=this.getOffsetParent||$,n=this.getDimensions;return{reference:X(t.reference,await e(t.floating),t.strategy),floating:{x:0,y:0,...await n(t.floating)}}},getClientRects:function(t){return Array.from(t.getClientRects())},getDimensions:function(t){const{width:e,height:n}=W(t);return{width:e,height:n}},getScale:z,isElement:x,isRTL:function(t){return"rtl"===C(t).direction}};function j(t,e,n,o){void 0===o&&(o={});const{ancestorScroll:i=!0,ancestorResize:r=!0,elementResize:c="function"==typeof ResizeObserver,layoutShift:l="function"==typeof IntersectionObserver,animationFrame:s=!1}=o,f=M(t),d=i||r?[...f?H(f):[],...H(e)]:[];d.forEach((t=>{i&&t.addEventListener("scroll",n,{passive:!0}),r&&t.addEventListener("resize",n)}));const p=f&&l?function(t,e){let n,o=null;const i=y(t);function r(){var t;clearTimeout(n),null==(t=o)||t.disconnect(),o=null}return function c(l,s){void 0===l&&(l=!1),void 0===s&&(s=1),r();const{left:f,top:d,width:p,height:m}=t.getBoundingClientRect();if(l||e(),!p||!m)return;const g={rootMargin:-h(d)+"px "+-h(i.clientWidth-(f+p))+"px "+-h(i.clientHeight-(d+m))+"px "+-h(f)+"px",threshold:a(0,u(1,s))||1};let y=!0;function w(t){const e=t[0].intersectionRatio;if(e!==s){if(!y)return c();e?c(!1,e):n=setTimeout((()=>{c(!1,1e-7)}),100)}y=!1}try{o=new IntersectionObserver(w,{...g,root:i.ownerDocument})}catch(t){o=new IntersectionObserver(w,g)}o.observe(t)}(!0),r}(f,n):null;let m,g=-1,w=null;c&&(w=new ResizeObserver((t=>{let[o]=t;o&&o.target===f&&w&&(w.unobserve(e),cancelAnimationFrame(g),g=requestAnimationFrame((()=>{var t;null==(t=w)||t.observe(e)}))),n()})),f&&!s&&w.observe(f),w.observe(e));let x=s?A(t):null;return s&&function e(){const o=A(t);!x||o.x===x.x&&o.y===x.y&&o.width===x.width&&o.height===x.height||n();x=o,m=requestAnimationFrame(e)}(),n(),()=>{var t;d.forEach((t=>{i&&t.removeEventListener("scroll",n),r&&t.removeEventListener("resize",n)})),null==p||p(),null==(t=w)||t.disconnect(),w=null,s&&cancelAnimationFrame(m)}}const G=e,J=n,K=o,Q=i,U=r,Z=c,tt=l,et=s,nt=(t,e,n)=>{const o=new Map,i={platform:_,...n},r={...i.platform,_c:o};return f(t,e,{...i,platform:r})};export{Z as arrow,G as autoPlacement,j as autoUpdate,nt as computePosition,K as flip,H as getOverflowAncestors,U as hide,tt as inline,et as limitShift,_ as platform,J as shift,Q as size};