UNPKG

@yrobot/auto-scroll

Version:

This is a tool which makes scroll-container auto scroll to the bottom easy.

2 lines (1 loc) 1.54 kB
var autoScroll=function(f){"use strict";const d=(a,l)=>{let n=-1/0,o,t=!1;return[(...i)=>{if(t)return null;let r=n+l-Date.now();return clearTimeout(o),o=window.setTimeout(()=>{n=Date.now(),a(...i)},Math.max(0,r)),null},()=>{t=!0,clearTimeout(o)}]},v=({threshold:a=24,throttleTime:l=100}={})=>{const n={};let o=!1,t=-1/0;const[i]=d(r=>{const c=r.target;!o&&c.scrollTop<t&&(o=!0),o&&c.scrollHeight-c.scrollTop-c.clientHeight<=a&&(o=!1),t=c.scrollTop},l);return n.onMount=r=>{t=r.scrollTop,r.addEventListener("scroll",i)},n.onUnmount=r=>{r.removeEventListener("scroll",i)},n.escapeHook=()=>o,n};function b({throttleTime:a=100,plugins:l=[],offset:n=0,...o}){const t="container"in o?o.container:document.querySelector(o.selector);if(t===null)throw new Error("Container element not found");const i=l.map(e=>{var s;return(s=e==null?void 0:e.onMount)==null?void 0:s.call(e,t)}).filter(e=>typeof e=="function"),r=()=>l.find(e=>{var s;return((s=e==null?void 0:e.escapeHook)==null?void 0:s.call(e,t))===!0})?!1:(requestAnimationFrame(()=>{t.scrollTo({top:t.scrollHeight-n})}),!0),[c]=a>0?d(r,a):[r],u=new ResizeObserver(()=>{c()}),m=new MutationObserver(()=>{h(),c()}),h=()=>{u.disconnect();for(const e of t.children)u.observe(e)};return h(),m.observe(t,{childList:!0}),()=>{u.disconnect(),m.disconnect(),i.forEach(e=>e(t)),l.forEach(e=>{var s;return(s=e==null?void 0:e.onUnmount)==null?void 0:s.call(e,t)})}}return f.default=b,f.escapeWhenUpPlugin=v,Object.defineProperties(f,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),f}({});