Skip to content

Commit

Permalink
feat(router): configuration of scroll behavior for Link and useRouter (
Browse files Browse the repository at this point in the history
…#1231)

follow up for:
#1098 (comment)

cc @wesbos thanks for this call out

---------

Co-authored-by: Tyler <[email protected]>
Co-authored-by: Daishi Kato <[email protected]>
  • Loading branch information
3 people authored Feb 17, 2025
1 parent ff9c9ef commit 51961d0
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions packages/waku/src/router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,18 @@ export function useRouter_UNSTABLE() {
}
const { route, changeRoute, prefetchRoute } = router;
const push = useCallback(
(to: InferredPaths) => {
(
to: InferredPaths,
options?: {
/**
* indicates if the link should scroll or not on navigation
* - `true`: always scroll
* - `false`: never scroll
* - `undefined`: scroll on path change (not on searchParams change)
*/
scroll?: boolean;
},
) => {
const url = new URL(to, window.location.href);
const newPath = url.pathname !== window.location.pathname;
window.history.pushState(
Expand All @@ -125,16 +136,31 @@ export function useRouter_UNSTABLE() {
'',
url,
);
changeRoute(parseRoute(url), { shouldScroll: newPath });
changeRoute(parseRoute(url), {
shouldScroll: options?.scroll ?? newPath,
});
},
[changeRoute],
);
const replace = useCallback(
(to: InferredPaths) => {
(
to: InferredPaths,
options?: {
/**
* indicates if the link should scroll or not on navigation
* - `true`: always scroll
* - `false`: never scroll
* - `undefined`: scroll on path change (not on searchParams change)
*/
scroll?: boolean;
},
) => {
const url = new URL(to, window.location.href);
const newPath = url.pathname !== window.location.pathname;
window.history.replaceState(window.history.state, '', url);
changeRoute(parseRoute(url), { shouldScroll: newPath });
changeRoute(parseRoute(url), {
shouldScroll: options?.scroll ?? newPath,
});
},
[changeRoute],
);
Expand Down Expand Up @@ -172,6 +198,13 @@ export type LinkProps = {
to: InferredPaths;
pending?: ReactNode;
notPending?: ReactNode;
/**
* indicates if the link should scroll or not on navigation
* - `true`: always scroll
* - `false`: never scroll
* - `undefined`: scroll on path change (not on searchParams change)
*/
scroll?: boolean;
children: ReactNode;
unstable_prefetchOnEnter?: boolean;
unstable_prefetchOnView?: boolean;
Expand All @@ -186,6 +219,7 @@ export function Link({
unstable_prefetchOnEnter,
unstable_prefetchOnView,
unstable_startTransition,
scroll,
...props
}: LinkProps): ReactElement {
const router = useContext(RouterContext);
Expand Down Expand Up @@ -233,15 +267,16 @@ export function Link({
const route = parseRoute(url);
prefetchRoute(route);
(unstable_startTransition || startTransition)(() => {
const newPath = url.pathname !== window.location.pathname;
window.history.pushState(
{
...window.history.state,
waku_new_path: url.pathname !== window.location.pathname,
waku_new_path: newPath,
},
'',
url,
);
changeRoute(route, { shouldScroll: true });
changeRoute(route, { shouldScroll: scroll ?? newPath });
});
}
props.onClick?.(event);
Expand Down

0 comments on commit 51961d0

Please sign in to comment.