-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Raw snippet alternative #12425
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
116de3e
feat: add createRawSnippet API
trueadm 2b41c4b
handle missing hydrate function, improve types
Rich-Harris 263ef5f
fix
Rich-Harris 5f77f80
tweak types
Rich-Harris 4e7279a
beef up test
Rich-Harris 12e5fe9
build
trueadm 011cef6
types
Rich-Harris d2e6232
Merge branch 'raw-snippet' of github.com:sveltejs/svelte into raw-sni…
Rich-Harris 9b89700
oops this was temporary
Rich-Harris 806f7a7
typo
Rich-Harris 08d2350
regenerate types
Rich-Harris 442334f
make mount/render optional, error if missing
Rich-Harris e361a20
move code to new module
Rich-Harris 40d1191
test hydration
Rich-Harris 1e4c6a2
simpler createRawSnippet API
Rich-Harris 62300f7
regenerate types
Rich-Harris 1775b2c
merge main
Rich-Harris b133c04
change signature
Rich-Harris fde31d4
docs
Rich-Harris 92cb9f8
h1 -> node
Rich-Harris 7239cbb
allow `setup` to return a teardown function
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
feat: add createRawSnippet API |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** @import { Snippet } from 'svelte' */ | ||
/** @import { Payload } from '#server' */ | ||
/** @import { Getters } from '#shared' */ | ||
import { add_snippet_symbol } from '../../shared/validate.js'; | ||
|
||
/** | ||
* Create a snippet programmatically | ||
* @template {unknown[]} Params | ||
* @param {(...params: Getters<Params>) => { | ||
* render: () => string | ||
* setup?: (element: Element) => void | ||
* }} fn | ||
* @returns {Snippet<Params>} | ||
*/ | ||
export function createRawSnippet(fn) { | ||
return add_snippet_symbol((/** @type {Payload} */ payload, /** @type {Params} */ ...args) => { | ||
var getters = /** @type {Getters<Params>} */ (args.map((value) => () => value)); | ||
payload.out += fn(...getters) | ||
.render() | ||
.trim(); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/svelte/tests/hydration/samples/snippet-raw-hydrate/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
snapshot(target) { | ||
return { | ||
p: target.querySelector('p') | ||
}; | ||
} | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/svelte/tests/hydration/samples/snippet-raw-hydrate/_expected.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!--[--><p>hydrated</p><!--]--> |
14 changes: 14 additions & 0 deletions
14
packages/svelte/tests/hydration/samples/snippet-raw-hydrate/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script> | ||
import { createRawSnippet } from 'svelte'; | ||
|
||
const snippet = createRawSnippet(() => ({ | ||
render: () => ` | ||
<p>rendered</p> | ||
`, | ||
setup(p) { | ||
p.textContent = 'hydrated'; | ||
} | ||
})); | ||
</script> | ||
|
||
{@render snippet()} |
11 changes: 11 additions & 0 deletions
11
packages/svelte/tests/runtime-runes/samples/snippet-raw-teardown/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
test({ target, assert, logs }) { | ||
const button = target.querySelector('button'); | ||
|
||
flushSync(() => button?.click()); | ||
assert.deepEqual(logs, ['tearing down']); | ||
} | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/svelte/tests/runtime-runes/samples/snippet-raw-teardown/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<script> | ||
import { createRawSnippet } from 'svelte'; | ||
let show = $state(true); | ||
const snippet = createRawSnippet(() => ({ | ||
render: () => `<hr>`, | ||
setup(p) { | ||
return () => console.log('tearing down') | ||
} | ||
})); | ||
</script> | ||
|
||
<button onclick={() => show = !show}>click</button> | ||
|
||
{#if show} | ||
{@render snippet()} | ||
{/if} |
17 changes: 17 additions & 0 deletions
17
packages/svelte/tests/runtime-runes/samples/snippet-raw/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
compileOptions: { | ||
dev: true // Render in dev mode to check that the validation error is not thrown | ||
}, | ||
|
||
html: `<button>click</button><p>clicks: 0</p>`, | ||
|
||
test({ target, assert }) { | ||
const button = target.querySelector('button'); | ||
|
||
flushSync(() => button?.click()); | ||
assert.htmlEqual(target.innerHTML, `<button>click</button><p>clicks: 1</p>`); | ||
} | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/svelte/tests/runtime-runes/samples/snippet-raw/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script> | ||
import { createRawSnippet } from 'svelte'; | ||
|
||
let count = $state(0); | ||
|
||
const hello = createRawSnippet((count) => ({ | ||
render: () => ` | ||
<p>clicks: ${count()}</p> | ||
`, | ||
setup(p) { | ||
$effect(() => { | ||
p.textContent = `clicks: ${count()}` | ||
}); | ||
} | ||
})); | ||
</script> | ||
|
||
<button onclick={() => count += 1}>click</button> | ||
|
||
{@render hello(count)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.