jotai-location
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -5,2 +5,6 @@ # Change Log | ||
## [0.3.0] - 2022-12-01 | ||
### Added | ||
- feat(atom-with-hash): allow optional setHash #4 | ||
## [0.2.0] - 2022-11-17 | ||
@@ -7,0 +11,0 @@ ### Added |
@@ -1,2 +0,2 @@ | ||
import{atom as n}from"jotai";import{unstable_NO_STORAGE_VALUE as e,atomWithStorage as t}from"jotai/utils";const a=()=>({pathname:window.location.pathname,searchParams:new URLSearchParams(window.location.search)}),o=(n,e)=>{const t=new URL(window.location.href);n.pathname&&(t.pathname=n.pathname),n.searchParams&&(t.search=n.searchParams.toString()),null!=e&&e.replace?window.history.replaceState(null,"",t):window.history.pushState(null,"",t)},i=n=>(window.addEventListener("popstate",n),()=>window.removeEventListener("popstate",n));function r(e){var t;const r=(null==e?void 0:e.getLocation)||a,s=(null==e?void 0:e.applyLocation)||o,l=(null==e?void 0:e.subscribe)||i,c=n(null!=(t=null==e?void 0:e.preloaded)?t:r());return c.onMount=n=>{const e=()=>n(r()),t=l(e);return e(),t},n(n=>n(c),(n,t,a)=>{t(c,a),s(n(c),e)})}function s(){return s=Object.assign?Object.assign.bind():function(n){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var a in t)Object.prototype.hasOwnProperty.call(t,a)&&(n[a]=t[a])}return n},s.apply(this,arguments)}function l(n,a,o){const i=(null==o?void 0:o.serialize)||JSON.stringify,r=(null==o?void 0:o.deserialize)||(n=>{try{return JSON.parse(n||"")}catch(n){return e}}),l=(null==o?void 0:o.subscribe)||(n=>(window.addEventListener("hashchange",n),()=>{window.removeEventListener("hashchange",n)})),c=n=>{null!=o&&o.replaceState?window.history.replaceState(null,"",`${window.location.pathname}${window.location.search}#${n.toString()}`):window.location.hash=n.toString()},w=s({getItem:n=>{if(void 0===window.location)return e;const t=new URLSearchParams(window.location.hash.slice(1)).get(n);return r(t)},setItem:(n,e)=>{const t=new URLSearchParams(window.location.hash.slice(1));t.set(n,i(e)),c(t)},removeItem:n=>{const e=new URLSearchParams(window.location.hash.slice(1));e.delete(n),c(e)}},(null==o?void 0:o.delayInit)&&{delayInit:!0},{subscribe:(n,e)=>l(()=>{const t=new URLSearchParams(window.location.hash.slice(1)).get(n);e(null!==t?r(t):a)})});return t(n,a,w)}export{l as atomWithHash,r as atomWithLocation}; | ||
import{atom as e}from"jotai";import{unstable_NO_STORAGE_VALUE as n,atomWithStorage as t}from"jotai/utils";const a=()=>({pathname:window.location.pathname,searchParams:new URLSearchParams(window.location.search)}),o=(e,n)=>{const t=new URL(window.location.href);e.pathname&&(t.pathname=e.pathname),e.searchParams&&(t.search=e.searchParams.toString()),null!=n&&n.replace?window.history.replaceState(null,"",t):window.history.pushState(null,"",t)},i=e=>(window.addEventListener("popstate",e),()=>window.removeEventListener("popstate",e));function r(n){var t;const r=(null==n?void 0:n.getLocation)||a,s=(null==n?void 0:n.applyLocation)||o,l=(null==n?void 0:n.subscribe)||i,c=e(null!=(t=null==n?void 0:n.preloaded)?t:r());return c.onMount=e=>{const n=()=>e(r()),t=l(n);return n(),t},e(e=>e(c),(e,t,a)=>{t(c,a),s(e(c),n)})}function s(){return s=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var a in t)Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a])}return e},s.apply(this,arguments)}function l(e,a,o){const i=(null==o?void 0:o.serialize)||JSON.stringify,r=(null==o?void 0:o.deserialize)||(e=>{try{return JSON.parse(e||"")}catch(e){return n}}),l=(null==o?void 0:o.subscribe)||(e=>(window.addEventListener("hashchange",e),()=>{window.removeEventListener("hashchange",e)}));null!=o&&o.replaceState&&console.warn("[DEPRECATED] Use setHash=replaceState instead");const c=null==o?void 0:o.setHash;let h=e=>{window.location.hash=e};("replaceState"===c||null!=o&&o.replaceState)&&(h=e=>{window.history.replaceState(null,"",`${window.location.pathname}${window.location.search}#${e}`)}),"function"==typeof c&&(h=c);const w=s({getItem:e=>{if(void 0===window.location)return n;const t=new URLSearchParams(window.location.hash.slice(1)).get(e);return r(t)},setItem:(e,n)=>{const t=new URLSearchParams(window.location.hash.slice(1));t.set(e,i(n)),h(t.toString())},removeItem:e=>{const n=new URLSearchParams(window.location.hash.slice(1));n.delete(e),h(n.toString())}},(null==o?void 0:o.delayInit)&&{delayInit:!0},{subscribe:(e,n)=>l(()=>{const t=new URLSearchParams(window.location.hash.slice(1)).get(e);n(null!==t?r(t):a)})});return t(e,a,w)}export{l as atomWithHash,r as atomWithLocation}; | ||
//# sourceMappingURL=index.modern.mjs.map |
@@ -1,2 +0,2 @@ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("jotai"),require("jotai/utils")):"function"==typeof define&&define.amd?define(["exports","jotai","jotai/utils"],t):t((n||self).jotaiLocation={},n.jotai,n.utils)}(this,function(n,t,e){var o=function(){return{pathname:window.location.pathname,searchParams:new URLSearchParams(window.location.search)}},i=function(n,t){var e=new URL(window.location.href);n.pathname&&(e.pathname=n.pathname),n.searchParams&&(e.search=n.searchParams.toString()),null!=t&&t.replace?window.history.replaceState(null,"",e):window.history.pushState(null,"",e)},a=function(n){return window.addEventListener("popstate",n),function(){return window.removeEventListener("popstate",n)}};function r(){return r=Object.assign?Object.assign.bind():function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o])}return n},r.apply(this,arguments)}n.atomWithHash=function(n,t,o){var i=(null==o?void 0:o.serialize)||JSON.stringify,a=(null==o?void 0:o.deserialize)||function(n){try{return JSON.parse(n||"")}catch(n){return e.unstable_NO_STORAGE_VALUE}},l=(null==o?void 0:o.subscribe)||function(n){return window.addEventListener("hashchange",n),function(){window.removeEventListener("hashchange",n)}},c=function(n){null!=o&&o.replaceState?window.history.replaceState(null,"",""+window.location.pathname+window.location.search+"#"+n.toString()):window.location.hash=n.toString()},u=r({getItem:function(n){if(void 0===window.location)return e.unstable_NO_STORAGE_VALUE;var t=new URLSearchParams(window.location.hash.slice(1)).get(n);return a(t)},setItem:function(n,t){var e=new URLSearchParams(window.location.hash.slice(1));e.set(n,i(t)),c(e)},removeItem:function(n){var t=new URLSearchParams(window.location.hash.slice(1));t.delete(n),c(t)}},(null==o?void 0:o.delayInit)&&{delayInit:!0},{subscribe:function(n,e){return l(function(){var o=new URLSearchParams(window.location.hash.slice(1)).get(n);e(null!==o?a(o):t)})}});return e.atomWithStorage(n,t,u)},n.atomWithLocation=function(n){var e,r=(null==n?void 0:n.getLocation)||o,l=(null==n?void 0:n.applyLocation)||i,c=(null==n?void 0:n.subscribe)||a,u=t.atom(null!=(e=null==n?void 0:n.preloaded)?e:r());return u.onMount=function(n){var t=function(){return n(r())},e=c(t);return t(),e},t.atom(function(n){return n(u)},function(t,e,o){e(u,o),l(t(u),n)})}}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("jotai"),require("jotai/utils")):"function"==typeof define&&define.amd?define(["exports","jotai","jotai/utils"],t):t((n||self).jotaiLocation={},n.jotai,n.utils)}(this,function(n,t,e){var a=function(){return{pathname:window.location.pathname,searchParams:new URLSearchParams(window.location.search)}},o=function(n,t){var e=new URL(window.location.href);n.pathname&&(e.pathname=n.pathname),n.searchParams&&(e.search=n.searchParams.toString()),null!=t&&t.replace?window.history.replaceState(null,"",e):window.history.pushState(null,"",e)},i=function(n){return window.addEventListener("popstate",n),function(){return window.removeEventListener("popstate",n)}};function r(){return r=Object.assign?Object.assign.bind():function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&(n[a]=e[a])}return n},r.apply(this,arguments)}n.atomWithHash=function(n,t,a){var o=(null==a?void 0:a.serialize)||JSON.stringify,i=(null==a?void 0:a.deserialize)||function(n){try{return JSON.parse(n||"")}catch(n){return e.unstable_NO_STORAGE_VALUE}},l=(null==a?void 0:a.subscribe)||function(n){return window.addEventListener("hashchange",n),function(){window.removeEventListener("hashchange",n)}};null!=a&&a.replaceState&&console.warn("[DEPRECATED] Use setHash=replaceState instead");var c=null==a?void 0:a.setHash,u=function(n){window.location.hash=n};("replaceState"===c||null!=a&&a.replaceState)&&(u=function(n){window.history.replaceState(null,"",""+window.location.pathname+window.location.search+"#"+n)}),"function"==typeof c&&(u=c);var s=r({getItem:function(n){if(void 0===window.location)return e.unstable_NO_STORAGE_VALUE;var t=new URLSearchParams(window.location.hash.slice(1)).get(n);return i(t)},setItem:function(n,t){var e=new URLSearchParams(window.location.hash.slice(1));e.set(n,o(t)),u(e.toString())},removeItem:function(n){var t=new URLSearchParams(window.location.hash.slice(1));t.delete(n),u(t.toString())}},(null==a?void 0:a.delayInit)&&{delayInit:!0},{subscribe:function(n,e){return l(function(){var a=new URLSearchParams(window.location.hash.slice(1)).get(n);e(null!==a?i(a):t)})}});return e.atomWithStorage(n,t,s)},n.atomWithLocation=function(n){var e,r=(null==n?void 0:n.getLocation)||a,l=(null==n?void 0:n.applyLocation)||o,c=(null==n?void 0:n.subscribe)||i,u=t.atom(null!=(e=null==n?void 0:n.preloaded)?e:r());return u.onMount=function(n){var t=function(){return n(r())},e=c(t);return t(),e},t.atom(function(n){return n(u)},function(t,e,a){e(u,a),l(t(u),n)})}}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -8,5 +8,9 @@ import type { WritableAtom } from 'jotai'; | ||
delayInit?: boolean; | ||
/** | ||
* @deprecated Use {@link options.setHash} with 'replaceState' instead | ||
*/ | ||
replaceState?: boolean; | ||
subscribe?: (callback: () => void) => () => void; | ||
setHash?: 'default' | 'replaceState' | ((searchParams: string) => void); | ||
}): WritableAtom<Value, SetStateActionWithReset<Value>>; | ||
export {}; |
{ | ||
"name": "jotai-location", | ||
"description": "👻🔗", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": "Daishi Kato", | ||
@@ -36,3 +36,4 @@ "repository": { | ||
"examples:01_minimal": "DIR=01_minimal EXT=js webpack serve", | ||
"examples:02_typescript": "DIR=02_typescript EXT=tsx webpack serve" | ||
"examples:02_typescript": "DIR=02_typescript EXT=tsx webpack serve", | ||
"examples:03_hash": "DIR=03_hash EXT=tsx webpack serve" | ||
}, | ||
@@ -39,0 +40,0 @@ "jest": { |
@@ -5,4 +5,7 @@ # jotai-location | ||
https://jotai.org/docs/integrations/location | ||
## Tweets | ||
- [Initial announcement](https://twitter.com/dai_shi/status/1558093027024875520) | ||
- [v0.2.0 announcement](https://twitter.com/dai_shi/status/1593219435896410114) |
@@ -22,4 +22,8 @@ // TODO consider refactoring without atomWithStorage | ||
delayInit?: boolean; | ||
/** | ||
* @deprecated Use {@link options.setHash} with 'replaceState' instead | ||
*/ | ||
replaceState?: boolean; | ||
subscribe?: (callback: () => void) => () => void; | ||
setHash?: 'default' | 'replaceState' | ((searchParams: string) => void); | ||
}, | ||
@@ -45,15 +49,21 @@ ): WritableAtom<Value, SetStateActionWithReset<Value>> { | ||
}); | ||
const setHash = (searchParams: URLSearchParams) => { | ||
if (options?.replaceState) { | ||
if (options?.replaceState) { | ||
console.warn('[DEPRECATED] Use setHash=replaceState instead'); | ||
} | ||
const setHashOption = options?.setHash; | ||
let setHash = (searchParams: string) => { | ||
window.location.hash = searchParams; | ||
}; | ||
if (setHashOption === 'replaceState' || options?.replaceState) { | ||
setHash = (searchParams) => { | ||
window.history.replaceState( | ||
null, | ||
'', | ||
`${window.location.pathname}${ | ||
window.location.search | ||
}#${searchParams.toString()}`, | ||
`${window.location.pathname}${window.location.search}#${searchParams}`, | ||
); | ||
} else { | ||
window.location.hash = searchParams.toString(); | ||
} | ||
}; | ||
}; | ||
} | ||
if (typeof setHashOption === 'function') { | ||
setHash = setHashOption; | ||
} | ||
const hashStorage = { | ||
@@ -71,3 +81,3 @@ getItem: (k: string) => { | ||
searchParams.set(k, serialize(newValue)); | ||
setHash(searchParams); | ||
setHash(searchParams.toString()); | ||
}, | ||
@@ -77,3 +87,3 @@ removeItem: (k: string) => { | ||
searchParams.delete(k); | ||
setHash(searchParams); | ||
setHash(searchParams.toString()); | ||
}, | ||
@@ -80,0 +90,0 @@ ...(options?.delayInit && { delayInit: true }), |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43593
236
11