Skip to content
Merged
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
5 changes: 4 additions & 1 deletion packages/void/src/pages/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ export async function submitAction(
options: VisitOptions,
): Promise<ActionResult> {
try {
const pageData = await router.visit(url, options);
const pageData = await router.visit(url, {
preserveScroll: true,
...options,
});
if (
typeof pageData === 'object' &&
pageData !== null &&
Expand Down
34 changes: 34 additions & 0 deletions packages/void/test/unit/action-result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,37 @@ describe('action result handling', () => {
expect(isAbortError(new DOMException('Navigation aborted', 'AbortError'))).toBe(true);
expect(isAbortError(new Error('Boom'))).toBe(false);
});

it('preserves scroll by default when submitting an action', async () => {
const visit = vi.fn(async () => ({ props: {} }));
const router = routerWithVisit(visit);

await submitAction(router, '/users', {
method: 'POST',
data: { name: 'Alice' },
});

expect(visit).toHaveBeenCalledWith('/users', {
preserveScroll: true,
method: 'POST',
data: { name: 'Alice' },
});
});

it('allows explicitly disabling scroll preservation', async () => {
const visit = vi.fn(async () => ({ props: {} }));
const router = routerWithVisit(visit);

await submitAction(router, '/users', {
method: 'POST',
preserveScroll: false,
});

expect(visit).toHaveBeenCalledWith('/users', {
preserveScroll: false,
method: 'POST',
});
});
});

describe('action clients', () => {
Expand All @@ -138,6 +169,7 @@ describe('action clients', () => {
method: 'POST',
data: undefined,
_resolveOnShell: true,
preserveScroll: true,
});
});

Expand Down Expand Up @@ -169,6 +201,7 @@ describe('action clients', () => {
expect(firstVisit).toHaveBeenCalledWith('/__action-test/static', {
method: 'POST',
data: undefined,
preserveScroll: true,
});
});

Expand All @@ -186,6 +219,7 @@ describe('action clients', () => {
expect(visit).toHaveBeenCalledWith('/__action-test/users/a%2Fb%3Fc?save', {
method: 'PATCH',
data: { name: 'Alice' },
preserveScroll: true,
});
});

Expand Down