+18
-13
@@ -25,17 +25,22 @@ import { useSyncExternalStore } from './react-deps.js'; | ||
| const navigate = (to, { state = null } = {}) => { | ||
| // calling `replaceState` allows us to set the history | ||
| // state without creating an extra entry | ||
| const navigate = (to, { state = null, replace = false } = {}) => { | ||
| const [hash, search] = to.replace(/^#?\/?/, "").split("?"); | ||
| history.replaceState( | ||
| state, | ||
| "", | ||
| // keep the current pathname, but replace query string and hash | ||
| location.pathname + | ||
| (search ? `?${search}` : location.search) + | ||
| // update location hash, this will cause `hashchange` event to fire | ||
| // normalise the value before updating, so it's always preceeded with "#/" | ||
| (location.hash = `#/${hash}`) | ||
| ); | ||
| const newRelativePath = | ||
| location.pathname + (search ? `?${search}` : location.search) + `#/${hash}`; | ||
| const oldURL = location.href; | ||
| const newURL = new URL(newRelativePath, location.origin).href; | ||
| if (replace) { | ||
| history.replaceState(state, "", newRelativePath); | ||
| } else { | ||
| history.pushState(state, "", newRelativePath); | ||
| } | ||
| const event = | ||
| typeof HashChangeEvent !== "undefined" | ||
| ? new HashChangeEvent("hashchange", { oldURL, newURL }) | ||
| : new Event("hashchange", { detail: { oldURL, newURL } }); | ||
| dispatchEvent(event); | ||
| }; | ||
@@ -42,0 +47,0 @@ |
+1
-1
| { | ||
| "name": "wouter", | ||
| "version": "3.6.0", | ||
| "version": "3.7.0", | ||
| "description": "Minimalist-friendly ~1.5KB router for React", | ||
@@ -5,0 +5,0 @@ "type": "module", |
+2
-2
@@ -48,3 +48,3 @@ <div align="center"> | ||
| **[React-three-fiber](https://github.com/react-spring/react-three-fiber)**, | ||
| **[Sunmao UI](https://sunmao-ui.com/)**, **[Million](https://millionjs.org/)** and many more. | ||
| **[Sunmao UI](https://sunmao-ui.com/)**, **[Million](https://million.dev/)** and many more. | ||
@@ -368,3 +368,3 @@ ## Table of Contents | ||
| ```jsx | ||
| import { useSearchParams } from 'wouter-search'; | ||
| import { useSearchParams } from 'wouter'; | ||
@@ -371,0 +371,0 @@ const [searchParams, setSearchParams] = useSearchParams(); |
| import { Path } from "./location-hook.js"; | ||
| export function navigate<S = any>(to: Path, options?: { state: S }): void; | ||
| export function navigate<S = any>( | ||
| to: Path, | ||
| options?: { state?: S; replace?: boolean } | ||
| ): void; | ||
@@ -5,0 +8,0 @@ export function useHashLocation(options?: { |
68229
0.16%836
0.6%