Skip to content

Commit fde31d4

Browse files
committed
docs
1 parent b133c04 commit fde31d4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ We can tighten things up further by declaring a generic, so that `data` and `row
256256
</script>
257257
```
258258

259+
## Creating snippets programmatically
260+
261+
In advanced scenarios, you may need to create a snippet programmatically. For this, you can use [`createRawSnippet`](/docs/imports#svelte-createrawsnippet)
262+
259263
## Snippets and slots
260264

261265
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.

sites/svelte-5-preview/src/routes/docs/content/01-api/05-imports.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,37 @@ To prevent something from being treated as an `$effect`/`$derived` dependency, u
9393
</script>
9494
```
9595

96+
### `createRawSnippet`
97+
98+
An advanced API designed for people building frameworks that integrate with Svelte, `createRawSnippet` allows you to create [snippets](/docs/snippets) programmatically for use with `{@render ...}` tags:
99+
100+
```js
101+
import { createRawSnippet } from 'svelte';
102+
103+
const greet = createRawSnippet((name) => {
104+
return {
105+
render: () => `
106+
<h1>Hello ${name()}!</h1>
107+
`,
108+
setup: (h1) => {
109+
$effect(() => {
110+
h1.textContent = `Hello ${name()}!`;
111+
});
112+
}
113+
};
114+
});
115+
```
116+
117+
The `render` function is called during server-side rendering, or during `mount` (but not during `hydrate`, because it already ran on the server), and must return HTML representing a single element.
118+
119+
The `setup` function is called during `mount` or `hydrate` with that same element as its sole argument. It is responsible for ensuring that the DOM is updated when the arguments change their value — in this example, when `name` changes:
120+
121+
```svelte
122+
{@render greet(name)}
123+
```
124+
125+
If the snippet is fully static, you can omit the `setup` function.
126+
96127
## `svelte/reactivity`
97128

98129
Svelte provides reactive `SvelteMap`, `SvelteSet`, `SvelteDate` and `SvelteURL` classes. These can be imported from `svelte/reactivity` and used just like their native counterparts. [Demo:](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE32QwUrEMBBAf2XMpQrb9t7tFrx7UjxZYWM6NYFkEpJJ16X03yWK9OQeZ3iPecwqZmMxie5tFSQdik48hiAOgq-hDGlByygOIvkcVdn0SUUTeBhpZOOCjwwrvPxgr89PsMEcvYPqV2wjSsVmMXytjiMVR3lKDDlaOAHhZVfvK80cUte2-CVdsNgo79ogWVcPx5H6dj9M_V1dg9KSPjEBe2CNCZumgboeRuoNhczwYWjqFmkzntYcbROiZ6-83f5HtE9c3nADKUF_yEi9jnvQxVgLOUySEc464nwGSRMsRiEsGJO8mVeEbRAH4fxkZoOT6Dhm3N63b9_bGfOlAQAA)

0 commit comments

Comments
 (0)