@@ -8,21 +8,24 @@ import { useSyncExternalStore } from "./react-deps.js";
88export const memoryLocation = ( {
99 path = "/" ,
1010 searchPath = "" ,
11+ state = null ,
1112 static : staticLocation ,
1213 record,
1314} = { } ) => {
1415 let initialPath = path ;
16+ const initialState = state ;
1517 if ( searchPath ) {
1618 // join with & if path contains search query, and ? otherwise
1719 initialPath += path . split ( "?" ) [ 1 ] ? "&" : "?" ;
1820 initialPath += searchPath ;
1921 }
2022
2123 let [ currentPath , currentSearch = "" ] = initialPath . split ( "?" ) ;
24+ let currentState = initialState ;
2225 const history = [ initialPath ] ;
2326 const emitter = mitt ( ) ;
2427
25- const navigateImplementation = ( path , { replace = false } = { } ) => {
28+ const navigateImplementation = ( path , { replace = false , state = null } = { } ) => {
2629 if ( record ) {
2730 if ( replace ) {
2831 history . splice ( history . length - 1 , 1 , path ) ;
@@ -32,6 +35,7 @@ export const memoryLocation = ({
3235 }
3336
3437 [ currentPath , currentSearch = "" ] = path . split ( "?" ) ;
38+ currentState = state ;
3539 emitter . emit ( "navigate" , path ) ;
3640 } ;
3741
@@ -56,15 +60,21 @@ export const memoryLocation = ({
5660 function reset ( ) {
5761 // clean history array with mutation to preserve link
5862 history . splice ( 0 , history . length ) ;
59-
60- navigateImplementation ( initialPath ) ;
63+ navigateImplementation ( initialPath , { state : initialState } ) ;
6164 }
6265
63- return {
66+ const memoryLocationResult = {
6467 hook : useMemoryLocation ,
6568 searchHook : useMemoryQuery ,
6669 navigate,
6770 history : record ? history : undefined ,
6871 reset : record ? reset : undefined ,
6972 } ;
73+
74+ Object . defineProperty ( memoryLocationResult , "state" , {
75+ enumerable : true ,
76+ get : ( ) => currentState ,
77+ } ) ;
78+
79+ return memoryLocationResult ;
7080} ;
0 commit comments