Skip to content

Commit

Permalink
Svelte: Fix Svelte 3 slots for decorators (#9724)
Browse files Browse the repository at this point in the history
Svelte: Fix Svelte 3 slots for decorators
  • Loading branch information
shilman authored Feb 4, 2020
2 parents e3a0b4b + 447e235 commit cfeb47d
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions app/svelte/src/client/preview/render.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { detach, insert, noop } from 'svelte/internal';
import { document } from 'global';
import dedent from 'ts-dedent';
import { MountViewArgs, RenderMainArgs } from './types';
Expand All @@ -14,6 +15,32 @@ function cleanUpPreviousStory() {
previousComponent = null;
}

function createSlotFn(element: any) {
return [
function createSlot() {
return {
c: noop,
m: function mount(target: any, anchor: any) {
insert(target, element, anchor);
},
d: function destroy(detaching: boolean) {
if (detaching) {
detach(element);
}
},
l: noop,
};
},
];
}

function createSlots(slots: Record<string, any>): Record<string, any> {
return Object.entries(slots).reduce((acc, [slotName, element]) => {
acc[slotName] = createSlotFn(element);
return acc;
}, {} as Record<string, any>);
}

function mountView({ Component, target, props, on, Wrapper, WrapperData }: MountViewArgs) {
let component: Component;

Expand All @@ -23,8 +50,11 @@ function mountView({ Component, target, props, on, Wrapper, WrapperData }: Mount

const wrapper = new Wrapper({
target,
slots: { default: fragment },
props: WrapperData || {},
props: {
...WrapperData,
$$slots: createSlots({ default: fragment }),
$$scope: {},
},
});
component.$on('destroy', () => {
wrapper.$destroy(true);
Expand Down

0 comments on commit cfeb47d

Please sign in to comment.