Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ export function create_client(app, target) {
to: {
params: current.params,
route: { id: current.route?.id ?? null },
url: new URL(location.href)
url: new URL(location.href),
scroll: scroll_state()
},
willUnload: false,
type: 'enter'
Expand Down Expand Up @@ -903,22 +904,29 @@ export function create_client(app, target) {
* type: import('types').NavigationType;
* intent?: import('./types').NavigationIntent;
* delta?: number;
* scroll?: {x: number, y: number};
* }} opts
*/
function before_navigate({ url, type, intent, delta }) {
function before_navigate({ url, type, intent, delta, scroll }) {
let should_block = false;

/** @type {import('types').Navigation} */
const navigation = {
from: {
params: current.params,
route: { id: current.route?.id ?? null },
url: current.url
url: current.url,
scroll: scroll_state()
},
to: {
params: intent?.params ?? null,
route: { id: intent?.route?.id ?? null },
url
url,
scroll: {
x: 0,
y: 0,
...scroll
}
},
willUnload: !intent,
type
Expand Down Expand Up @@ -973,7 +981,7 @@ export function create_client(app, target) {
blocked
}) {
const intent = get_navigation_intent(url, false);
const navigation = before_navigate({ url, type, delta, intent });
const navigation = before_navigate({ url, type, delta, intent, scroll: scroll ?? undefined });

if (!navigation) {
blocked();
Expand Down Expand Up @@ -1449,7 +1457,14 @@ export function create_client(app, target) {
from: {
params: current.params,
route: { id: current.route?.id ?? null },
url: current.url
url: current.url,
scroll: {
// @ts-expect-error
x: 0,
// @ts-expect-error
y: 0,
...scroll_positions[current_history_index]
}
},
to: null,
willUnload: true,
Expand Down Expand Up @@ -1644,6 +1659,8 @@ export function create_client(app, target) {

const delta = event.state[INDEX_KEY] - current_history_index;

console.log({ scroll });

await navigate({
url: new URL(location.href),
scroll,
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,14 @@ export interface NavigationTarget {
* The URL that is navigated to
*/
url: URL;

/**
* The scroll position of the target
*/
scroll: {
x: number;
y: number;
};
}

/**
Expand Down