mirror of https://github.com/sveltejs/svelte
feat: $props.id(), a SSR-safe ID generation (#15185)
* first impl of $$uid * fix * $props.id() * fix errors * rename $.create_uid() into $.props_id() * fix message * relax const requirement, validate assignments instead * oops * simplify * non-constants should be lowercased * ditto * start at 1 * add docs * changeset * add test * add docs * doc : add code example * fix type reported by bennymi --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/15274/head
parent
73220b8667
commit
85f83ec435
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
feat: SSR-safe ID generation with `$props.id()`
|
@ -0,0 +1,5 @@
|
|||||||
|
<script>
|
||||||
|
let id = $props.id();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<p>{id}</p>
|
@ -0,0 +1,61 @@
|
|||||||
|
import { flushSync } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
test({ assert, target, variant }) {
|
||||||
|
if (variant === 'dom') {
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>toggle</button>
|
||||||
|
<h1>c1</h1>
|
||||||
|
<p>c2</p>
|
||||||
|
<p>c3</p>
|
||||||
|
<p>c4</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>toggle</button>
|
||||||
|
<h1>s1</h1>
|
||||||
|
<p>s2</p>
|
||||||
|
<p>s3</p>
|
||||||
|
<p>s4</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let button = target.querySelector('button');
|
||||||
|
flushSync(() => button?.click());
|
||||||
|
|
||||||
|
if (variant === 'dom') {
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>toggle</button>
|
||||||
|
<h1>c1</h1>
|
||||||
|
<p>c2</p>
|
||||||
|
<p>c3</p>
|
||||||
|
<p>c4</p>
|
||||||
|
<p>c5</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// `c6` because this runs after the `dom` tests
|
||||||
|
// (slightly brittle but good enough for now)
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>toggle</button>
|
||||||
|
<h1>s1</h1>
|
||||||
|
<p>s2</p>
|
||||||
|
<p>s3</p>
|
||||||
|
<p>s4</p>
|
||||||
|
<p>c6</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,19 @@
|
|||||||
|
<script>
|
||||||
|
import Child from './Child.svelte';
|
||||||
|
|
||||||
|
let id = $props.id();
|
||||||
|
|
||||||
|
let show = $state(false);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={() => show = !show}>toggle</button>
|
||||||
|
|
||||||
|
<h1>{id}</h1>
|
||||||
|
|
||||||
|
<Child />
|
||||||
|
<Child />
|
||||||
|
<Child />
|
||||||
|
|
||||||
|
{#if show}
|
||||||
|
<Child />
|
||||||
|
{/if}
|
Loading…
Reference in new issue