preline
Version:
Preline UI is an open-source set of prebuilt UI components based on the utility-first Tailwind CSS framework.
24 lines • 6.55 kB
JavaScript
var t={7615(t,e,l){l.d(e,{A:()=>o});
/*
* HSBasePlugin
* @version: 4.2.0
* @author: Preline Labs Ltd.
* @license: Licensed under MIT and Preline UI Fair Use License (https://preline.co/docs/license.html)
* Copyright 2024 Preline Labs Ltd.
*/
class o{constructor(t,e,l){this.el=t,this.options=e,this.events=l,this.el=t,this.options=e,this.events={}}createCollection(t,e){var l,o;let s=t;if(!Array.isArray(s)&&"undefined"!=typeof window){const t=null===(l=this.constructor)||void 0===l?void 0:l.name,e="string"==typeof t&&t.startsWith("HS")?`$hs${t.slice(2)}Collection`:null;e&&(Array.isArray(window[e])||(window[e]=[]),s=window[e])}Array.isArray(s)&&s.push({id:(null===(o=null==e?void 0:e.el)||void 0===o?void 0:o.id)||s.length+1,element:e})}fireEvent(t,e=null){if(this.events.hasOwnProperty(t))return this.events[t](e)}on(t,e){this.events[t]=e}}},8402(t,e,l){l.d(e,{A:()=>r});var o=l(1926),s=l(7615);
/*
* HSScrollspy
* @version: 4.2.0
* @author: Preline Labs Ltd.
* @license: Licensed under MIT and Preline UI Fair Use License (https://preline.co/docs/license.html)
* Copyright 2024 Preline Labs Ltd.
*/
class i extends s.A{constructor(t,e={}){super(t,e),this.isScrollingDown=!1,this.lastScrollTop=0;const l=t.getAttribute("data-hs-scrollspy-options"),o=l?JSON.parse(l):{},s=Object.assign(Object.assign({},o),e);this.ignoreScrollUp=void 0!==s.ignoreScrollUp&&s.ignoreScrollUp,this.links=this.el.querySelectorAll("[href]"),this.sections=[],this.scrollableId=this.el.getAttribute("data-hs-scrollspy-scrollable-parent"),this.scrollable=this.scrollableId?document.querySelector(this.scrollableId):document,this.onLinkClickListener=[],this.init()}scrollableScroll(t){const e=this.scrollable instanceof HTMLElement?this.scrollable.scrollTop:window.scrollY;this.isScrollingDown=e>this.lastScrollTop,this.lastScrollTop=e<=0?0:e,Array.from(this.sections).forEach(e=>{if(!e.getAttribute("id"))return!1;this.update(t,e)})}init(){this.createCollection(window.$hsScrollspyCollection,this),this.links.forEach(t=>{this.sections.push(this.scrollable.querySelector(t.getAttribute("href")))}),this.onScrollableScrollListener=t=>this.scrollableScroll(t),this.onPopstateListener=t=>this.handlePopstate(t),this.scrollable.addEventListener("scroll",this.onScrollableScrollListener),window.addEventListener("popstate",this.onPopstateListener),this.links.forEach(t=>{this.onLinkClickListener.push({el:t,fn:e=>this.linkClick(e,t)}),t.addEventListener("click",this.onLinkClickListener.find(e=>e.el===t).fn)})}determineScrollDirection(t){const e=this.el.querySelector("a.active");if(!e)return!0;const l=Array.from(this.links).indexOf(e),o=Array.from(this.links).indexOf(t);return-1===o||o>l}linkClick(t,e){t.preventDefault();const l=e.getAttribute("href");if(!l||"javascript:;"===l)return;(l?document.querySelector(l):null)&&(this.isScrollingDown=this.determineScrollDirection(e),this.scrollTo(e))}update(t,e){const l=parseInt((0,o.gj)(this.el,"--scrollspy-offset","0")),s=parseInt((0,o.gj)(e,"--scrollspy-offset"))||l,i=t.target===document?0:parseInt(String(t.target.getBoundingClientRect().top)),r=parseInt(String(e.getBoundingClientRect().top))-s-i,n=e.offsetHeight;if(this.ignoreScrollUp||this.isScrollingDown?r<=0&&r+n>0:r<=0&&r<n){this.links.forEach(t=>t.classList.remove("active"));const t=this.el.querySelector(`[href="#${e.getAttribute("id")}"]`);if(t){t.classList.add("active");const e=t.closest("[data-hs-scrollspy-group]");if(e){const t=e.querySelector("[href]");t&&t.classList.add("active")}}this.fireEvent("afterScroll",t),(0,o.JD)("afterScroll.hs.scrollspy",t,this.el)}}handlePopstate(t){const e=window.location.hash;if(!e)return;const l=this.el.querySelector(`[href="${e}"]`);if(!l)return;const s=document.querySelector(e);if(!s)return;this.isScrollingDown=this.determineScrollDirection(l);const i=parseInt((0,o.gj)(this.el,"--scrollspy-offset","0")),r=parseInt((0,o.gj)(s,"--scrollspy-offset"))||i,n=this.scrollable===document?0:this.scrollable.offsetTop,c=s.offsetTop-r-n,a=this.scrollable===document?window:this.scrollable;"scrollTo"in a&&a.scrollTo({top:c,left:0,behavior:"smooth"})}scrollTo(t){const e=t.getAttribute("href"),l=document.querySelector(e),s=parseInt((0,o.gj)(this.el,"--scrollspy-offset","0")),i=parseInt((0,o.gj)(l,"--scrollspy-offset"))||s,r=this.scrollable===document?0:this.scrollable.offsetTop,n=l.offsetTop-i-r,c=this.scrollable===document?window:this.scrollable,a=()=>{window.history.pushState(null,null,t.getAttribute("href")),"scrollTo"in c&&c.scrollTo({top:n,left:0,behavior:"smooth"})},h=this.fireEvent("beforeScroll",this.el);(0,o.JD)("beforeScroll.hs.scrollspy",this.el,this.el),h instanceof Promise?h.then(()=>a()):a()}destroy(){this.el.querySelector("[href].active").classList.remove("active"),this.scrollable.removeEventListener("scroll",this.onScrollableScrollListener),window.removeEventListener("popstate",this.onPopstateListener),this.onLinkClickListener.length&&this.onLinkClickListener.forEach(({el:t,fn:e})=>{t.removeEventListener("click",e)}),window.$hsScrollspyCollection=window.$hsScrollspyCollection.filter(({element:t})=>t.el!==this.el)}static getInstance(t,e=!1){const l=window.$hsScrollspyCollection.find(e=>e.element.el===("string"==typeof t?document.querySelector(t):t));return l?e?l:l.element.el:null}static autoInit(){window.$hsScrollspyCollection||(window.$hsScrollspyCollection=[]),window.$hsScrollspyCollection&&(window.$hsScrollspyCollection=window.$hsScrollspyCollection.filter(({element:t})=>document.contains(t.el))),document.querySelectorAll("[data-hs-scrollspy]:not(.--prevent-on-load-init)").forEach(t=>{window.$hsScrollspyCollection.find(e=>{var l;return(null===(l=null==e?void 0:e.element)||void 0===l?void 0:l.el)===t})||new i(t)})}}const r=i},1926(t,e,l){l.d(e,{JD:()=>s,gj:()=>o});
/*
* @version: 4.2.0
* @author: Preline Labs Ltd.
* @license: Licensed under MIT and Preline UI Fair Use License (https://preline.co/docs/license.html)
* Copyright 2024 Preline Labs Ltd.
*/
const o=(t,e,l="")=>(window.getComputedStyle(t).getPropertyValue(e)||l).replace(" ",""),s=(t,e,l=null)=>{const o=new CustomEvent(t,{detail:{payload:l},bubbles:!0,cancelable:!0,composed:!1});e.dispatchEvent(o)}}},e={};function l(o){var s=e[o];if(void 0!==s)return s.exports;var i=e[o]={exports:{}};return t[o](i,i.exports,l),i.exports}l.d=(t,e)=>{for(var o in e)l.o(e,o)&&!l.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},l.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var o=l(8402);window.addEventListener("load",()=>{o.A.autoInit()}),"undefined"!=typeof window&&(window.HSScrollspy=o.A);