Skip to content

Commit

Permalink
Fix unmount Svelte slots
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Feb 15, 2023
1 parent 16cb5bc commit 5b3904d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curvy-snakes-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/svelte': patch
---

Fix unmounting slots passed to Svelte components
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let isNavOpen = false;
const toggleNav = () => (isNavOpen = !isNavOpen);
</script>

<button id="toggle" on:click={toggleNav}>
{#if isNavOpen}
<slot name="open" />
{:else}
<slot name="close" />
{/if}
</button>
1 change: 1 addition & 0 deletions packages/astro/e2e/fixtures/svelte-component/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="astro/client" />
10 changes: 10 additions & 0 deletions packages/astro/e2e/fixtures/svelte-component/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import Counter from '../components/Counter.svelte';
import SvelteComponent from '../components/SvelteComponent.svelte';
import ToggleSlots from '../components/ToggleSlots.svelte';
const someProps = {
count: 0,
Expand Down Expand Up @@ -33,5 +34,14 @@ const someProps = {
</Counter>

<SvelteComponent id="client-only" client:only="svelte" />

<ToggleSlots client:load>
<div slot="open">
open
</div>
<div slot="close">
close
</div>
</ToggleSlots>
</body>
</html>
12 changes: 12 additions & 0 deletions packages/astro/e2e/svelte-component.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect } from '@playwright/test';
import { prepareTestFactory } from './shared-component-tests.js';

const { test, createTests } = prepareTestFactory({ root: './fixtures/svelte-component/' });
Expand All @@ -23,3 +24,14 @@ test.describe('Svelte components in MDX files', () => {
pageSourceFilePath: './src/pages/mdx.mdx',
});
});

test.describe('Svelte components lifecycle', () => {
test('slot should unmount properly', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));

const toggle = page.locator('#toggle');
expect((await toggle.textContent()).trim()).toBe('close');
await toggle.click()
expect((await toggle.textContent()).trim()).toBe('open');
});
});
10 changes: 9 additions & 1 deletion packages/integrations/svelte/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export default (target) => {
};

function createSlotDefinition(key, children) {
let parent;
return [
() => ({
// mount
m(target) {
parent = target;
target.insertAdjacentHTML(
'beforeend',
`<astro-slot${key === 'default' ? '' : ` name="${key}"`}>${children}</astro-slot>`
Expand All @@ -37,7 +39,13 @@ function createSlotDefinition(key, children) {
// hydrate
l: noop,
// destroy
d: noop,
d() {
if (!parent) return;
const slot = parent.querySelector(
`astro-slot${key === 'default' ? ':not([name])' : `[name="${key}"]`}`
);
if (slot) slot.remove();
},
}),
noop,
noop,
Expand Down

0 comments on commit 5b3904d

Please sign in to comment.