1
1
import mitt from 'mitt'
2
- import { useEffect , useState } from 'react'
2
+ import {
3
+ createContext ,
4
+ createElement ,
5
+ useContext ,
6
+ useEffect ,
7
+ useMemo ,
8
+ useState ,
9
+ type ReactNode
10
+ } from 'react'
3
11
import { renderQueryString } from '../url-encoding'
4
12
import { createAdapterProvider } from './lib/context'
5
13
import type { AdapterOptions } from './lib/defs'
6
14
import { patchHistory , type SearchParamsSyncEmitter } from './lib/patch-history'
7
15
8
16
const emitter : SearchParamsSyncEmitter = mitt ( )
9
17
10
- function updateUrl ( search : URLSearchParams , options : AdapterOptions ) {
11
- const url = new URL ( location . href )
12
- url . search = renderQueryString ( search )
13
- const method =
14
- options . history === 'push' ? history . pushState : history . replaceState
15
- method . call ( history , history . state , '' , url )
16
- emitter . emit ( 'update' , search )
18
+ function generateUpdateUrlFn ( reloadPageOnShallowFalseUpdates : boolean ) {
19
+ return function updateUrl ( search : URLSearchParams , options : AdapterOptions ) {
20
+ const url = new URL ( location . href )
21
+ url . search = renderQueryString ( search )
22
+ if ( reloadPageOnShallowFalseUpdates && options . shallow === false ) {
23
+ const method =
24
+ options . history === 'push' ? location . assign : location . replace
25
+ method . call ( location , url )
26
+ } else {
27
+ const method =
28
+ options . history === 'push' ? history . pushState : history . replaceState
29
+ method . call ( history , history . state , '' , url )
30
+ }
31
+ emitter . emit ( 'update' , search )
32
+ }
17
33
}
18
34
35
+ const NuqsReactAdapterContext = createContext ( {
36
+ reloadPageOnShallowFalseUpdates : false
37
+ } )
38
+
19
39
function useNuqsReactAdapter ( ) {
40
+ const { reloadPageOnShallowFalseUpdates } = useContext (
41
+ NuqsReactAdapterContext
42
+ )
20
43
const [ searchParams , setSearchParams ] = useState ( ( ) => {
21
44
if ( typeof location === 'undefined' ) {
22
45
return new URLSearchParams ( )
@@ -36,13 +59,31 @@ function useNuqsReactAdapter() {
36
59
window . removeEventListener ( 'popstate' , onPopState )
37
60
}
38
61
} , [ ] )
62
+ const updateUrl = useMemo (
63
+ ( ) => generateUpdateUrlFn ( reloadPageOnShallowFalseUpdates ) ,
64
+ [ reloadPageOnShallowFalseUpdates ]
65
+ )
39
66
return {
40
67
searchParams,
41
68
updateUrl
42
69
}
43
70
}
44
71
45
- export const NuqsAdapter = createAdapterProvider ( useNuqsReactAdapter )
72
+ const NuqsReactAdapter = createAdapterProvider ( useNuqsReactAdapter )
73
+
74
+ export function NuqsAdapter ( {
75
+ children,
76
+ reloadPageOnShallowFalseUpdates = false
77
+ } : {
78
+ children : ReactNode
79
+ reloadPageOnShallowFalseUpdates ?: boolean
80
+ } ) {
81
+ return createElement (
82
+ NuqsReactAdapterContext . Provider ,
83
+ { value : { reloadPageOnShallowFalseUpdates } } ,
84
+ createElement ( NuqsReactAdapter , null , children )
85
+ )
86
+ }
46
87
47
88
/**
48
89
* Opt-in to syncing shallow updates of the URL with the useOptimisticSearchParams hook.
0 commit comments