Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte: Fix Svelte 3 slots for decorators #9724

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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