Skip to content

Commit 309ae8d

Browse files
authored
feat: typed params prop for page/layout components (#13999)
1 parent 3806a8e commit 309ae8d

File tree

7 files changed

+41
-9
lines changed

7 files changed

+41
-9
lines changed

.changeset/long-bars-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: typed `params` prop for page/layout components

packages/kit/src/core/sync/write_root.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@ export function write_root(manifest_data, output) {
3636
isSvelte5Plus()
3737
? dedent`{@const Pyramid_${l} = constructors[${l}]}
3838
<!-- svelte-ignore binding_property_non_reactive -->
39-
<Pyramid_${l} bind:this={components[${l}]} data={data_${l}} {form}>
39+
<Pyramid_${l} bind:this={components[${l}]} data={data_${l}} {form} params={page.params}>
4040
${pyramid}
4141
</Pyramid_${l}>`
42-
: dedent`<svelte:component this={constructors[${l}]} bind:this={components[${l}]} data={data_${l}}>
42+
: dedent`<svelte:component this={constructors[${l}]} bind:this={components[${l}]} data={data_${l}} params={page.params}>
4343
${pyramid}
4444
</svelte:component>`
4545
}
46-
46+
4747
{:else}
4848
${
4949
isSvelte5Plus()
5050
? dedent`
5151
{@const Pyramid_${l} = constructors[${l}]}
5252
<!-- svelte-ignore binding_property_non_reactive -->
53-
<Pyramid_${l} bind:this={components[${l}]} data={data_${l}} {form} />
53+
<Pyramid_${l} bind:this={components[${l}]} data={data_${l}} {form} params={page.params} />
5454
`
55-
: dedent`<svelte:component this={constructors[${l}]} bind:this={components[${l}]} data={data_${l}} {form} />`
55+
: dedent`<svelte:component this={constructors[${l}]} bind:this={components[${l}]} data={data_${l}} {form} params={page.params} />`
5656
}
57-
57+
5858
{/if}
5959
`;
6060
}

packages/kit/src/core/sync/write_types/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,11 @@ function update_types(config, routes, route, to_delete = new Set()) {
272272
}
273273

274274
if (route.leaf.server) {
275-
exports.push('export type PageProps = { data: PageData; form: ActionData }');
275+
exports.push(
276+
'export type PageProps = { params: RouteParams; data: PageData; form: ActionData }'
277+
);
276278
} else {
277-
exports.push('export type PageProps = { data: PageData }');
279+
exports.push('export type PageProps = { params: RouteParams; data: PageData }');
278280
}
279281
}
280282

@@ -341,7 +343,7 @@ function update_types(config, routes, route, to_delete = new Set()) {
341343
if (proxies.universal?.modified) to_delete.delete(proxies.universal.file_name);
342344

343345
exports.push(
344-
'export type LayoutProps = { data: LayoutData; children: import("svelte").Snippet }'
346+
'export type LayoutProps = { params: LayoutParams; data: LayoutData; children: import("svelte").Snippet }'
345347
);
346348
}
347349

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
let { children } = $props();
3+
</script>
4+
5+
<a href="/params-prop/123">123</a>
6+
<a href="/params-prop/456">456</a>
7+
8+
{@render children()}

packages/kit/test/apps/basics/src/routes/params-prop/+page.svelte

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let { params } = $props();
3+
</script>
4+
5+
<p>x: {params.x}</p>

packages/kit/test/apps/basics/test/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,3 +1556,15 @@ test.describe('getRequestEvent', () => {
15561556
expect(await page.textContent('h1')).toBe('Crashing now (500 hello from hooks.server.js)');
15571557
});
15581558
});
1559+
1560+
test.describe('params prop', () => {
1561+
test('params prop is passed to the page', async ({ page, clicknav }) => {
1562+
await page.goto('/params-prop');
1563+
1564+
await clicknav('[href="/params-prop/123"]');
1565+
await expect(page.locator('p')).toHaveText('x: 123');
1566+
1567+
await clicknav('[href="/params-prop/456"]');
1568+
await expect(page.locator('p')).toHaveText('x: 456');
1569+
});
1570+
});

0 commit comments

Comments
 (0)