jotai-location
Version:
3 lines (2 loc) • 2.37 kB
JavaScript
import{atom as n}from"jotai/vanilla";import{RESET as o}from"jotai/vanilla/utils";function t(){return t=Object.assign?Object.assign.bind():function(n){for(var o=1;o<arguments.length;o++){var t=arguments[o];for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&(n[e]=t[e])}return n},t.apply(this,arguments)}const e=()=>"undefined"!=typeof window&&window.location?{pathname:window.location.pathname,searchParams:new URLSearchParams(window.location.search),hash:window.location.hash}:{},a=(n,o)=>{const t=new URL(window.location.href);"pathname"in n&&(t.pathname=n.pathname),"searchParams"in n&&(t.search=n.searchParams.toString()),"hash"in n&&(t.hash=n.hash),null!=o&&o.replace?window.history.replaceState(window.history.state,"",t):window.history.pushState(null,"",t)},i=n=>(window.addEventListener("popstate",n),()=>window.removeEventListener("popstate",n));function r(o){var r;const s=(null==o?void 0:o.getLocation)||e,l=(null==o?void 0:o.applyLocation)||a,c=(null==o?void 0:o.subscribe)||i,h=n(null!=(r=null==o?void 0:o.preloaded)?r:s());return"production"!==process.env.NODE_ENV&&(h.debugPrivate=!0),h.onMount=n=>{const o=()=>n(s()),t=c(o);return o(),t},n(n=>n(h),(n,e,a,i={})=>{e(h,a),l(n(h),t({},o,i))})}const s=n=>{window.location.hash=n},l=n=>{window.history.replaceState(window.history.state,"",`${window.location.pathname}${window.location.search}#${n}`)};function c(t,e,a){const i=(null==a?void 0:a.serialize)||JSON.stringify,r=(null==a?void 0:a.deserialize)||(n=>o=>{try{return JSON.parse(o)}catch(o){return n}})(e),c=(null==a?void 0:a.subscribe)||(n=>(window.addEventListener("hashchange",n),()=>{window.removeEventListener("hashchange",n)})),h="undefined"!=typeof window&&!!window.location,w=n(h?new URLSearchParams(window.location.hash.slice(1)).get(t):null);w.onMount=n=>{if(!h)return;const o=()=>{n(new URLSearchParams(window.location.hash.slice(1)).get(t))},e=c(o);return o(),e};const d=n(n=>{const o=n(w);return null===o?e:r(o)});return n(n=>n(d),(n,e,r,c)=>{var h;const u="function"==typeof r?r(n(d)):r,p=new URLSearchParams(window.location.hash.slice(1));if(u===o)e(w,null),p.delete(t);else{const n=i(u);e(w,n),p.set(t,n)}var v;("replaceState"===(v=null!=(h=null==c?void 0:c.setHash)?h:null==a?void 0:a.setHash)?l:"function"==typeof v?v:s)(p.toString())})}export{c as atomWithHash,r as atomWithLocation};
//# sourceMappingURL=index.modern.mjs.map