From da1cd90a306c3bda2493c0431edf9f5c81dff0a1 Mon Sep 17 00:00:00 2001 From: Nguyen Tran Date: Sun, 30 Apr 2023 18:29:45 -0400 Subject: [PATCH] Implements so that slot defaults apply when forwarding --- src/compiler/compile/render_dom/Block.ts | 11 ++++++++++- src/compiler/compile/render_dom/wrappers/Slot.ts | 1 + .../compile/render_ssr/handlers/InlineComponent.ts | 6 ++++-- src/compiler/compile/render_ssr/handlers/Slot.ts | 4 +++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/compiler/compile/render_dom/Block.ts b/src/compiler/compile/render_dom/Block.ts index cb35673847..95c6596a80 100644 --- a/src/compiler/compile/render_dom/Block.ts +++ b/src/compiler/compile/render_dom/Block.ts @@ -43,6 +43,7 @@ export default class Block { binding_groups: Set = new Set(); chunks: { + conditional_fragment: Array; declarations: Array; init: Array; create: Array; @@ -92,6 +93,7 @@ export default class Block { this.bindings = options.bindings; this.chunks = { + conditional_fragment: [], declarations: [], init: [], create: [], @@ -381,7 +383,7 @@ export default class Block { } } - const return_value: any = x`{ + const return_fragment: any = x`{ key: ${properties.key}, first: ${properties.first}, c: ${properties.create}, @@ -399,6 +401,13 @@ export default class Block { }`; const block = dev && this.get_unique_name('block'); + const conditional_array = { + type: 'ArrayExpression', + elements: this.chunks.conditional_fragment + }; + const conditional = this.chunks.conditional_fragment.length ? x`${conditional_array}.some(@identity)` : null; + + const return_value = conditional ? x`${conditional} ? ${return_fragment} : null` : return_fragment; const body = b` ${this.chunks.declarations} diff --git a/src/compiler/compile/render_dom/wrappers/Slot.ts b/src/compiler/compile/render_dom/wrappers/Slot.ts index 0a589e3394..ed92597400 100644 --- a/src/compiler/compile/render_dom/wrappers/Slot.ts +++ b/src/compiler/compile/render_dom/wrappers/Slot.ts @@ -128,6 +128,7 @@ export default class SlotWrapper extends Wrapper { const slot_definition = block.get_unique_name(`${sanitize(slot_name)}_slot_template`); const slot_or_fallback = has_fallback ? block.get_unique_name(`${sanitize(slot_name)}_slot_or_fallback`) : slot; + block.chunks.conditional_fragment.push(slot_or_fallback); block.chunks.init.push(b` const ${slot_definition} = ${renderer.reference('#slots')}.${slot_name}; const ${slot} = @create_slot(${slot_definition}, #ctx, ${renderer.reference('$$scope')}, ${get_slot_context_fn}); diff --git a/src/compiler/compile/render_ssr/handlers/InlineComponent.ts b/src/compiler/compile/render_ssr/handlers/InlineComponent.ts index 73d31940e2..a636f1e44a 100644 --- a/src/compiler/compile/render_ssr/handlers/InlineComponent.ts +++ b/src/compiler/compile/render_ssr/handlers/InlineComponent.ts @@ -77,9 +77,11 @@ export default function(node: InlineComponent, renderer: Renderer, options: Rend slot_scopes })); - slot_scopes.forEach(({ input, output, statements }, name) => { + slot_scopes.forEach(({ input, output, condition, default_value, statements }, name) => { slot_fns.push( - p`${name}: (${input}) => { ${statements}; return ${output}; }` + condition && default_value + ? p`${name}: ${condition} || ${default_value} ? (${input}) => ${output} : null` + : p`${name}: (${input}) => { ${statements}; return ${output}; }` ); }); } diff --git a/src/compiler/compile/render_ssr/handlers/Slot.ts b/src/compiler/compile/render_ssr/handlers/Slot.ts index f89b619c46..42aea48791 100644 --- a/src/compiler/compile/render_ssr/handlers/Slot.ts +++ b/src/compiler/compile/render_ssr/handlers/Slot.ts @@ -34,7 +34,9 @@ export default function(node: Slot, renderer: Renderer, options: RenderOptions & }); options.slot_scopes.set(slot, { input: get_slot_scope(node.lets), - output: renderer.pop() + condition: x`#slots.${node.slot_name}`, + output: renderer.pop(), + default_value: result }); } }