@rethink-js/rt-smooth-scroll
Version:
Lightweight smooth scrolling powered by Lenis with automatic loading, attribute config, and multi-instance support.
4 lines (3 loc) • 13.3 kB
JavaScript
/*! @rethink-js/rt-smooth-scroll v1.3.0 | MIT */
(()=>{(function(){var j="rtSmoothScroll";if(window[j]&&window[j].__initialized)return;function E(o,l){return!o||!o.hasAttribute(l)?null:o.getAttribute(l)}function N(o){var l=document.documentElement,a=document.body,e=E(l,o);return e!==null||(e=E(a,o),e!==null)?e:null}function H(o){var l=document.documentElement,a=document.body;return!!(l&&l.hasAttribute(o)||a&&a.hasAttribute(o))}function b(o,l){if(o==null)return l;var a=String(o).trim().toLowerCase();return a===""||a==="true"||a==="1"||a==="yes"||a==="y"||a==="on"?!0:a==="false"||a==="0"||a==="no"||a==="n"||a==="off"?!1:l}function q(o,l){if(o==null)return l;var a=String(o).trim();if(!a.length)return l;var e=Number(a);return Number.isFinite(e)?e:l}function W(o,l){if(o==null)return l;var a=String(o);return a.length?a:l}function T(o){return o<0?0:o>1?1:o}function P(o){if(!o)return null;var l=String(o).trim();if(!l)return null;if(l==="window")return window;var a=l.match(/^(.*)\(\s*(\d+)\s*\)$/);if(a){var e=a[1].trim(),d=parseInt(a[2],10);if(!e||isNaN(d)||d<1)return null;try{var c=document.querySelectorAll(e);return c[d-1]||null}catch(g){return null}}try{return document.querySelector(l)}catch(g){return null}}function ie(o){var l=String(o||"").trim();if(!l)return null;var a={linear:function(e){return T(e)},easeInQuad:function(e){return e=T(e),e*e},easeOutQuad:function(e){return e=T(e),e*(2-e)},easeInOutQuad:function(e){return e=T(e),e<.5?2*e*e:1-Math.pow(-2*e+2,2)/2},easeInCubic:function(e){return e=T(e),e*e*e},easeOutCubic:function(e){return e=T(e),1-Math.pow(1-e,3)},easeInOutCubic:function(e){return e=T(e),e<.5?4*e*e*e:1-Math.pow(-2*e+2,3)/2},easeInOutSine:function(e){return e=T(e),-(Math.cos(Math.PI*e)-1)/2},easeOutExpo:function(e){return e=T(e),e===1?1:1-Math.pow(2,-10*e)}};return a[l]||null}function oe(){var o=document.body;if(o){var l=document.querySelectorAll("[rt-smooth-scroll-instance]"),a=l&&l.length>0;!H("rt-smooth-scroll")&&!a&&o.setAttribute("rt-smooth-scroll","")}}function _(o){return o!=null}function U(o){var l="rt-smooth-scroll-";function a(r){var i=o(r);return _(i)?i:N(r)}function e(r){return a(l+r)}function d(r){return _(e(r))}var c={},g=d("lerp"),t=q(e("lerp"),void 0),p=d("duration"),w=q(e("duration"),void 0),F=d("easing"),v=W(e("easing"),""),n=ie(v);g&&t!==void 0?c.lerp=t:(p&&w!==void 0&&(c.duration=w),F&&n&&(c.easing=n)),d("orientation")&&(c.orientation=W(e("orientation"),"")),d("gesture-orientation")&&(c.gestureOrientation=W(e("gesture-orientation"),""));var A=e("smooth-wheel"),z=e("normalize-wheel");if(_(A)?c.smoothWheel=b(A,!0):_(z)&&(c.smoothWheel=b(z,!0)),d("wheel-multiplier")&&(c.wheelMultiplier=q(e("wheel-multiplier"),void 0)),d("touch-multiplier")&&(c.touchMultiplier=q(e("touch-multiplier"),void 0)),d("sync-touch")&&(c.syncTouch=b(e("sync-touch"),!1)),d("sync-touch-lerp")&&(c.syncTouchLerp=q(e("sync-touch-lerp"),void 0)),d("touch-inertia-exponent")&&(c.touchInertiaExponent=q(e("touch-inertia-exponent"),void 0)),d("infinite")&&(c.infinite=b(e("infinite"),!1)),d("auto-resize")&&(c.autoResize=b(e("auto-resize"),!0)),d("overscroll")&&(c.overscroll=b(e("overscroll"),!0)),d("anchors")){var y=String(e("anchors")||"").trim();if(y===""||y.toLowerCase()==="true")c.anchors=!0;else if(y.toLowerCase()==="false")c.anchors=!1;else try{c.anchors=JSON.parse(y)}catch(r){c.anchors=!0}}d("auto-toggle")&&(c.autoToggle=b(e("auto-toggle"),!1)),d("allow-nested-scroll")&&(c.allowNestedScroll=b(e("allow-nested-scroll"),!1));var I=a(l+"options-json");if(I)try{var O=JSON.parse(I);if(O&&typeof O=="object")for(var u in O)c[u]=O[u]}catch(r){}return c}function ue(o){return new Promise(function(l,a){if(typeof window.Lenis!="undefined")return l();var e=document.querySelector('script[data-rt-lenis="true"]');if(e){e.addEventListener("load",function(){l()}),e.addEventListener("error",function(c){a(c)});return}var d=document.createElement("script");d.src=o,d.async=!0,d.dataset.rtLenis="true",d.onload=function(){l()},d.onerror=function(c){a(c)},document.head.appendChild(d)})}function le(o){if(o==null)return null;var l=String(o).trim();if(!l.length)return null;var a=l.toLowerCase();function e(v){if(!v)return null;try{return document.querySelector(v)}catch(n){return null}}function d(v){if(!v)return!1;try{return v.click(),!0}catch(n){return!1}}function c(v){if(!v)return!1;try{return typeof v.focus=="function"&&v.focus(),!0}catch(n){return!1}}if(a.indexOf("{")===0||a.indexOf("[")===0)try{var g=JSON.parse(l);return function(v,n){try{if(!g)return;for(var A=Array.isArray(g)?g:[g],z=0;z<A.length;z++){var y=A[z];if(!(!y||typeof y!="object")){var I=typeof y.type=="string"?y.type.trim().toLowerCase():"",O=typeof y.selector=="string"?y.selector.trim():"",u=typeof y.name=="string"?y.name.trim():"",r=y.detail!==void 0?y.detail:void 0;if(I==="click")d(e(O));else if(I==="focus")c(e(O));else if(I==="dispatch"){var i=u||O;if(i)try{var s=new CustomEvent(i,{detail:{trigger:n&&n.trigger?n.trigger:null,target:n&&n.target?n.target:null,value:n&&n.value?n.value:null,id:n&&n.id?n.id:null,detail:r}});window.dispatchEvent(s)}catch(m){}}else if(I==="call"){var f=u||O;if(f&&window[f]&&typeof window[f]=="function")try{window[f]({lenis:v||null,trigger:n&&n.trigger?n.trigger:null,target:n&&n.target?n.target:null,value:n&&n.value?n.value:null,id:n&&n.id?n.id:null,detail:r})}catch(m){}}}}}catch(m){}}}catch(v){}var t=l.match(/^\s*([a-zA-Z]+)\s*:\s*(.+)\s*$/);if(t){var p=String(t[1]||"").trim().toLowerCase(),w=String(t[2]||"").trim();if(!w.length)return null;if(p==="click")return function(){d(e(w))};if(p==="focus")return function(){c(e(w))};if(p==="dispatch"||p==="event")return function(v,n){try{var A=new CustomEvent(w,{detail:{trigger:n&&n.trigger?n.trigger:null,target:n&&n.target?n.target:null,value:n&&n.value?n.value:null,id:n&&n.id?n.id:null}});window.dispatchEvent(A)}catch(z){}};if(p==="call"||p==="fn"||p==="function")return function(v,n){try{window[w]&&typeof window[w]=="function"&&window[w]({lenis:v||null,trigger:n&&n.trigger?n.trigger:null,target:n&&n.target?n.target:null,value:n&&n.value?n.value:null,id:n&&n.id?n.id:null})}catch(A){}}}var F=e(l);return F?function(){d(F)}:null}function V(){var o=N("rt-smooth-scroll-anchor-links");if(b(o,!1))for(var l=N("rt-smooth-scroll-anchor-links-on-complete"),a=document.querySelectorAll('a[href*="#"]'),e=window.location.pathname.replace(/\/+$/,"").toLowerCase(),d=window.location.origin,c=0;c<a.length;c++){var g=a[c];if(!g.hasAttribute("rt-smooth-scroll-to")){var t=g.getAttribute("href");if(t){var p=t.indexOf("#");if(p!==-1){var w=t.substring(0,p),F=t.substring(p);if(!(F.length<=1)){var v=!1;if(w===""||w==="./")v=!0;else{var n=w.replace(/\/+$/,"").toLowerCase();if(w.indexOf("http")===0)try{var A=new URL(t,d);A.origin===d&&A.pathname.replace(/\/+$/,"").toLowerCase()===e&&(v=!0)}catch(z){}else n===e&&(v=!0)}v&&(g.setAttribute("rt-smooth-scroll-to",F),!g.hasAttribute("rt-smooth-scroll-on-complete")&&l!==null&&l!==void 0&&g.setAttribute("rt-smooth-scroll-on-complete",String(l)),g.removeAttribute("href"),g.style.cursor="pointer",g.setAttribute("tabindex","0"),g.setAttribute("role","button"))}}}}}}function Z(){oe(),V();var o=H("rt-smooth-scroll"),l=document.querySelectorAll("[rt-smooth-scroll-instance]"),a=l&&l.length>0,e=o||a;if(!e)return;var d=W(N("rt-smooth-scroll-lenis-src"),"https://cdn.jsdelivr.net/npm/lenis@1.3.16/dist/lenis.min.js"),c=q(N("rt-smooth-scroll-resize-debounce-ms"),0),g=b(N("rt-smooth-scroll-debug"),!0),t={destroyed:!1,rafId:0,instances:{},order:[],resizeTimers:{},clickListener:null,keyListener:null};function p(u){var r=t.instances[u];if(!(!r||t.destroyed)){if(c>0){clearTimeout(t.resizeTimers[u]),t.resizeTimers[u]=setTimeout(function(){var i=t.instances[u];!i||t.destroyed||i.resize()},c);return}r.resize()}}function w(){function u(r){if(!t.destroyed){for(var i=0;i<t.order.length;i++){var s=t.order[i],f=t.instances[s];f&&f.raf(r)}t.rafId=requestAnimationFrame(u)}}t.rafId=requestAnimationFrame(u)}function F(u){var r={};for(var i in u)if(Object.prototype.hasOwnProperty.call(u,i)){var s=u[i];typeof s=="function"?r[i]="[Function]":r[i]=s}return r}function v(u,r){if(!r)return null;var i=String(r).trim();if(!i.length)return null;if(i==="window")return window;try{return(u||document).querySelector(i)}catch(s){return null}}function n(u,r){var i="rt-smooth-scroll-",s=E(u,i+"wrapper"),f=E(u,i+"content"),m=E(u,i+"events-target");if(s){var h=v(document,s);h&&(r.wrapper=h)}if(f){var L=v(document,f);L&&(r.content=L)}if(m){var C=v(document,m);C&&(r.eventsTarget=C)}return r}function A(u){var r=E(u,"rt-smooth-scroll-content");if(r){var i=null;try{i=u.querySelector(r)}catch(s){i=null}if(i)return i}return u.firstElementChild||u}function z(u,r,i,s,f){var m=s||{};f||(r&&(m.wrapper=r),i&&(m.content=i));var h=new window.Lenis(m);if(t.instances[u]=h,t.order.push(u),u==="root"&&(window.lenis=h),g)try{console.log("[rt-smooth-scroll] instance:",u,{wrapper:f?m.wrapper||"default":r,content:f?m.content||"default":i,options:F(m)})}catch(L){}return h}function y(){if(!t.clickListener){var u=function(r,i){var s=r.getAttribute("rt-smooth-scroll-to");if(s){i&&i.preventDefault();var f=null,m=parseFloat(s);if(s==="top"?f=0:!isNaN(m)&&isFinite(m)?f=m:f=P(s),!(f===null&&s!=="top"&&isNaN(m))){var h=null,L=r.getAttribute("rt-smooth-scroll-target-id");if(L&&t.instances[L])h=t.instances[L];else{var C=r.closest("[rt-smooth-scroll-instance]");if(C){var k=C.getAttribute("rt-smooth-scroll-id");k&&t.instances[k]&&(h=t.instances[k])}}if(!h&&t.instances.root&&(h=t.instances.root),!!h){h.resize();var S={},R=r.getAttribute("rt-smooth-scroll-offset");if(R){var M=parseFloat(R);if(!isNaN(M)&&isFinite(M))S.offset=M;else{var J=P(R);J&&(S.offset=-1*J.offsetHeight)}}var K=q(r.getAttribute("rt-smooth-scroll-duration"),void 0);K!==void 0&&(S.duration=K);var X=b(r.getAttribute("rt-smooth-scroll-immediate"),null);X!==null&&(S.immediate=X);var Y=b(r.getAttribute("rt-smooth-scroll-lock"),null);Y!==null&&(S.lock=Y);var x=b(r.getAttribute("rt-smooth-scroll-force"),null);x!==null&&(S.force=x);var ee=r.getAttribute("rt-smooth-scroll-on-complete"),ae=N("rt-smooth-scroll-on-complete"),re=le(_(ee)?ee:ae),se={trigger:r,target:f,value:s,id:L||null},$=null;re&&($=function(G){try{re(G,se)}catch(D){}});var te=!1,B=S.onComplete;S.onComplete=function(G){if(f instanceof Element&&!te){te=!0;try{h.resize()}catch(Q){}var D={};for(var ne in S)D[ne]=S[ne];delete D.onComplete,D.onComplete=function(Q){if(B)try{B(Q)}catch(ce){}if($)try{$(Q)}catch(ce){}},h.scrollTo(f,D);return}if(B)try{B(G)}catch(Q){}if($)try{$(G)}catch(Q){}},h.scrollTo(f,S)}}}};t.clickListener=function(r){var i=r.target.closest("[rt-smooth-scroll-to]");i&&u(i,r)},t.keyListener=function(r){if(!(r.key!=="Enter"&&r.key!==" ")){var i=r.target.closest("[rt-smooth-scroll-to]");i&&!i.hasAttribute("href")&&u(i,r)}},document.addEventListener("click",t.clickListener),document.addEventListener("keydown",t.keyListener)}}function I(){function u(r,i){if(typeof r=="string"&&r.length){var s=t.instances[r];s&&i(r,s);return}for(var f=0;f<t.order.length;f++){var m=t.order[f],h=t.instances[m];h&&i(m,h)}}return{__initialized:!0,ids:function(){return t.order.slice()},get:function(r){return t.instances[r]||null},start:function(r){u(r,function(i,s){s.start()})},stop:function(r){u(r,function(i,s){s.stop()})},toggle:function(r,i){u(r,function(s,f){if(typeof i=="boolean"){i?f.stop():f.start();return}f.isStopped?f.start():f.stop()})},resize:function(r){u(r,function(i){p(i)})},refreshAnchors:function(){V()},destroy:function(r){if(t.destroyed)return;function i(s){clearTimeout(t.resizeTimers[s]);var f=t.instances[s];if(f){try{f.destroy()}catch(h){}delete t.instances[s]}var m=t.order.indexOf(s);if(m>=0&&t.order.splice(m,1),s==="root")try{delete window.lenis}catch(h){window.lenis=void 0}}if(typeof r=="string"&&r.length){i(r);return}for(t.clickListener&&(document.removeEventListener("click",t.clickListener),t.clickListener=null),t.keyListener&&(document.removeEventListener("keydown",t.keyListener),t.keyListener=null);t.order.length;)i(t.order[0]);t.destroyed=!0,t.rafId&&cancelAnimationFrame(t.rafId)}}}function O(u){window.disableScroll=function(){u.stop(),document.body&&document.body.classList.add("no-scroll")},window.enableScroll=function(){u.start(),document.body&&document.body.classList.remove("no-scroll")}}ue(d).then(function(){if(!t.destroyed){var u=document.querySelectorAll("[rt-smooth-scroll-instance]"),r=(o?1:0)+(u?u.length:0),i=r===1&&o&&(!u||u.length===0);if(o&&!t.instances.root){var s=U(function(){return null});if(s=n(document.body||document.documentElement,s),i){var f=N("rt-smooth-scroll-auto-raf"),m=_(f)?b(f,!0):!0;s.autoRaf=m}else s.autoRaf=!1;z("root",null,null,s,!0)}for(var h=0,L=0;L<u.length;L++){var C=u[L],k=E(C,"rt-smooth-scroll-id");if(k||(h++,k="instance-"+h,C.setAttribute("rt-smooth-scroll-id",k)),!t.instances[k]){var S=A(C),R=U(function(J){return E(C,J)});R=n(C,R),R.autoRaf=!1,z(k,C,S,R,!1)}}i||w();var M=I();window[j]=M,O(M),y(),window.addEventListener("resize",function(){M.resize()})}}).catch(function(){})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",Z):Z(),window[j]=window[j]||{__initialized:!0,ids:function(){return[]},get:function(){return null}}})();})();
//# sourceMappingURL=index.min.js.map