mirror of https://github.com/sveltejs/svelte
expose which slots are present in $$slots (#4602)
parent
dccdb9f130
commit
0ac341d2c6
@ -1,4 +1,4 @@
|
||||
export const reserved_keywords = new Set(["$$props", "$$restProps"]);
|
||||
export const reserved_keywords = new Set(["$$props", "$$restProps", "$$slots"]);
|
||||
|
||||
export function is_reserved_keyword(name) {
|
||||
return reserved_keywords.has(name);
|
||||
|
@ -0,0 +1,31 @@
|
||||
<script>
|
||||
let data = '';
|
||||
|
||||
if ($$slots.b) {
|
||||
data = 'foo';
|
||||
}
|
||||
|
||||
export function getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
function toString(data) {
|
||||
const result = {};
|
||||
const sortedKeys = Object.keys(data).sort();
|
||||
sortedKeys.forEach(key => result[key] = data[key]);
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
</script>
|
||||
|
||||
<slot></slot>
|
||||
<slot name="a"></slot>
|
||||
|
||||
$$slots: {toString($$slots)}
|
||||
|
||||
{#if $$slots.b}
|
||||
<div>
|
||||
<slot name="b"></slot>
|
||||
</div>
|
||||
{:else}
|
||||
Slot b is not available
|
||||
{/if}
|
@ -0,0 +1,18 @@
|
||||
export default {
|
||||
html: `
|
||||
<span>bye</span><span>world</span>
|
||||
<span slot="a">hello world</span>
|
||||
$$slots: {"a":true,"default":true}
|
||||
Slot b is not available
|
||||
|
||||
<span>bye world</span>
|
||||
<span slot="a">hello world</span>
|
||||
$$slots: {"a":true,"b":true,"default":true}
|
||||
<div><span slot="b">hello world</span></div>
|
||||
`,
|
||||
|
||||
async test({ assert, target, component }) {
|
||||
assert.equal(component.getA(), '');
|
||||
assert.equal(component.getB(), 'foo');
|
||||
}
|
||||
};
|
@ -0,0 +1,23 @@
|
||||
<script>
|
||||
import A from "./A.svelte";
|
||||
let a, b;
|
||||
|
||||
export function getA() {
|
||||
return a.getData();
|
||||
}
|
||||
export function getB() {
|
||||
return b.getData();
|
||||
}
|
||||
</script>
|
||||
|
||||
<A bind:this={a}>
|
||||
<span slot="a">hello world</span>
|
||||
<span>bye</span>
|
||||
<span>world</span>
|
||||
</A>
|
||||
|
||||
<A bind:this={b}>
|
||||
<span slot="a">hello world</span>
|
||||
<span slot="b">hello world</span>
|
||||
<span>bye world</span>
|
||||
</A>
|
Loading…
Reference in new issue