-
-
Notifications
You must be signed in to change notification settings - Fork 107
(preact-iso): Use pushState/ replaceState for useLocation().route method #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
56f83c8
43214d3
0faacea
25fd059
7337f1c
1f5654b
d772def
81f18f7
3d0d9f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,34 @@ | ||
| import { h, createContext, cloneElement } from 'preact'; | ||
| import { useContext, useMemo, useReducer, useEffect, useLayoutEffect, useRef } from 'preact/hooks'; | ||
|
|
||
| const UPDATE = (state, url, push) => { | ||
| if (url && url.type === 'click') { | ||
| /** | ||
| * @param {string} state | ||
| * @param {MouseEvent|PopStateEvent|Object|string} url | ||
| * @return {string} | ||
| */ | ||
| const UPDATE = (state, url) => { | ||
| /** @type {boolean|undefined} - History state update strategy */ | ||
| let push = undefined | ||
|
|
||
| // user click | ||
| if (url instanceof MouseEvent) { | ||
piotr-cz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // @ts-ignore-next | ||
| const link = url.target.closest('a[href]'); | ||
| if (!link || link.origin != location.origin) return state; | ||
|
|
||
| url.preventDefault(); | ||
| push = true; | ||
| url = link.href.replace(location.origin, ''); | ||
| } else if (typeof url !== 'string') { | ||
| // navigation | ||
| } else if (url instanceof PopStateEvent) { | ||
| url = location.pathname + location.search; | ||
| // manual invocation: route({ path, replace }) | ||
| } else if (typeof url === 'object') { | ||
|
||
| url = url.url; | ||
| push = !url.replace; | ||
| // manual invocation: route(path) | ||
| } else { | ||
| push = true; | ||
| } | ||
|
|
||
| if (push === true) history.pushState(null, '', url); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we can use
(url, replace)here? That would matchpreact-router.