UNPKG

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