compute-scroll-into-view
Version:
The engine that powers scroll-into-view-if-needed
2 lines • 4.53 kB
JavaScript
import{a as e,i as t,n,o as r,r as i,t as a}from"./shared-td4zZn_K.js";const o=e=>typeof e==`object`&&!!e&&e.nodeType===1,s=(e,t,n,r)=>{let i=t-e,a=r-n;return n<e&&r>t||n>e&&r<t?0:n<=e&&a<=i||r>=t&&a>=i?n-e:r>t&&a<i||n<e&&a>i?r-t:0},c=(e,t,n,r,i)=>e===`start`?r-t:e===`end`?i-n:e===`nearest`?s(t,n,r,i):(r+i)/2-(t+n)/2,l=e=>`offsetWidth`in e,u=e=>{let{writingMode:t,direction:n}=e,r=n===`rtl`,i=/^(vertical|sideways|tb)/.test(t);return{vertical:i,x:i?!t.endsWith(`lr`):r,y:i&&(t===`sideways-lr`?!r:r)}},d=(e,t)=>t?e===`start`?`end`:e===`end`?`start`:e:e,f=e=>e===`start`||e===`center`||e===`end`,p=(e,t)=>{let[n]=(t.scrollSnapType||`none`).split(` `),[r,i=r]=(e.scrollSnapAlign||`none`).split(` `),a=u(t),o=(e,t)=>{let o=t?r:i;return(n===`both`||n===e||n===(t?`block`:`inline`))&&f(o)?d(o,a[e]):void 0};return{x:o(`x`,a.vertical),y:o(`y`,!a.vertical)}},m=(e,t,n)=>n?Math.min(0,Math.max(e,-t)):Math.max(0,Math.min(e,t)),h=(e,t,n)=>(e.endsWith(`%`)?parseFloat(e)/100*t:parseFloat(e)*n)||0,g=(e,t,n)=>r((r,i,a)=>h(e.getPropertyValue(`scroll-padding-${r}`),a===`x`?t.right-t.left:t.bottom-t.top,n[a])),_=e=>getComputedStyle(e.documentElement),v=e=>{let{overflowX:t,overflowY:n}=_(e);return t===`visible`&&n===`visible`},y=e=>e.scrollingElement||e.documentElement,b=(e,t)=>{let n=e.getBoundingClientRect(),i=l(e),a={x:i&&e.offsetWidth?n.width/e.offsetWidth:0,y:i&&e.offsetHeight?n.height/e.offsetHeight:0};return{scrollport:r((r,o,s)=>n[r]+(o?1:-1)*(i?o?s===`x`?e.clientLeft:e.clientTop:s===`x`?e.offsetWidth-e.clientLeft-e.clientWidth:e.offsetHeight-e.clientTop-e.clientHeight:parseInt(t.getPropertyValue(`border-${r}-width`),10))*a[s]),scale:a}},x=e=>!!e&&e!==`none`,S=(e,t)=>{let n=new Set(e.getPropertyValue(`will-change`).split(`,`).map(e=>e.trim()));return!t&&(e.position!==`static`||n.has(`position`))||[`transform`,`translate`,`rotate`,`scale`,`perspective`,`filter`,`backdrop-filter`].some(t=>x(e.getPropertyValue(t))||n.has(t))||/layout|paint|strict|content/.test(e.getPropertyValue(`contain`))||/auto|hidden/.test(e.getPropertyValue(`content-visibility`))},C=(s,l={})=>{if(typeof document>`u`)return[];let{block:f=`center`,inline:h=`nearest`,container:x=`all`,middleware:C=[]}=l,w={block:f,inline:h,container:x};if(!o(s))throw TypeError(`Invalid target`);let T=s.ownerDocument,E=T.defaultView;if(!E)return[];let D=E,O=y(T),k={x:D.scrollX,y:D.scrollY},A=getComputedStyle(s),j=u(A),M={x:d(j.vertical?f:h,j.x),y:d(j.vertical?h:f,j.y)},N=s.getBoundingClientRect(),P=r(e=>N[e]),F=r(e=>parseFloat(A.getPropertyValue(`scroll-margin-${e}`))||0),I={x:[],y:[]},L=e=>r((t,n,r)=>I[r].reduce((e,t)=>e+t,e[t])),R=(t,n,r,i=!0)=>{let a=t===O,o,c,l=()=>{let e=D.visualViewport;return a?{scrollport:{top:0,right:e?e.width:D.innerWidth,bottom:e?e.height:D.innerHeight,left:0},scale:{x:1,y:1}}:o=b(t,n)},u=()=>{if(!c){let{scrollport:e,scale:t}=l();c={scrollport:e,target:Object.assign({},P),scrollPadding:g(a?_(T):n,e,t),scrollMargin:Object.assign({},F)}}return c},d=i&&(a||e(t)&&!(t===T.body&&v(T))),f=!1;for(let e=0;e<r;e++){let r=C[e].fn({target:s,container:t,viewport:a,scrollable:d,options:w,get rects(){return u()},get viewportRects(){return a?u():R(O,n,e).rects()}});r.scrollable!==void 0&&(d=r.scrollable),r.stop&&(f=!0),r.rects&&(c=Object.assign({},u(),r.rects))}return{viewport:a,scrollable:d,stop:f,rects:u,scale:()=>(o||l()).scale}},z=[],B=!0,V=s.contains(O)?O:n(s),H=A.position;for(;o(V);){let e=V,o=getComputedStyle(e),s=H!==`absolute`&&H!==`fixed`||H===`absolute`&&e===O||S(o,H===`fixed`);s&&(H=o.position);let l=R(e,o,C.length,s),{viewport:d,scrollable:f}=l,{stop:h}=l;if(f){let n=l.rects(),r=i(n),a=L(t(n)),s=B?p(A,d?_(document):o):{};B=!1;let f=l.scale(),g=u(d?_(T):o),v=t=>{let[n,i]=t===`x`?[`left`,`right`]:[`top`,`bottom`],l=c(s[t]||M[t],r[n],r[i],a[n],a[i]),u=d?k[t]:t===`x`?e.scrollLeft:e.scrollTop,p=t===`x`?e.scrollWidth-e.clientWidth:e.scrollHeight-e.clientHeight,h=!d&&(t===`x`?o.overflowX:o.overflowY)===`clip`?u:f[t]===0?0:m(u+l/f[t],p,g[t]);return I[t].push(u-h),d?m(u+l,1/0,g[t]):h};z.push({el:e,top:v(`y`),left:v(`x`)}),x===`nearest`&&(h=!0)}if(h)break;if(d){let t=x===`nearest`?null:a(e),i=t&&t.ownerDocument.defaultView;if(!t||!i)break;let o=L(l.rects().target);I={x:[],y:[]};let s=getComputedStyle(t),{scrollport:c,scale:u}=b(t,s),d={x:c.left+(parseFloat(s.paddingLeft)||0)*u.x,y:c.top+(parseFloat(s.paddingTop)||0)*u.y};P=r((e,t,n)=>d[n]+o[e]*u[n]),T=t.ownerDocument,D=i,k={x:D.scrollX,y:D.scrollY},O=y(T),H=s.position,V=n(t)}else V=n(e)}return z};export{C as compute};
//# sourceMappingURL=index.js.map