UNPKG

spa-navigator

Version:

A lightweight JavaScript library for managing **Single Page Application (SPA)** navigation without reloading the page and url management.The SPA-Navigator NPM package helps implement 'soft' page loads for Single Page Applications (SPAs). This allows for a

1 lines 1.15 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.SPANavigator=e():t.SPANavigator=e()}(this,(()=>{return t={174:t=>{class e{constructor(t={}){this.pages=t.pages||[],this.defaultPage=t.defaultPage||"home",this.init()}init(){document.addEventListener("DOMContentLoaded",(()=>{let t=location.pathname.substring(1);t&&this.pages.includes(t)?this.showPage(t,!1):this.navigateTo(this.defaultPage,!1)})),window.onpopstate=t=>{t.state&&t.state.page?this.showPage(t.state.page,!1):this.showPage(this.defaultPage,!1)}}showPage(t,e=!0){this.pages.forEach((t=>{const e=document.getElementById(t);e&&(e.style.display="none")}));const o=document.getElementById(t);o?(o.style.display="block",window.scrollTo({top:0,behavior:"smooth"}),e&&history.pushState({page:t},"",`/${t}`)):console.error(`Page '${t}' not found.`)}navigateTo(t,e=!0){this.showPage(t,e)}}void 0!==t.exports?t.exports=e:window.SPANavigator=e}},e={},function o(s){var a=e[s];if(void 0!==a)return a.exports;var i=e[s]={exports:{}};return t[s](i,i.exports,o),i.exports}(174);var t,e}));