hybrids-router
Version:
Declarative routing for your Hybrids app.
2 lines (1 loc) • 1 kB
JavaScript
function t(t){const{href:n,origin:o}=t.location;return n.replace(o,"")}function n(t){const n="/"===t.slice(0,1);return"#"===t.slice(1,2)?t:n?"/#"+t.slice(1):"/#"+t}function o(t){const n="#"!==t.slice(0,1)&&"#"!==t.slice(1,2),o="/"!==t.slice(0,1);return o&&n?"/"+t:o&&!n?"/"+t.replace("#",""):o||n?t:t.replace("/#","/")}function e(e){const r=t=>{const r="history"===t.mode?t.routes.find(n=>o(n.path)===t.currentPath):t.routes.find(o=>n(o.path)===t.currentPath);return(null==r?void 0:r.component)||e.html``};return function({mode:n="hash",routes:o=[],shadowRoot:i=!0}){return{mode:n,routes:o,currentPath:t(window),render:e.render(r,{shadowRoot:i})}}}function r(e,r){const i=n(r),c=o(r),s=t(window);if(s===i||s===c)return;const{state:u}=window.history,{pathname:a}=window.location,h="history"===e.mode;e.currentPath=h?c:i,h?window.history.pushState(u,a,e.currentPath):window.location.href=e.currentPath,window.addEventListener("popstate",()=>e.currentPath=t(window))}export{e as createRouter,r as push};