Skip to content

Raw snippet alternative #12425

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 21 commits into from
Jul 17, 2024
Merged

Raw snippet alternative #12425

merged 21 commits into from
Jul 17, 2024

Conversation

Rich-Harris
Copy link
Member

Alternative to #12409 (but see #12409 (comment)):

snippet = createRawSnippet({
  render: (one, two) => `<p class="${one}">${two}</p>`,
  update: (p, one, two) => {
    $effect(() => p.className = one());
    $effect(() => p.textContent = two());
  }
});

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jul 12, 2024

🦋 Changeset detected

Latest commit: 7239cbb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@trueadm
Copy link
Contributor

trueadm commented Jul 16, 2024

As per the other thread: #12409 (comment)

@trueadm
Copy link
Contributor

trueadm commented Jul 16, 2024

Just needs docs then we’re good to go

@Rich-Harris
Copy link
Member Author

Oops, good catch

@dummdidumm
Copy link
Member

@bluwy does this version of the raw snippet API work for Astro, too?

@trueadm trueadm merged commit c287bd5 into main Jul 17, 2024
9 checks passed
@bluwy
Copy link
Member

bluwy commented Jul 17, 2024

@bluwy does this version of the raw snippet API work for Astro, too?

I updated https://github.com/withastro/astro/tree/svelte-5-slots-fix and it seems to work fine too. (I also made additional changes for snippet children to work)

@trueadm trueadm deleted the raw-snippet-alternative branch July 17, 2024 08:49
@sircumferencee
Copy link

Why not simply createSnippet?

@trueadm
Copy link
Contributor

trueadm commented Jul 17, 2024

Why not simply createSnippet?

This isn't something most people should be using, and instead should be using {#snippet} instead. createSnippet might confuse people into thinking that the two ways are equal.

@targetlucked69
Copy link

targetlucked69 commented Jul 20, 2024

@trueadm @Rich-Harris how can one use an existing component inside createRawSnippet? I tried something like

import { createRawSnippet, mount } from 'svelte';
import Counter from './lib/Counter.svelte'

export const counterSnippet = createRawSnippet(() => {
	return {
		render: () => `<h1>Count: 0</h1>`,
		setup: (node) => {
  		   mount(Counter, {
                       target: node
                    })
		}
	};
});

But this doesnt make sense because I have a different content when rendering. Or am I understanding this wrong?

@Rich-Harris
Copy link
Member Author

You can do this...

<script>
import { createRawSnippet, hydrate } from 'svelte';
import { render } from 'svelte/server';
import Child from './Child.svelte';
let { browser } = $props();
let count = $state(0);
const hello = createRawSnippet((count) => ({
render: () => `
<div>${browser ? '' : render(Child).body}</div>
`,
setup(target) {
hydrate(Child, { target })
}
}));
</script>
{@render hello()}

...though there's currently a bug with hydration (we don't reset hydration state after the snippet has been hydrated, causing the larger app to subsequently crash) that is fixed in #12512

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants