state-in-url 4.1.7 → 4.1.9

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 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 y,useSearchParams as C}from"next/navigation";import s from"react";import{parseSPObj as S}from"../../parseSPObj.mjs";import{useUrlStateBase as H}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as R,isSSR as g,filterUnknownParams as f,filterUnknownParamsClient as j}from"../../utils.mjs";function w(t,a){const e="defaultState"in t?t.defaultState:t,d="defaultState"in t?t.searchParams:a?.searchParams,o="defaultState"in t?t.useHistory:a?.useHistory,P="defaultState"in t?{scroll:t.scroll,replace:t.replace}:{scroll:a?.scroll,replace:a?.replace},U=o===void 0||o?R:y(),{state:u,updateState:l,updateUrl:c,reset:i,getState:b}=H(e,U,({parse:r})=>g?S(f(e,d),e):r(j(e))),n=s.useMemo(()=>({...E,...P}),[]),m=s.useCallback((r,k)=>c(r,{...n,...k}),[c]),p=C();s.useEffect(()=>{l(f(e,S(Object.fromEntries([...p.entries()]),e)))},[p]);const h=s.useCallback(r=>{i({...n,...r})},[i]);return{setState:l,updateState:l,setUrl:m,updateUrl:m,urlState:u,state:u,reset:h,getState:b}}const E={replace:!0,scroll:!1};export{w as useUrlState};
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};
@@ -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: <T extends JSONCompatible>(objKey: T, value: T) => void;
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 s}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),o=typeof e=="function"?e(r):{...r,...e};b(r,o)||(n.set(t.current,o),s.get(t.current).forEach(p=>p()))},[]);g(()=>{const e=()=>{i(n.get(t.current)||t.current)},r=s.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};
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) => typeof stateShape;
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 f}from"../utils.mjs";function i(e){const c=o.useCallback((t,r)=>f(t)==="object"?a(t,e,r):"",[e]);return{parse:o.useCallback(t=>n(t,e),[e]),stringify:c}}export{i as useUrlEncode};
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?: Parameters<(value: Partial<T> | ((currState: T) => T)) => void>[0], options?: Options) => void;
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 u from"react";import{TIMEOUT as b}from"../constants/constants.mjs";import{useInsertionEffect as v}from"../useInsertionEffect.mjs";import{useSharedState as y}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as L}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as P}from"../utils.mjs";function C(t,a,l){const{parse:c,stringify:n}=L(t),{state:m,getState:r,setState:s}=y(t,()=>l?.({parse:c})||t),p=u.useRef(void 0);v(()=>{const e=()=>{const o=c(P(t));s(o)};return window.addEventListener(S,e),()=>{window.removeEventListener(S,e),clearTimeout(p.current)}},[s]);const i=u.useRef([]),f=u.useCallback((e,o)=>{const w=typeof e=="function"?e(r()):e?{...r(),...e}:r(),d=n(w,I(t)),h=`${window.location.pathname}${d.length?"?":""}${d}${window.location.hash}`,$=`${window.location.pathname}${window.location.search}${window.location.hash}`;if(h===$)return;s(w);const E=o?.replace;delete o?.replace,i.current.push([E?"replace":"push",h,o]),clearTimeout(p.current),p.current=setTimeout(()=>{queueMicrotask(()=>{if(!i.current.length)return;const g=i.current.at(-1);i.current=[];const[T,k,R]=g||[];a[T](k,R)})},b)},[a,n,r]),U=u.useCallback(e=>{f(t,e)},[f]);return{updateState:s,updateUrl:f,state:m,reset:U,getState:r}}function I(t){const a=Object.keys(t),l=window.location.search,c=new URLSearchParams(l),n=new URLSearchParams;return c.forEach((m,r)=>!a.includes(r)&&n.set(r,m)),n}const S="popstate";export{C as useUrlStateBase};
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,a,u){const{parse:s,stringify:n}=L(t),{state:l,getState:r,setState:o}=v(t,()=>u?.({parse:s})||t),m=i.useRef(void 0);T(()=>{const e=()=>{const w=s(P(t));o(w)};return window.addEventListener(U,e),()=>{window.removeEventListener(U,e),clearTimeout(m.current)}},[o]);const c=i.useRef([]),p=i.useCallback((e,w)=>{const d=typeof e=="function"?e(r()):{...r(),...e};o(d);const h=n(d,y(t)),S=`${window.location.pathname}${h.length?"?":""}${h}${window.location.hash}`,$=`${window.location.pathname}${window.location.search}${window.location.hash}`;if(S===$)return;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=[],a[f[0]](f[1],f[2])},R))})},[a,n,r]),E=i.useCallback(e=>{p(t,e)},[p,o]);return{updateState:o,updateUrl:p,state:l,reset:E,getState:r}}function y(t){const a=Object.keys(t),u=window.location.search,s=new URLSearchParams(u),n=new URLSearchParams;return s.forEach((l,r)=>!a.includes(r)&&n.set(r,l)),n}const U="popstate";export{b as useUrlStateBase};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-in-url",
3
- "version": "4.1.7",
3
+ "version": "4.1.9",
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",