diff --git a/packages/void/src/pages/client.ts b/packages/void/src/pages/client.ts index a0ec4534c..94eaee1cd 100644 --- a/packages/void/src/pages/client.ts +++ b/packages/void/src/pages/client.ts @@ -282,7 +282,10 @@ export async function submitAction( options: VisitOptions, ): Promise { try { - const pageData = await router.visit(url, options); + const pageData = await router.visit(url, { + preserveScroll: true, + ...options, + }); if ( typeof pageData === 'object' && pageData !== null && diff --git a/packages/void/test/unit/action-result.test.ts b/packages/void/test/unit/action-result.test.ts index 5b08eab4f..3acb0d539 100644 --- a/packages/void/test/unit/action-result.test.ts +++ b/packages/void/test/unit/action-result.test.ts @@ -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', () => { @@ -138,6 +169,7 @@ describe('action clients', () => { method: 'POST', data: undefined, _resolveOnShell: true, + preserveScroll: true, }); }); @@ -169,6 +201,7 @@ describe('action clients', () => { expect(firstVisit).toHaveBeenCalledWith('/__action-test/static', { method: 'POST', data: undefined, + preserveScroll: true, }); }); @@ -186,6 +219,7 @@ describe('action clients', () => { expect(visit).toHaveBeenCalledWith('/__action-test/users/a%2Fb%3Fc?save', { method: 'PATCH', data: { name: 'Alice' }, + preserveScroll: true, }); });