fix: ensure `$$slots` returns a record of booleans (#12359)

Returned the underlying functions previously
pull/12365/head
Simon H 6 months ago committed by GitHub
parent c314d1bb33
commit 3dbb220169
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure `$$slots` returns a record of booleans

@ -13,3 +13,17 @@ export function slot(anchor, slot_fn, slot_props, fallback_fn) {
slot_fn(anchor, slot_props);
}
}
/**
* @param {Record<string, any>} props
* @returns {Record<string, boolean>}
*/
export function sanitize_slots(props) {
/** @type {Record<string, boolean>} */
const sanitized = {};
if (props.children) sanitized.default = true;
for (const key in props.$$slots) {
sanitized[key] = true;
}
return sanitized;
}

@ -15,7 +15,7 @@ export { key_block as key } from './dom/blocks/key.js';
export { css_props } from './dom/blocks/css-props.js';
export { index, each } from './dom/blocks/each.js';
export { html } from './dom/blocks/html.js';
export { slot } from './dom/blocks/slot.js';
export { sanitize_slots, slot } from './dom/blocks/slot.js';
export { snippet, wrap_snippet } from './dom/blocks/snippet.js';
export { component } from './dom/blocks/svelte-component.js';
export { element } from './dom/blocks/svelte-element.js';
@ -119,7 +119,7 @@ export {
update_pre_store,
update_store
} from './reactivity/store.js';
export { append_styles, sanitize_slots, set_text } from './render.js';
export { append_styles, set_text } from './render.js';
export {
get,
invalidate_inner_signals,

@ -267,16 +267,6 @@ export function unmount(component) {
fn?.();
}
/**
* @param {Record<string, any>} props
* @returns {Record<string, any>}
*/
export function sanitize_slots(props) {
const sanitized = { ...props.$$slots };
if (props.children) sanitized.default = props.children;
return sanitized;
}
/**
* @param {Node} target
* @param {string} style_sheet_id

@ -446,11 +446,15 @@ export function sanitize_props(props) {
/**
* @param {Record<string, any>} props
* @returns {Record<string, any>}
* @returns {Record<string, boolean>}
*/
export function sanitize_slots(props) {
const sanitized = { ...props.$$slots };
if (props.children) sanitized.default = props.children;
/** @type {Record<string, boolean>} */
const sanitized = {};
if (props.children) sanitized.default = true;
for (const key in props.$$slots) {
sanitized[key] = true;
}
return sanitized;
}

@ -1,21 +1,20 @@
<script>
let data = '';
let data = '';
if ($$slots.b) {
data = 'foo';
}
if ($$slots.b) {
data = 'foo';
}
export function getData() {
return data;
}
export function getData() {
return data;
}
function toString(data) {
const result = {};
const sortedKeys = Object.keys(data).sort();
// TODO added !! to make it work since $$slots exposes the slot functions - we need to decide what to do with them
sortedKeys.forEach((key) => (result[key] = !!data[key]));
return JSON.stringify(result);
}
function toString(data) {
const result = {};
const sortedKeys = Object.keys(data).sort();
sortedKeys.forEach((key) => (result[key] = data[key]));
return JSON.stringify(result);
}
</script>
<slot />
@ -24,9 +23,9 @@
$$slots: {toString($$slots)}
{#if $$slots.b}
<div>
<slot name="b" />
</div>
<div>
<slot name="b" />
</div>
{:else}
Slot b is not available
Slot b is not available
{/if}

Loading…
Cancel
Save