Skip to content

fix: better children snippet / default slot interop #13734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2024
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: 5 additions & 0 deletions .changeset/tidy-spies-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: better children snippet / default slot interop
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ export function build_component(node, component_name, context, anchor = context.
push_prop(b.prop('init', child.expression, child.expression));

// Interop: allows people to pass snippets when component still uses slots
serialized_slots.push(b.init(child.expression.name, b.true));
serialized_slots.push(
b.init(child.expression.name === 'children' ? 'default' : child.expression.name, b.true)
);

continue;
}
Expand Down Expand Up @@ -273,7 +275,14 @@ export function build_component(node, component_name, context, anchor = context.
);

if (slot_name === 'default' && !has_children_prop) {
if (lets.length === 0 && children.default.every((node) => node.type !== 'SvelteFragment')) {
if (
lets.length === 0 &&
children.default.every(
(node) =>
node.type !== 'SvelteFragment' ||
!node.attributes.some((attr) => attr.type === 'LetDirective')
)
) {
// create `children` prop...
push_prop(
b.init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ export function build_inline_component(node, expression, context) {
push_prop(b.prop('init', child.expression, child.expression));

// Interop: allows people to pass snippets when component still uses slots
serialized_slots.push(b.init(child.expression.name, b.true));
serialized_slots.push(
b.init(child.expression.name === 'children' ? 'default' : child.expression.name, b.true)
);

continue;
}
Expand Down Expand Up @@ -200,7 +202,11 @@ export function build_inline_component(node, expression, context) {
if (slot_name === 'default' && !has_children_prop) {
if (
lets.default.length === 0 &&
children.default.every((node) => node.type !== 'SvelteFragment')
children.default.every(
(node) =>
node.type !== 'SvelteFragment' ||
!node.attributes.some((attr) => attr.type === 'LetDirective')
)
) {
// create `children` prop...
push_prop(b.prop('init', b.id('children'), slot_fn));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `<p>bar</p>`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let { children } = $props();
</script>

{@render children()}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import Child from './child.svelte';
</script>

<Child>
<svelte:fragment>
<p>bar</p>
</svelte:fragment>
</Child>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from '../../test';

export default test({
html: `<p>Default</p> <p>Named foo</p>`
html: `<p>Default foo</p> <p>Named bar</p>`
});
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<p><slot /></p>
<p><slot name="named" foo="foo" /></p>
<p><slot foo="foo" /></p>
<p><slot name="named" bar="bar" /></p>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
</script>

<Child>
Default
{#snippet named({ foo })}
Named {foo}
{#snippet children({ foo })}
Default {foo}
{/snippet}
{#snippet named({ bar })}
Named {bar}
{/snippet}
</Child>