feat: createSlot util

pull/4296/head
Tan Li Hau 6 years ago
parent 188edd2464
commit 969ad237b1

@ -66,6 +66,7 @@ module.exports = {
'svelte/internal', 'svelte/internal',
'svelte/store', 'svelte/store',
'svelte/easing', 'svelte/easing',
'svelte/slot',
'estree' 'estree'
], ],
'svelte3/compiler': require('./compiler') 'svelte3/compiler': require('./compiler')

1
.gitignore vendored

@ -13,6 +13,7 @@ node_modules
/motion /motion
/transition /transition
/animate /animate
/slot
/scratch/ /scratch/
/coverage/ /coverage/
/coverage.lcov /coverage.lcov

@ -496,11 +496,6 @@ export default function dom(
constructor(options) { constructor(options) {
super(${options.dev && `options`}); super(${options.dev && `options`});
${should_add_css && b`if (!@_document.getElementById("${component.stylesheet.id}-style")) ${add_css}();`} ${should_add_css && b`if (!@_document.getElementById("${component.stylesheet.id}-style")) ${add_css}();`}
${component.slots.size > 0 && b`if (options.slots) {
options.props = options.props || {};
options.props.$$scope = {};
options.props.$$slots = @create_root_component_slots(options.slots);
}`}
@init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment': 'null'}, ${not_equal}, ${prop_indexes}, ${dirty}); @init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment': 'null'}, ${not_equal}, ${prop_indexes}, ${dirty});
${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`} ${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`}

@ -1,7 +1,7 @@
import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler'; import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler';
import { current_component, set_current_component } from './lifecycle'; import { current_component, set_current_component } from './lifecycle';
import { blank_object, is_function, run, run_all, noop } from './utils'; import { blank_object, is_function, run, run_all, noop } from './utils';
import { children, insert, detach } from './dom'; import { children } from './dom';
import { transition_in } from './transitions'; import { transition_in } from './transitions';
interface Fragment { interface Fragment {
@ -101,6 +101,9 @@ export function init(component, options, instance, create_fragment, not_equal, p
set_current_component(component); set_current_component(component);
const prop_values = options.props || {}; const prop_values = options.props || {};
if (options.slots) {
prop_values.$$slots = options.slots;
}
const $$: T$$ = component.$$ = { const $$: T$$ = component.$$ = {
fragment: null, fragment: null,
@ -226,37 +229,3 @@ export class SvelteComponent {
// overridden by instance, if it has props // overridden by instance, if it has props
} }
} }
function create_root_component_slot_fn(elements) {
return function create_root_component_slot() {
return {
c: noop,
m: function mount(target, anchor) {
elements.forEach(element => {
insert(target, element, anchor);
});
},
d: function destroy(detaching) {
if (detaching) {
elements.forEach(element => detach(element));
}
},
l: noop,
};
};
}
export function create_root_component_slots(slots) {
const root_component_slots = {};
for (const slot_name in slots) {
let elements = slots[slot_name];
if (!Array.isArray(elements)) {
elements = [elements];
}
root_component_slots[slot_name] = [create_root_component_slot_fn(elements)];
}
return root_component_slots;
}

@ -0,0 +1,35 @@
import { noop, insert, detach } from 'svelte/internal';
function create_root_slot_fn(elements) {
return function create_root_slot() {
return {
c: noop,
m: function mount(target, anchor) {
elements.forEach(element => {
insert(target, element, anchor);
});
},
d: function destroy(detaching) {
if (detaching) {
elements.forEach(element => detach(element));
}
},
l: noop,
};
};
}
export function createSlot(slots) {
const root_slots = {};
for (const slot_name in slots) {
let elements = slots[slot_name];
if (!Array.isArray(elements)) {
elements = [elements];
}
root_slots[slot_name] = [create_root_slot_fn(elements)];
}
return root_slots;
}

@ -2,7 +2,6 @@
import { import {
SvelteComponent, SvelteComponent,
append, append,
create_root_component_slots,
create_slot, create_slot,
detach, detach,
element, element,
@ -91,13 +90,6 @@ function instance($$self, $$props, $$invalidate) {
class Component extends SvelteComponent { class Component extends SvelteComponent {
constructor(options) { constructor(options) {
super(); super();
if (options.slots) {
options.props = options.props || {};
options.props.$$scope = {};
options.props.$$slots = create_root_component_slots(options.slots);
}
init(this, options, instance, create_fragment, safe_not_equal, {}); init(this, options, instance, create_fragment, safe_not_equal, {});
} }
} }

@ -1,3 +1,5 @@
import { createSlot } from 'svelte/slot';
export default { export default {
options(window) { options(window) {
const default_el = window.document.createElement('div'); const default_el = window.document.createElement('div');
@ -11,12 +13,12 @@ export default {
const conditional_slot_el = window.document.createElement('div'); const conditional_slot_el = window.document.createElement('div');
conditional_slot_el.innerHTML = 'conditional slot'; conditional_slot_el.innerHTML = 'conditional slot';
return { return {
slots: { slots: createSlot({
default: default_el, default: default_el,
'my-slot': my_slot_els, 'my-slot': my_slot_els,
'another-slot-with-content': another_slot_el, 'another-slot-with-content': another_slot_el,
'conditional-slot': conditional_slot_el, 'conditional-slot': conditional_slot_el,
}, }),
}; };
}, },

Loading…
Cancel
Save