Skip to content

Commit 5ae5bb4

Browse files
committed
handle arbitrary slot names
1 parent b8bc122 commit 5ae5bb4

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/compile/render-dom/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import add_to_set from '../utils/add_to_set';
1010
import get_object from '../utils/get_object';
1111
import { extract_names } from '../utils/scope';
1212
import { nodes_match } from '../../utils/nodes_match';
13-
import { sanitize } from '../../utils/names';
13+
import { sanitize, quote_prop_if_necessary, quote_name_if_necessary } from '../../utils/names';
1414

1515
export default function dom(
1616
component: Component,
@@ -305,8 +305,7 @@ export default function dom(
305305
const reactive_stores = component.vars.filter(variable => variable.name[0] === '$' && variable.name[1] !== '$');
306306

307307
if (renderer.slots.size > 0) {
308-
const arr = Array.from(renderer.slots);
309-
filtered_declarations.push(...arr.map(name => `$$slot_${sanitize(name)}`), '$$scope');
308+
filtered_declarations.push('$$slots', '$$scope');
310309
}
311310

312311
if (renderer.binding_groups.length > 0) {
@@ -399,7 +398,7 @@ export default function dom(
399398
400399
${component.javascript}
401400
402-
${renderer.slots.size && `let { ${[...renderer.slots].map(name => `$$slot_${sanitize(name)}`).join(', ')}, $$scope } = $$props;`}
401+
${renderer.slots.size && `let { $$slots = {}, $$scope } = $$props;`}
403402
404403
${renderer.binding_groups.length > 0 && `const $$binding_groups = [${renderer.binding_groups.map(_ => `[]`).join(', ')}];`}
405404

src/compile/render-dom/wrappers/InlineComponent/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,19 @@ export default class InlineComponentWrapper extends Wrapper {
119119

120120
const uses_spread = !!this.node.attributes.find(a => a.is_spread);
121121

122-
const slot_props = Array.from(this.slots).map(([name, slot]) => `$$slot_${sanitize(name)}: [${slot.block.name}${slot.fn ? `, ${slot.fn}` : ''}]`);
123-
if (slot_props.length > 0) slot_props.push(`$$scope: { ctx }`);
122+
const slot_props = Array.from(this.slots).map(([name, slot]) => `${quote_name_if_necessary(name)}: [${slot.block.name}${slot.fn ? `, ${slot.fn}` : ''}]`);
123+
124+
const initial_props = slot_props.length > 0
125+
? [`$$slots: ${stringify_props(slot_props)}`, `$$scope: { ctx }`]
126+
: [];
124127

125128
const attribute_object = uses_spread
126-
? stringify_props(slot_props)
129+
? stringify_props(initial_props)
127130
: stringify_props(
128-
this.node.attributes.map(attr => `${quote_name_if_necessary(attr.name)}: ${attr.get_value(block)}`).concat(slot_props)
131+
this.node.attributes.map(attr => `${quote_name_if_necessary(attr.name)}: ${attr.get_value(block)}`).concat(initial_props)
129132
);
130133

131-
if (this.node.attributes.length || this.node.bindings.length || slot_props.length) {
134+
if (this.node.attributes.length || this.node.bindings.length || initial_props.length) {
132135
if (!uses_spread && this.node.bindings.length === 0) {
133136
component_opts.push(`props: ${attribute_object}`);
134137
} else {

src/compile/render-dom/wrappers/Slot.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Block from '../Block';
44
import Slot from '../../nodes/Slot';
55
import FragmentWrapper from './Fragment';
66
import deindent from '../../utils/deindent';
7-
import { sanitize } from '../../../utils/names';
7+
import { sanitize, quote_prop_if_necessary } from '../../../utils/names';
88
import add_to_set from '../../utils/add_to_set';
99
import get_slot_data from '../../utils/get_slot_data';
1010
import { stringify_props } from '../../utils/stringify_props';
@@ -99,7 +99,7 @@ export default class SlotWrapper extends Wrapper {
9999
const slot_definition = block.get_unique_name(`${sanitize(slot_name)}_slot`);
100100

101101
block.builders.init.add_block(deindent`
102-
const ${slot_definition} = ctx.$$slot_${sanitize(slot_name)};
102+
const ${slot_definition} = ctx.$$slots${quote_prop_if_necessary(slot_name)};
103103
const ${slot} = @create_slot(${slot_definition}, ctx, ${get_slot_context});
104104
`);
105105

0 commit comments

Comments
 (0)