@@ -10,7 +10,15 @@ import mitt from 'mitt'; | ||
| path = "/", | ||
| searchPath = "", | ||
| static: staticLocation, | ||
| record, | ||
| } = {}) => { | ||
| let currentPath = path; | ||
| const history = [currentPath]; | ||
| let initialPath = path; | ||
| if (searchPath) { | ||
| // join with & if path contains search query, and ? otherwise | ||
| initialPath += path.split("?")[1] ? "&" : "?"; | ||
| initialPath += searchPath; | ||
| } | ||
| let [currentPath, currentSearch = ""] = initialPath.split("?"); | ||
| const history = [initialPath]; | ||
| const emitter = mitt(); | ||
@@ -27,3 +35,3 @@ | ||
| currentPath = path; | ||
| [currentPath, currentSearch = ""] = path.split("?"); | ||
| emitter.emit("navigate", path); | ||
@@ -44,2 +52,5 @@ }; | ||
| const useMemoryQuery = () => | ||
| useSyncExternalStore(subscribe, () => currentSearch); | ||
| function reset() { | ||
@@ -49,3 +60,3 @@ // clean history array with mutation to preserve link | ||
| navigateImplementation(path); | ||
| navigateImplementation(initialPath); | ||
| } | ||
@@ -55,2 +66,3 @@ | ||
| hook: useMemoryLocation, | ||
| searchHook: useMemoryQuery, | ||
| navigate, | ||
@@ -57,0 +69,0 @@ history: record ? history : undefined, |
+1
-1
| { | ||
| "name": "wouter", | ||
| "version": "3.5.1", | ||
| "version": "3.6.0", | ||
| "description": "Minimalist-friendly ~1.5KB router for React", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -1,2 +0,7 @@ | ||
| import { BaseLocationHook, Path } from "./location-hook.js"; | ||
| import { | ||
| BaseLocationHook, | ||
| BaseSearchHook, | ||
| Path, | ||
| SearchString, | ||
| } from "./location-hook.js"; | ||
@@ -8,3 +13,7 @@ type Navigate<S = any> = ( | ||
| type HookReturnValue = { hook: BaseLocationHook; navigate: Navigate }; | ||
| type HookReturnValue = { | ||
| hook: BaseLocationHook; | ||
| searchHook: BaseSearchHook; | ||
| navigate: Navigate; | ||
| }; | ||
| type StubHistory = { history: Path[]; reset: () => void }; | ||
@@ -14,2 +23,3 @@ | ||
| path?: Path; | ||
| searchPath?: SearchString; | ||
| static?: boolean; | ||
@@ -20,4 +30,5 @@ record?: false; | ||
| path?: Path; | ||
| searchPath?: SearchString; | ||
| static?: boolean; | ||
| record: true; | ||
| }): HookReturnValue & StubHistory; |
68122
0.82%831
2.59%