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