Skip to content

Commit 969ad23

Browse files
committed
feat: createSlot util
1 parent 188edd2 commit 969ad23

File tree

8 files changed

+46
-51
lines changed

8 files changed

+46
-51
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ module.exports = {
6666
'svelte/internal',
6767
'svelte/store',
6868
'svelte/easing',
69+
'svelte/slot',
6970
'estree'
7071
],
7172
'svelte3/compiler': require('./compiler')

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ node_modules
1313
/motion
1414
/transition
1515
/animate
16+
/slot
1617
/scratch/
1718
/coverage/
1819
/coverage.lcov

src/compiler/compile/render_dom/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,6 @@ export default function dom(
496496
constructor(options) {
497497
super(${options.dev && `options`});
498498
${should_add_css && b`if (!@_document.getElementById("${component.stylesheet.id}-style")) ${add_css}();`}
499-
${component.slots.size > 0 && b`if (options.slots) {
500-
options.props = options.props || {};
501-
options.props.$$scope = {};
502-
options.props.$$slots = @create_root_component_slots(options.slots);
503-
}`}
504499
@init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment': 'null'}, ${not_equal}, ${prop_indexes}, ${dirty});
505500
${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`}
506501

src/runtime/internal/Component.ts

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler';
22
import { current_component, set_current_component } from './lifecycle';
33
import { blank_object, is_function, run, run_all, noop } from './utils';
4-
import { children, insert, detach } from './dom';
4+
import { children } from './dom';
55
import { transition_in } from './transitions';
66

77
interface Fragment {
@@ -101,6 +101,9 @@ export function init(component, options, instance, create_fragment, not_equal, p
101101
set_current_component(component);
102102

103103
const prop_values = options.props || {};
104+
if (options.slots) {
105+
prop_values.$$slots = options.slots;
106+
}
104107

105108
const $$: T$$ = component.$$ = {
106109
fragment: null,
@@ -226,37 +229,3 @@ export class SvelteComponent {
226229
// overridden by instance, if it has props
227230
}
228231
}
229-
230-
function create_root_component_slot_fn(elements) {
231-
return function create_root_component_slot() {
232-
return {
233-
c: noop,
234-
235-
m: function mount(target, anchor) {
236-
elements.forEach(element => {
237-
insert(target, element, anchor);
238-
});
239-
},
240-
241-
d: function destroy(detaching) {
242-
if (detaching) {
243-
elements.forEach(element => detach(element));
244-
}
245-
},
246-
247-
l: noop,
248-
};
249-
};
250-
}
251-
252-
export function create_root_component_slots(slots) {
253-
const root_component_slots = {};
254-
for (const slot_name in slots) {
255-
let elements = slots[slot_name];
256-
if (!Array.isArray(elements)) {
257-
elements = [elements];
258-
}
259-
root_component_slots[slot_name] = [create_root_component_slot_fn(elements)];
260-
}
261-
return root_component_slots;
262-
}

src/runtime/internal/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,4 @@ export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj,
127127

128128
export function action_destroyer(action_result) {
129129
return action_result && is_function(action_result.destroy) ? action_result.destroy : noop;
130-
}
130+
}

src/runtime/slot/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { noop, insert, detach } from 'svelte/internal';
2+
3+
function create_root_slot_fn(elements) {
4+
return function create_root_slot() {
5+
return {
6+
c: noop,
7+
8+
m: function mount(target, anchor) {
9+
elements.forEach(element => {
10+
insert(target, element, anchor);
11+
});
12+
},
13+
14+
d: function destroy(detaching) {
15+
if (detaching) {
16+
elements.forEach(element => detach(element));
17+
}
18+
},
19+
20+
l: noop,
21+
};
22+
};
23+
}
24+
25+
export function createSlot(slots) {
26+
const root_slots = {};
27+
for (const slot_name in slots) {
28+
let elements = slots[slot_name];
29+
if (!Array.isArray(elements)) {
30+
elements = [elements];
31+
}
32+
root_slots[slot_name] = [create_root_slot_fn(elements)];
33+
}
34+
return root_slots;
35+
}

test/js/samples/root-component-slot/expected.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import {
33
SvelteComponent,
44
append,
5-
create_root_component_slots,
65
create_slot,
76
detach,
87
element,
@@ -91,13 +90,6 @@ function instance($$self, $$props, $$invalidate) {
9190
class Component extends SvelteComponent {
9291
constructor(options) {
9392
super();
94-
95-
if (options.slots) {
96-
options.props = options.props || {};
97-
options.props.$$scope = {};
98-
options.props.$$slots = create_root_component_slots(options.slots);
99-
}
100-
10193
init(this, options, instance, create_fragment, safe_not_equal, {});
10294
}
10395
}

test/runtime/samples/root-component-slot/_config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createSlot } from 'svelte/slot';
2+
13
export default {
24
options(window) {
35
const default_el = window.document.createElement('div');
@@ -11,12 +13,12 @@ export default {
1113
const conditional_slot_el = window.document.createElement('div');
1214
conditional_slot_el.innerHTML = 'conditional slot';
1315
return {
14-
slots: {
16+
slots: createSlot({
1517
default: default_el,
1618
'my-slot': my_slot_els,
1719
'another-slot-with-content': another_slot_el,
1820
'conditional-slot': conditional_slot_el,
19-
},
21+
}),
2022
};
2123
},
2224

0 commit comments

Comments
 (0)