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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use alternative
  • Loading branch information
bluwy committed Jul 17, 2024
commit 3b53c638ccc48b532c77b9e9da9cfb7ab23b31d6
20 changes: 12 additions & 8 deletions packages/integrations/svelte/client-v5.js
Original file line number Diff line number Diff line change
@@ -4,17 +4,20 @@ export default (element) => {
return async (Component, props, slotted, { client }) => {
if (!element.hasAttribute('ssr')) return;

let children = undefined;
let $$slots = undefined;
for (const [key, value] of Object.entries(slotted)) {
$$slots ??= {};
$$slots[key] = createRawSnippet({
mount() {
const el = document.createElement('astro-slot');
if (key !== 'default') el.setAttribute('name', key);
el.innerHTML = value;
return el;
},
});
if (key === 'default') {
$$slots.default = true;
children = createRawSnippet(() => ({
render: () => `<astro-slot>${value}</astro-slot>`,
}));
} else {
$$slots[key] = createRawSnippet(() => ({
render: () => `<astro-slot name="${key}">${value}</astro-slot>`,
}));
}
}

const bootstrap = client !== 'only' ? hydrate : mount;
@@ -23,6 +26,7 @@ export default (element) => {
target: element,
props: {
...props,
children,
$$slots,
},
});
15 changes: 12 additions & 3 deletions packages/integrations/svelte/server-v5.js
Original file line number Diff line number Diff line change
@@ -15,17 +15,26 @@ function needsHydration(metadata) {
async function renderToStaticMarkup(Component, props, slotted, metadata) {
const tagName = needsHydration(metadata) ? 'astro-slot' : 'astro-static-slot';

let children = undefined;
let $$slots = undefined;
for (const [key, value] of Object.entries(slotted)) {
$$slots ??= {};
$$slots[key] = createRawSnippet({
render: () => `<${tagName}${key === 'default' ? '' : ` name="${key}"`}>${value}</${tagName}>`,
});
if (key === 'default') {
$$slots.default = true;
children = createRawSnippet(() => ({
render: () => `<${tagName}>${value}</${tagName}>`,
}));
} else {
$$slots[key] = createRawSnippet(() => ({
render: () => `<${tagName} name="${key}">${value}</${tagName}>`,
}));
}
}

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