-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
(preact-iso): Use pushState/ replaceState for useLocation().route method #413
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.
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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's what I'd suggest, in order to preserve the duck typing (as mentioned in previous comment): const UPDATE = (state, update) => {
/** @type {boolean|undefined} - History state update strategy */
let push, url;
if (!update || typeof update === 'string') {
url = update;
push = true;
}
else if (update.type === 'click') {
// user click
// @ts-ignore-next
const link = update.target.closest('a[href]');
if (!link || link.origin != location.origin) return state;
update.preventDefault();
url = link.pathname + link.search + link.hash;
push = true;
}
else if (update.type === 'popstate') {
// navigation
url = location.pathname + location.search + location.hash;
}
else {
// manual invocation: route(url) or route({ url, replace })
url = update.url || update;
push = !url.replace;
}
if (push === true) history.pushState(null, '', url);
else if (push === false) history.replaceState(null, '', url);
return url;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we allow passing a value that evaluates to false (undefined/ null) with route method? if (!update || typeof update === 'string') {
// ^ When the else {
// manual invocation: route(url) or route({ url, replace })
url = update.url || update;
// ^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated PR with code you suggested, adapting to eslint/ prettier rules There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @developit |
||
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
.