Skip to content
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

Fix Svelte 5 slots change #11490

Merged
merged 4 commits into from
Jul 18, 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/nine-carpets-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/svelte': minor
---

Bumps Svelte 5 peer dependency to `^5.0.0-next.190` and support the latest slots/snippets API
32 changes: 9 additions & 23 deletions packages/integrations/svelte/client-v5.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { hydrate, mount, unmount } from 'svelte';
import { add_snippet_symbol } from 'svelte/internal/client';

// Allow a slot to be rendered as a snippet (dev validation only)
const tagSlotAsSnippet = import.meta.env.DEV ? add_snippet_symbol : (s) => s;
import { createRawSnippet, hydrate, mount, unmount } from 'svelte';

export default (element) => {
return async (Component, props, slotted, { client }) => {
Expand All @@ -11,11 +7,16 @@ export default (element) => {
let children = undefined;
let $$slots = undefined;
for (const [key, value] of Object.entries(slotted)) {
$$slots ??= {};
if (key === 'default') {
children = createSlotDefinition(key, value);
$$slots.default = true;
children = createRawSnippet(() => ({
render: () => `<astro-slot>${value}</astro-slot>`,
}));
} else {
$$slots ??= {};
$$slots[key] = createSlotDefinition(key, value);
$$slots[key] = createRawSnippet(() => ({
render: () => `<astro-slot name="${key}">${value}</astro-slot>`,
}));
}
}

Expand All @@ -33,18 +34,3 @@ export default (element) => {
element.addEventListener('astro:unmount', () => unmount(component), { once: true });
};
};

function createSlotDefinition(key, children) {
/**
* @param {Comment} $$anchor A comment node for slots in Svelte 5
*/
const fn = ($$anchor, _$$slotProps) => {
const parent = $$anchor.parentNode;
const el = document.createElement('div');
el.innerHTML = `<astro-slot${
key === 'default' ? '' : ` name="${key}"`
}>${children}</astro-slot>`;
parent.insertBefore(el.children[0], $$anchor);
};
return tagSlotAsSnippet(fn);
}
2 changes: 1 addition & 1 deletion packages/integrations/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"peerDependencies": {
"astro": "^4.0.0",
"svelte": "^4.0.0 || ^5.0.0-next.90",
"svelte": "^4.0.0 || ^5.0.0-next.190",
"typescript": "^5.3.3"
},
"engines": {
Expand Down
20 changes: 11 additions & 9 deletions packages/integrations/svelte/server-v5.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { add_snippet_symbol } from 'svelte/internal/server';
import { createRawSnippet } from 'svelte';
import { render } from 'svelte/server';

// Allow a slot to be rendered as a snippet (dev validation only)
const tagSlotAsSnippet = import.meta.env.DEV ? add_snippet_symbol : (s) => s;

function check(Component) {
// Svelte 5 generated components always accept these two props
const str = Component.toString();
Expand All @@ -21,22 +18,27 @@ async function renderToStaticMarkup(Component, props, slotted, metadata) {
let children = undefined;
let $$slots = undefined;
for (const [key, value] of Object.entries(slotted)) {
$$slots ??= {};
if (key === 'default') {
children = tagSlotAsSnippet(() => `<${tagName}>${value}</${tagName}>`);
$$slots.default = true;
children = createRawSnippet(() => ({
render: () => `<${tagName}>${value}</${tagName}>`,
}));
} else {
$$slots ??= {};
$$slots[key] = tagSlotAsSnippet(() => `<${tagName} name="${key}">${value}</${tagName}>`);
$$slots[key] = createRawSnippet(() => ({
render: () => `<${tagName} name="${key}">${value}</${tagName}>`,
}));
}
}

const { html } = render(Component, {
const result = render(Component, {
props: {
...props,
children,
$$slots,
},
});
return { html };
return { html: result.body };
}

export default {
Expand Down
Loading