Skip to content

Commit 8aed27d

Browse files
authored
fix: allow mixing slots and snippets in custom elements mode (#12929)
We need to allow mixing those within custom element components because in a future version of Svelte where we remove the Svelte-version of slots, we'll preserve slots as-is, and people should use those within their components. At the same time they should be able to make use of snippets for reusable blocks of code. Also document that you should continue using slots within custom elements. closes #12892
1 parent 212b602 commit 8aed27d

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

.changeset/old-planets-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: allow mixing slots and snippets in custom elements mode

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,10 @@ export function analyze_component(root, source, options) {
649649
);
650650
}
651651

652-
if (analysis.uses_render_tags && (analysis.uses_slots || analysis.slot_names.size > 0)) {
652+
if (
653+
analysis.uses_render_tags &&
654+
(analysis.uses_slots || (!analysis.custom_element && analysis.slot_names.size > 0))
655+
) {
653656
const pos = analysis.slot_names.values().next().value ?? analysis.source.indexOf('$$slot');
654657
e.slot_snippet_conflict(pos);
655658
}

packages/svelte/src/compiler/phases/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export interface ComponentAnalysis extends Analysis {
6363
event_directive_node: OnDirective | null;
6464
/** true if uses event attributes (onclick) on a DOM element */
6565
uses_event_attributes: boolean;
66+
/**
67+
* Contains the content of `<svelte:options customElement={...} />`,
68+
* or if not present a boolean which corresponds to the compiler option value
69+
*/
6670
custom_element: boolean | SvelteOptions['customElement'];
6771
/** If `true`, should append styles through JavaScript */
6872
inject_styles: boolean;

sites/svelte-5-preview/src/routes/docs/content/01-api/03-snippets.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,5 @@ In advanced scenarios, you may need to create a snippet programmatically. For th
265265
In Svelte 4, content can be passed to components using [slots](https://svelte.dev/docs/special-elements#slot). Snippets are more powerful and flexible, and as such slots are deprecated in Svelte 5.
266266

267267
They continue to work, however, and you can mix and match snippets and slots in your components.
268+
269+
When using custom elements, you should still use `<slot />` like before. In a future version, when Svelte removes its internal version of slots, it will leave those slots as-is, i.e. output a regular DOM tag instead of transforming it.

sites/svelte-5-preview/src/routes/docs/content/02-examples/04-old-vs-new.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,5 @@ Combined with event attributes, this reduces the number of concepts to learn —
293293

294294
- [Before](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACn2PzYrCQBCEX6XpSxTE3GeTgPoYzh5i0sHB-WOmIywh776ZjCgieOsqqvrrnnBQmiKK84S2NYQCD97jDvnPJxHvpJkWHd0YuuRUsQvKcyOtZGW8CwzHkdlZGIIzUOzLLPe5WvxIW5Wvjq0eaWdFp1V3q6fNFuoGWk2BN8UpedQDXwkua7LYzqCJhQ_Or1TJaxGm5MxpfV7ZLGca16tBUY-Cw0jz7vlVjnx97PJ-2MqqonYMCVTLJWoI7q0eSSKUTSLnzjJ-sn_nfxmfF-FdAQAA)
295295
- [After](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACo2PzW6DMBCEX2XlVgIkBHcKUdo-RumBwqJYNbZlL5Eqy-9e_9DkkEtv3vHMzn6OrVygZd2HY3LakHXsVWtWM_rRcbBXFIRhtmo3c1R6Oxuu6TTKkfimlSF424mUhNWoDYqmzWOTo8XLKPv2npH94VZyFnz-HlxZwXCCSaChsniPGi5AF4SvZCwqn7rck5VcaySYL1wsBmWpjdKVj58jpWXgopQU1x52H_tz5ylwbGrhK8eFdWR29PUNO1v-Sy7CHe52SQ1N08RqCx4GeE7PsnpAz0Tg_twH2TmsWNDcwcZQuiFcJ7HjyKqEkLMh8Ajx6X8BPkQdmscBAAA=)
296+
297+
> When using custom elements, you should still use `<slot />` like before. In a future version, when Svelte removes its internal version of slots, it will leave those slots as-is, i.e. output a regular DOM tag instead of transforming it.

0 commit comments

Comments
 (0)