state-in-url 4.1.6 → 4.1.8
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.
- package/README.md +2 -1
- package/dist/next/useUrlState/useUrlState.mjs +1 -1
- package/dist/subscribers.d.ts +1 -1
- package/dist/useSharedState/useSharedState.mjs +1 -1
- package/dist/useUrlEncode/useUrlEncode.d.ts +1 -1
- package/dist/useUrlEncode/useUrlEncode.mjs +1 -1
- package/dist/useUrlStateBase/useUrlStateBase.d.ts +1 -1
- package/dist/useUrlStateBase/useUrlStateBase.mjs +1 -1
- package/dist/utils.d.ts +2 -2
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -85,6 +85,7 @@ This library is a good alternative for NUQS.
|
|
|
85
85
|
- ✨ **Complex data**: Nested objects, dates and arrays, works same as JSON, but in URL
|
|
86
86
|
- ☂ **Default values**: Giving you default values if parameter not in url
|
|
87
87
|
- ⌨ **Organized**: All possible values defined at start, protect you from getting non existing key
|
|
88
|
+
- **compatible**: Will keep 3rd party query params as is
|
|
88
89
|
- **flexible**: Can use more than 1 state objects on the same page, just use different keys
|
|
89
90
|
- **Fast**: Minimal rerenders, around [1ms](https://github.com/asmyshlyaev177/state-in-url/blob/87c8c7c995c5cd7d9e7aa039c30bfe64b24abe4b/packages/urlstate/encoder/encoder.test.ts#L185) to encode and decode big object
|
|
90
91
|
- **Server Side Rendering**: Can use it in Server Components, Next.js 14 and 15 are supported
|
|
@@ -170,7 +171,7 @@ import { userState } from './userState';
|
|
|
170
171
|
|
|
171
172
|
function MyComponent() {
|
|
172
173
|
// can pass `replace` arg, it's control will `setUrl` will use `rounter.push` or `router.replace`, default replace=true
|
|
173
|
-
// can pass `searchParams` from server components
|
|
174
|
+
// can pass `searchParams` from server components, pass `useHistory: false` if you need to fetch smt in the server component
|
|
174
175
|
const { urlState, setUrl, setState, reset } = useUrlState(userState);
|
|
175
176
|
|
|
176
177
|
return (
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useRouter as
|
|
1
|
+
import r from"react";import{useRouter as C,useSearchParams as H}from"next/navigation";import{parseSPObj as f}from"../../parseSPObj.mjs";import{useUrlStateBase as g}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as d,isSSR as v,filterUnknownParams as P,filterUnknownParamsClient as w}from"../../utils.mjs";function E(t,s){const a="defaultState"in t?t.defaultState:t,U="defaultState"in t?t.searchParams:s?.searchParams,l="defaultState"in t?t.useHistory:s?.useHistory,h="defaultState"in t?{scroll:t.scroll,replace:t.replace}:{scroll:s?.scroll,replace:s?.replace},o=C(),b=r.useMemo(()=>({push:(...e)=>{l===void 0||l?d.push(...e):o.push(...e)},replace:(...e)=>{l===void 0||l?d.replace(...e):o.replace(...e)}}),[o]),{state:c,updateState:u,updateUrl:p,reset:i,getState:k}=g(a,b,({parse:e})=>v?f(P(a,U),a):e(w(a))),n=r.useMemo(()=>({...M,...h}),[]),m=r.useCallback((e,j)=>p(e,{...n,...j}),[p]),S=H();r.useLayoutEffect(()=>{u(P(a,f(Object.fromEntries([...S.entries()]),a)))},[S]);const y=r.useCallback(e=>{i({...n,...e})},[i]);return{setState:u,updateState:u,setUrl:m,updateUrl:m,urlState:c,state:c,reset:y,getState:k}}const M={replace:!0,scroll:!1};export{E as useUrlState};
|
package/dist/subscribers.d.ts
CHANGED
|
@@ -6,6 +6,6 @@ export declare const subscribers: {
|
|
|
6
6
|
};
|
|
7
7
|
export declare const stateMap: {
|
|
8
8
|
get: <T extends JSONCompatible | undefined>(objKey: T) => T | undefined;
|
|
9
|
-
set: <
|
|
9
|
+
set: <T_1 extends JSONCompatible>(objKey: T_1, value: T_1) => void;
|
|
10
10
|
};
|
|
11
11
|
export type Cb = () => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import u from"react";import{stateMap as n,subscribers as
|
|
1
|
+
import u from"react";import{stateMap as n,subscribers as o}from"../subscribers.mjs";import{useInsertionEffect as g}from"../useInsertionEffect.mjs";import{isSSR as l,isEqual as b}from"../utils.mjs";function d(a,c){const t=u.useRef(a),[f,i]=u.useState(()=>{if(l)return c?c?.():t.current;const e=n.get(t.current);if(e)return e;{const r=c?.()||t.current;return n.set(t.current,r),r}}),m=u.useCallback(e=>{const r=n.get(t.current),s=typeof e=="function"?e(r):{...r,...e};b(r,s)||(n.set(t.current,s),o.get(t.current).forEach(p=>p()))},[]);g(()=>{const e=()=>{i(n.get(t.current)||t.current)},r=o.add(t.current,e);return()=>{r()}},[]);const S=u.useCallback(()=>n.get(t.current)||t.current,[]);return{state:f,getState:S,setState:m}}export{d as useSharedState};
|
|
@@ -30,7 +30,7 @@ console.log(state); // Output: { name: 'Tom' }
|
|
|
30
30
|
*
|
|
31
31
|
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/useUrlEncode#parse}
|
|
32
32
|
*/
|
|
33
|
-
parse: (strOrSearchParams: string | URLSearchParams) =>
|
|
33
|
+
parse: (strOrSearchParams: string | URLSearchParams) => T;
|
|
34
34
|
/**
|
|
35
35
|
Converts a state object to a URL query string.
|
|
36
36
|
* * Example:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import o from"react";import{encodeState as a,decodeState as n}from"../encodeState/encodeState.mjs";import{typeOf as
|
|
1
|
+
import o from"react";import{encodeState as a,decodeState as n}from"../encodeState/encodeState.mjs";import{typeOf as s}from"../utils.mjs";function f(e){const c=o.useCallback((t,r)=>s(t)==="object"?a(t,e,r):"",[e]);return{parse:o.useCallback(t=>n(t,e),[e]),stringify:c}}export{f as useUrlEncode};
|
|
@@ -37,7 +37,7 @@ export declare function useUrlStateBase<T extends JSONCompatible>(defaultState:
|
|
|
37
37
|
parse: ReturnType<typeof useUrlEncode<T>>["parse"];
|
|
38
38
|
}) => T): {
|
|
39
39
|
updateState: (value: Partial<T> | ((currState: T) => T)) => void;
|
|
40
|
-
updateUrl: (value?:
|
|
40
|
+
updateUrl: (value?: Partial<T> | ((currState: T) => T) | undefined, options?: Options) => void;
|
|
41
41
|
state: T;
|
|
42
42
|
reset: (options?: Options) => void;
|
|
43
43
|
getState: () => T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import i from"react";import{TIMEOUT as R}from"../constants/constants.mjs";import{useInsertionEffect as T}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 b(t,o,u){const{parse:a,stringify:n}=L(t),{state:l,getState:r,setState:s}=v(t,()=>u?.({parse:a})||t),m=i.useRef(void 0);T(()=>{const e=()=>{const w=a(P(t));s(w)};return window.addEventListener($,e),()=>{window.removeEventListener($,e),clearTimeout(m.current)}},[s]);const c=i.useRef([]),p=i.useCallback((e,w)=>{const d=typeof e=="function"?e(r()):{...r(),...e},h=n(d,y(t)),S=`${window.location.pathname}${h.length?"?":""}${h}${window.location.hash}`,E=`${window.location.pathname}${window.location.search}${window.location.hash}`;if(S===E)return;s(d);const{replace:g,...k}=w||{};c.current.push([g?"replace":"push",S,k]),clearTimeout(m.current),queueMicrotask(()=>{c.current.length&&(m.current=setTimeout(()=>{const f=c.current.at(-1);c.current=[],o[f[0]](f[1],f[2])},R))})},[o,n,r]),U=i.useCallback(e=>{p(t,e)},[p]);return{updateState:s,updateUrl:p,state:l,reset:U,getState:r}}function y(t){const o=Object.keys(t),u=window.location.search,a=new URLSearchParams(u),n=new URLSearchParams;return a.forEach((l,r)=>!o.includes(r)&&n.set(r,l)),n}const $="popstate";export{b as useUrlStateBase};
|
package/dist/utils.d.ts
CHANGED
|
@@ -27,8 +27,8 @@ export declare function filterUnknownParams<T extends object>(shape: T, searchPa
|
|
|
27
27
|
export declare function filterUnknown<T extends object>(shape: T, entries: [key: string, value: string][]): string[][];
|
|
28
28
|
export declare function assignValue<T extends object>(shape: T, newVal: Partial<T>): T;
|
|
29
29
|
export interface Router {
|
|
30
|
-
push: (href: string, opts
|
|
31
|
-
replace: (href: string, opts
|
|
30
|
+
push: (href: string, opts?: object) => void;
|
|
31
|
+
replace: (href: string, opts?: object) => void;
|
|
32
32
|
}
|
|
33
33
|
export declare const routerHistory: Router;
|
|
34
34
|
export declare const isSafari: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "state-in-url",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.8",
|
|
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": {
|
|
@@ -364,6 +364,7 @@
|
|
|
364
364
|
"eslint-plugin-maintainable": "^0.4.4",
|
|
365
365
|
"eslint-plugin-prettier": "^5.2.1",
|
|
366
366
|
"eslint-plugin-react-hooks": "^4.6.2",
|
|
367
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
367
368
|
"happy-dom": "^15.11.6",
|
|
368
369
|
"husky": "^9.0.11",
|
|
369
370
|
"lint-staged": "^15.2.9",
|