state-in-url 4.1.12 → 4.1.13
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 r from"react";import{useRouter as C,useSearchParams as H}from"next/navigation";import{parseSPObj as
|
|
1
|
+
import r from"react";import{useRouter as C,useSearchParams as H}from"next/navigation";import{parseSPObj as d}from"../../parseSPObj.mjs";import{useUrlStateBase as g}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as U,filterUnknownParams as h,isSSR as v,filterUnknownParamsClient as w}from"../../utils.mjs";function E(t,s){const a="defaultState"in t?t.defaultState:t,c="defaultState"in t?t.searchParams:s?.searchParams,l="defaultState"in t?t.useHistory:s?.useHistory,P="defaultState"in t?{scroll:t.scroll,replace:t.replace}:{scroll:s?.scroll,replace:s?.replace},u=C(),b=r.useMemo(()=>({push:(...e)=>{l===void 0||l?U.push(...e):u.push(...e)},replace:(...e)=>{l===void 0||l?U.replace(...e):u.replace(...e)}}),[u]),{state:p,updateState:o,updateUrl:i,reset:n,getState:k}=g(a,b,({parse:e})=>v?d(h(a,c),a):e(w(a,c))),m=r.useMemo(()=>({...M,...P}),[]),f=r.useCallback((e,j)=>i(e,{...m,...j}),[i]),S=H();r.useLayoutEffect(()=>{o(h(a,d(Object.fromEntries([...S.entries()]),a)))},[S]);const y=r.useCallback(e=>{n({...m,...e})},[n]);return{setState:o,updateState:o,setUrl:f,updateUrl:f,urlState:p,state:p,reset:y,getState:k}}const M={replace:!0,scroll:!1};export{E as useUrlState};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import s from"react";import{useNavigate as b,useSearchParams as g}from"react-router-dom";import{parseSPObj as k}from"../../parseSPObj.mjs";import{useUrlStateBase as C}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as P,filterUnknownParamsClient as y,assignValue as H,filterUnknownParams as h}from"../../utils.mjs";function j(t,o){const r="defaultState"in t?t.defaultState:t,f="defaultState"in t?t.useHistory:o?.useHistory,d="defaultState"in t?{replace:t.replace,preventScrollReset:t.preventScrollReset}:{replace:o?.replace,preventScrollReset:o?.preventScrollReset},l=s.useMemo(()=>({...w,...d}),[]),u=b(),U=s.useMemo(()=>f?P:{replace:(e,a)=>u(e,{...l,...a}),push:(e,a)=>u(e,{...l,...a})},[u]),{state:
|
|
1
|
+
import s from"react";import{useNavigate as b,useSearchParams as g}from"react-router-dom";import{parseSPObj as k}from"../../parseSPObj.mjs";import{useUrlStateBase as C}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as P,filterUnknownParamsClient as y,assignValue as H,filterUnknownParams as h}from"../../utils.mjs";function j(t,o){const r="defaultState"in t?t.defaultState:t,f="defaultState"in t?t.useHistory:o?.useHistory,d="defaultState"in t?{replace:t.replace,preventScrollReset:t.preventScrollReset}:{replace:o?.replace,preventScrollReset:o?.preventScrollReset},l=s.useMemo(()=>({...w,...d}),[]),u=b(),U=s.useMemo(()=>f?P:{replace:(e,a)=>u(e,{...l,...a}),push:(e,a)=>u(e,{...l,...a})},[u]),[n]=g(),{state:c,updateState:p,updateUrl:i,getState:v,reset:S}=C(r,U,({parse:e})=>e(y(r,n.entries()))),m=s.useCallback((e,a)=>i(e,{...l,...a}),[i]);s.useEffect(()=>{p(H(r,h(r,k(Object.fromEntries([...n.entries()]),r))))},[n]);const R=s.useCallback(e=>{S({...l,...e})},[S]);return{setState:p,updateState:p,setUrl:m,updateUrl:m,urlState:c,state:c,reset:R,getState:v}}const w={replace:!0,preventScrollReset:!0};export{j as useUrlState};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import u from"react";import{TIMEOUT as
|
|
1
|
+
import u from"react";import{TIMEOUT as y}from"../constants/constants.mjs";import{useInsertionEffect as L}from"../useInsertionEffect.mjs";import{useSharedState as T}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as v}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as R}from"../utils.mjs";function b(t,a,l){const{parse:c,stringify:r}=v(t),{state:s,getState:o,setState:n}=T(t,()=>l?.({parse:c})||t),w=u.useRef(void 0);L(()=>{const e=()=>{const m=c(R(t,P()));n(m)};return window.addEventListener(U,e),()=>{window.removeEventListener(U,e),clearTimeout(w.current)}},[n]);const i=u.useRef([]),f=u.useCallback((e,m)=>{const d=typeof e=="function"?e(o()):{...o(),...e},h=r(d,C(t)),S=`${window.location.pathname}${h.length?"?":""}${h}${window.location.hash}`,g=`${window.location.pathname}${window.location.search}${window.location.hash}`;if(S===g)return;n(d);const{replace:k,...E}=m||{};i.current.push([k?"replace":"push",S,E]),clearTimeout(w.current),queueMicrotask(()=>{i.current.length&&(w.current=setTimeout(()=>{const p=i.current.at(-1);i.current=[],a[p[0]](p[1],p[2])},y))})},[a,r,o]),$=u.useCallback(e=>{n(t),f(t,e)},[f,n]);return{updateState:n,updateUrl:f,state:s,reset:$,getState:o}}function C(t){const a=Object.keys(t),l=window.location.search,c=new URLSearchParams(l),r=new URLSearchParams;for(const[s,o]of c)!a.includes(s)&&r.set(s,o);return r}const U="popstate";function P(){return typeof window<"u"&&window.location.search||""}export{b as useUrlStateBase};
|
package/dist/utils.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export type UnknownObj = object | {
|
|
|
22
22
|
[key: string]: unknown;
|
|
23
23
|
};
|
|
24
24
|
export declare const isEqual: (val1: unknown, val2: unknown) => boolean;
|
|
25
|
-
export declare function filterUnknownParamsClient<T extends object>(shape: T): string;
|
|
25
|
+
export declare function filterUnknownParamsClient<T extends object>(shape: T, params: string | URLSearchParams | Record<string, string> | object): string;
|
|
26
26
|
export declare function filterUnknownParams<T extends object>(shape: T, searchParams?: object): T;
|
|
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;
|
package/dist/utils.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const c=n=>{const t=typeof n,e=n===null,o=Array.isArray(n),r=n instanceof Date;return e&&"null"||r&&"date"||o&&"array"||!e&&!r&&!o&&t==="object"&&"object"||t},
|
|
1
|
+
const c=n=>{const t=typeof n,e=n===null,o=Array.isArray(n),r=n instanceof Date;return e&&"null"||r&&"date"||o&&"array"||!e&&!r&&!o&&t==="object"&&"object"||t},a=typeof window>"u",l=n=>new URLSearchParams(typeof n=="string"?u(n):n?.toString?.()||""),u=n=>n.split("?")?.[1]||n||"",f=(n,t)=>JSON.stringify(n)===JSON.stringify(t);function w(n,t){const e=new URLSearchParams,o=i(n,[...new URLSearchParams(t||"").entries()]);for(const[r,s]of o)e.set(r,s);return e.toString()}function p(n,t){return Object.fromEntries(i(n,Object.entries(t||{})))}function i(n,t){const e=Object.keys(n),o=[];for(let r=0;r<t.length;r++)e.includes(t[r]?.[0])&&o.push([t[r][0].replace(/\+/g," "),t[r][1]]);return o}function y(n,t){const e=Object.assign({},n);for(const[o]of Object.entries(n)){const r=o,s=t[r]!==void 0;e[r]=s?t[r]:n[r]}return e}const g={push:n=>{window&&window.history.pushState(null,"",n)},replace:n=>{window&&window.history.replaceState(null,"",n)}},S=/apple/i.test(!a&&navigator?.userAgent||"");export{y as assignValue,i as filterUnknown,p as filterUnknownParams,w as filterUnknownParamsClient,l as getParams,f as isEqual,a as isSSR,S as isSafari,g as routerHistory,c as typeOf};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "state-in-url",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.13",
|
|
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": {
|