Skip to content

Commit

Permalink
feat: createSlot util
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Feb 12, 2020
1 parent 188edd2 commit 969ad23
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 51 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = {
'svelte/internal',
'svelte/store',
'svelte/easing',
'svelte/slot',
'estree'
],
'svelte3/compiler': require('./compiler')
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ node_modules
/motion
/transition
/animate
/slot
/scratch/
/coverage/
/coverage.lcov
Expand Down
5 changes: 0 additions & 5 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,6 @@ export default function dom(
constructor(options) {
super(${options.dev && `options`});
${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});
${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`}
Expand Down
39 changes: 4 additions & 35 deletions src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler';
import { current_component, set_current_component } from './lifecycle';
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';

interface Fragment {
Expand Down Expand Up @@ -101,6 +101,9 @@ export function init(component, options, instance, create_fragment, not_equal, p
set_current_component(component);

const prop_values = options.props || {};
if (options.slots) {
prop_values.$$slots = options.slots;
}

const $$: T$$ = component.$$ = {
fragment: null,
Expand Down Expand Up @@ -226,37 +229,3 @@ export class SvelteComponent {
// 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;
}
2 changes: 1 addition & 1 deletion src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj,

export function action_destroyer(action_result) {
return action_result && is_function(action_result.destroy) ? action_result.destroy : noop;
}
}
35 changes: 35 additions & 0 deletions src/runtime/slot/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 0 additions & 8 deletions test/js/samples/root-component-slot/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import {
SvelteComponent,
append,
create_root_component_slots,
create_slot,
detach,
element,
Expand Down Expand Up @@ -91,13 +90,6 @@ function instance($$self, $$props, $$invalidate) {
class Component extends SvelteComponent {
constructor(options) {
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, {});
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/runtime/samples/root-component-slot/_config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createSlot } from 'svelte/slot';

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

Expand Down

0 comments on commit 969ad23

Please sign in to comment.