state-in-url 4.2.1 → 4.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
import a from"react";import{useNavigate as
|
|
1
|
+
import a from"react";import{useNavigate as H,useSearchParams as g,useHref as k}from"react-router-dom";import{parseSPObj as P}from"../../parseSPObj.mjs";import{useUrlStateBase as h}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as y,filterUnknownParamsClient as C,assignValue as j,filterUnknownParams as w}from"../../utils.mjs";function E(t,l){const r="defaultState"in t?t.defaultState:t;const m="defaultState"in t?t.useHistory:l?.useHistory;const d="defaultState"in t?{replace:t.replace,preventScrollReset:t.preventScrollReset}:{replace:l?.replace,preventScrollReset:l?.preventScrollReset};const o=a.useMemo(()=>({...M,...d}),[]);const n=H();const U=a.useMemo(()=>m?y:{replace:(e,s)=>n(e,{...o,...s}),push:(e,s)=>n(e,{...o,...s})},[n]);const[c]=g();const v=k("/");const{state:p,updateState:u,updateUrl:i,getState:R,reset:S}=h(r,U,({parse:e})=>e(C(r,c.entries())),v);const f=a.useCallback((e,s)=>i(e,{...o,...s}),[i]);a.useEffect(()=>{u(j(r,w(r,P(Object.fromEntries([...c.entries()]),r))))},[c]);const b=a.useCallback(e=>{S({...o,...e})},[S]);return{setState:u,updateState:u,setUrl:f,updateUrl:f,urlState:p,state:p,reset:b,getState:R}}const M={replace:true,preventScrollReset:true};export{E as useUrlState};
|
|
@@ -35,7 +35,7 @@ import { type JSONCompatible, type Router } from "../utils";
|
|
|
35
35
|
*/
|
|
36
36
|
export declare function useUrlStateBase<T extends JSONCompatible>(defaultState: T, router: Router, getInitialState?: ({ parse, }: {
|
|
37
37
|
parse: ReturnType<typeof useUrlEncode<T>>["parse"];
|
|
38
|
-
}) => T): {
|
|
38
|
+
}) => T, basename?: string): {
|
|
39
39
|
updateState: (value: Partial<T> | ((currState: T) => T)) => void;
|
|
40
40
|
updateUrl: (value?: Parameters<(value: Partial<T> | ((currState: T) => T)) => void>[0], options?: Options) => void;
|
|
41
41
|
state: T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import u from"react";import{TIMEOUT as
|
|
1
|
+
import u from"react";import{TIMEOUT as T}from"../constants/constants.mjs";import{useInsertionEffect as b}from"../useInsertionEffect.mjs";import{useSharedState as v}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as L}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as P}from"../utils.mjs";function C(t,e,o,f){const{parse:s,stringify:c}=L(t);const{state:l,getState:a,setState:r}=v(t,()=>o?.({parse:s})||t);const w=u.useRef(void 0);b(()=>{const n=()=>{const p=s(P(t,q()));r(p)};window.addEventListener(g,n);return()=>{window.removeEventListener(g,n);clearTimeout(w.current)}},[r]);const i=u.useRef([]);const m=u.useCallback((n,p)=>{const h=typeof n==="function"?n(a()):{...a(),...n};const S=c(h,O(t));const $=I(window.location.pathname,f);const U=`${$}${S.length?"?":""}${S}${window.location.hash}`;const E=`${$}${window.location.search}${window.location.hash}`;if(U===E)return;r(h);const{replace:y,...R}=p||{};i.current.push([y?"replace":"push",U,R]);clearTimeout(w.current);queueMicrotask(()=>{if(!i.current.length)return;w.current=setTimeout(()=>{const d=i.current.at(-1);i.current=[];e[d[0]](d[1],d[2])},T)})},[e,c,a]);const k=u.useCallback(n=>{r(t);m(t,n)},[m,r]);return{updateState:r,updateUrl:m,state:l,reset:k,getState:a}}function O(t){const e=Object.keys(t);const o=window.location.search;const f=new URLSearchParams(o);const s=new URLSearchParams;for(const[c,l]of f){!e.includes(c)&&s.set(c,l)}return s}const g="popstate";function q(){return typeof window!=="undefined"&&window.location.search||""}function I(t,e){if(!e||e==="/"){return t}const o=t.slice(e.length);return o.startsWith("/")?o:"/"+o}export{C as useUrlStateBase};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "state-in-url",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.1",
|
|
4
4
|
"description": "Store state in URL as in object, types and structure are preserved, with TS validation. Same API as React.useState, wthout any hasssle or boilerplate. Next.js@14-15 and react-router@6-7.",
|
|
5
5
|
"homepage": "https://state-in-url.dev",
|
|
6
6
|
"repository": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"publishConfig": {
|
|
16
16
|
"registry": "https://registry.npmjs.org/",
|
|
17
17
|
"tag": "latest",
|
|
18
|
-
"provenance":
|
|
18
|
+
"provenance": true
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"funding": {
|