mirror of https://github.com/sveltejs/svelte
parent
d9569d052e
commit
dfa5617419
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
feat: add createRawSnippet API
|
||||
@ -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: `<div><div>0</div></div><button>+</button>`,
|
||||
|
||||
test({ assert, target }) {
|
||||
const [b1] = target.querySelectorAll('button');
|
||||
|
||||
b1?.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(target.innerHTML, `<div><div>1</div></div><button>+</button>`);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,32 @@
|
||||
<script>
|
||||
import { createRawSnippet } from 'svelte';
|
||||
|
||||
let count = $state(0);
|
||||
|
||||
const snippet = createRawSnippet({
|
||||
mount(count) {
|
||||
const div = document.createElement('div');
|
||||
|
||||
$effect(() => {
|
||||
div.textContent = count();
|
||||
});
|
||||
|
||||
return div;
|
||||
},
|
||||
hydrate(element, count) {
|
||||
|
||||
$effect(() => {
|
||||
element.textContent = count();
|
||||
});
|
||||
|
||||
},
|
||||
render(count) {
|
||||
return `<div>${count}</div>`;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div>
|
||||
{@render snippet(count)}
|
||||
</div>
|
||||
<button onclick={() => count++}>+</button>
|
||||
@ -0,0 +1,8 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
compileOptions: {
|
||||
dev: true // Render in dev mode to check that the validation error is not thrown
|
||||
},
|
||||
html: `<p>hello world</p>`
|
||||
});
|
||||
@ -0,0 +1,16 @@
|
||||
<script>
|
||||
import { createRawSnippet } from 'svelte';
|
||||
|
||||
const hello = createRawSnippet({
|
||||
mount() {
|
||||
const p = document.createElement('p')
|
||||
p.textContent = 'hello world';
|
||||
return p;
|
||||
},
|
||||
render() {
|
||||
return '<p>hello world</p>';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
{@render hello()}
|
||||
Loading…
Reference in new issue