UNPKG

preline

Version:

Preline UI is an open-source set of prebuilt UI components based on the utility-first Tailwind CSS framework.

24 lines 9.94 kB
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o=e();for(var s in o)("object"==typeof exports?exports:t)[s]=o[s]}}(self,()=>(()=>{"use strict";var t={2961(t,e){ /* * 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. */ Object.defineProperty(e,"__esModule",{value:!0});e.default=class{constructor(t,e,o){this.el=t,this.options=e,this.events=o,this.el=t,this.options=e,this.events={}}createCollection(t,e){var o,s;let l=t;if(!Array.isArray(l)&&"undefined"!=typeof window){const t=null===(o=this.constructor)||void 0===o?void 0:o.name,e="string"==typeof t&&t.startsWith("HS")?`$hs${t.slice(2)}Collection`:null;e&&(Array.isArray(window[e])||(window[e]=[]),l=window[e])}Array.isArray(l)&&l.push({id:(null===(s=null==e?void 0:e.el)||void 0===s?void 0:s.id)||l.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}}},2500(t,e,o){var s=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const l=s(o(9948));window.addEventListener("load",()=>{l.default.autoInit()}),"undefined"!=typeof window&&(window.HSScrollspy=l.default)},9948(t,e,o){ /* * 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. */ var s=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const l=o(292),i=s(o(2961));class r extends i.default{constructor(t,e={}){super(t,e),this.isScrollingDown=!1,this.lastScrollTop=0;const o=t.getAttribute("data-hs-scrollspy-options"),s=o?JSON.parse(o):{},l=Object.assign(Object.assign({},s),e);this.ignoreScrollUp=void 0!==l.ignoreScrollUp&&l.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 o=Array.from(this.links).indexOf(e),s=Array.from(this.links).indexOf(t);return-1===s||s>o}linkClick(t,e){t.preventDefault();const o=e.getAttribute("href");if(!o||"javascript:;"===o)return;(o?document.querySelector(o):null)&&(this.isScrollingDown=this.determineScrollDirection(e),this.scrollTo(e))}update(t,e){const o=parseInt((0,l.getClassProperty)(this.el,"--scrollspy-offset","0")),s=parseInt((0,l.getClassProperty)(e,"--scrollspy-offset"))||o,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,l.dispatch)("afterScroll.hs.scrollspy",t,this.el)}}handlePopstate(t){const e=window.location.hash;if(!e)return;const o=this.el.querySelector(`[href="${e}"]`);if(!o)return;const s=document.querySelector(e);if(!s)return;this.isScrollingDown=this.determineScrollDirection(o);const i=parseInt((0,l.getClassProperty)(this.el,"--scrollspy-offset","0")),r=parseInt((0,l.getClassProperty)(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"),o=document.querySelector(e),s=parseInt((0,l.getClassProperty)(this.el,"--scrollspy-offset","0")),i=parseInt((0,l.getClassProperty)(o,"--scrollspy-offset"))||s,r=this.scrollable===document?0:this.scrollable.offsetTop,n=o.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"})},d=this.fireEvent("beforeScroll",this.el);(0,l.dispatch)("beforeScroll.hs.scrollspy",this.el,this.el),d instanceof Promise?d.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 o=window.$hsScrollspyCollection.find(e=>e.element.el===("string"==typeof t?document.querySelector(t):t));return o?e?o:o.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 o;return(null===(o=null==e?void 0:e.element)||void 0===o?void 0:o.el)===t})||new r(t)})}}e.default=r},292(t,e){ /* * @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. */ Object.defineProperty(e,"__esModule",{value:!0}),e.stringToBoolean=e.menuSearchHistory=e.isScrollable=e.isParentOrElementHidden=e.isJson=e.isIpadOS=e.isIOS=e.isFormElement=e.isFocused=e.isEnoughSpace=e.isDirectChild=e.htmlToElement=e.getZIndex=e.getHighestZIndex=e.getClassPropertyAlt=e.getClassProperty=e.dispatch=e.debounce=e.classToClassList=e.afterTransition=void 0;e.stringToBoolean=t=>"true"===t;e.getClassProperty=(t,e,o="")=>(window.getComputedStyle(t).getPropertyValue(e)||o).replace(" ","");e.getClassPropertyAlt=(t,e,o="")=>{let s="";return t.classList.forEach(t=>{t.includes(e)&&(s=t)}),s.match(/:(.*)]/)?s.match(/:(.*)]/)[1]:o};const o=t=>window.getComputedStyle(t).getPropertyValue("z-index");e.getZIndex=o;e.getHighestZIndex=t=>{let e=Number.NEGATIVE_INFINITY;return t.forEach(t=>{let s=o(t);"auto"!==s&&(s=parseInt(s,10),s>e&&(e=s))}),e};e.isDirectChild=(t,e)=>{const o=t.children;for(let t=0;t<o.length;t++)if(o[t]===e)return!0;return!1};e.isEnoughSpace=(t,e,o="auto",s=10,l=null)=>{const i=e.getBoundingClientRect(),r=l?l.getBoundingClientRect():null,n=window.innerHeight,c=r?i.top-r.top:i.top,a=(l?r.bottom:n)-i.bottom,d=t.clientHeight+s;return"bottom"===o?a>=d:"top"===o?c>=d:c>=d||a>=d};e.isFocused=t=>document.activeElement===t;e.isFormElement=t=>t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement;e.isIOS=()=>!!/iPad|iPhone|iPod/.test(navigator.platform)||navigator.maxTouchPoints&&navigator.maxTouchPoints>2&&/MacIntel/.test(navigator.platform);e.isIpadOS=()=>navigator.maxTouchPoints&&navigator.maxTouchPoints>2&&/MacIntel/.test(navigator.platform);e.isJson=t=>{if("string"!=typeof t)return!1;const e=t.trim()[0],o=t.trim().slice(-1);if("{"===e&&"}"===o||"["===e&&"]"===o)try{return JSON.parse(t),!0}catch(t){return!1}return!1};const s=t=>{if(!t)return!1;return"none"===window.getComputedStyle(t).display||s(t.parentElement)};e.isParentOrElementHidden=s;e.isScrollable=t=>{const e=window.getComputedStyle(t),o=e.overflowY,s=e.overflowX,l=("scroll"===o||"auto"===o)&&t.scrollHeight>t.clientHeight,i=("scroll"===s||"auto"===s)&&t.scrollWidth>t.clientWidth;return l||i};e.debounce=(t,e=200)=>{let o;return(...s)=>{clearTimeout(o),o=setTimeout(()=>{t.apply(this,s)},e)}};e.dispatch=(t,e,o=null)=>{const s=new CustomEvent(t,{detail:{payload:o},bubbles:!0,cancelable:!0,composed:!1});e.dispatchEvent(s)};e.afterTransition=(t,e)=>{const o=()=>{e(),t.removeEventListener("transitionend",o,!0)},s=window.getComputedStyle(t),l=s.getPropertyValue("transition-duration");"none"!==s.getPropertyValue("transition-property")&&parseFloat(l)>0?t.addEventListener("transitionend",o,!0):e()};e.htmlToElement=t=>{const e=document.createElement("template");return t=t.trim(),e.innerHTML=t,e.content.firstChild};e.classToClassList=(t,e,o=" ",s="add")=>{t.split(o).forEach(t=>{t.trim()&&("add"===s?e.classList.add(t):e.classList.remove(t))})};const l={historyIndex:-1,addHistory(t){this.historyIndex=t},existsInHistory(t){return t>this.historyIndex},clearHistory(){this.historyIndex=-1}};e.menuSearchHistory=l}},e={};var o=function o(s){var l=e[s];if(void 0!==l)return l.exports;var i=e[s]={exports:{}};return t[s].call(i.exports,i,i.exports,o),i.exports}(2500);return o})());