Merge pull request #1 from ekhaled/further-svelte-support-help

Make actions work with centered
This commit is contained in:
Gavin King 2018-07-24 16:53:53 +02:00 committed by GitHub
commit 2c8b95862f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 17 deletions

View File

@ -1,6 +1,6 @@
<div class="svelte-centered-wrapper" style="{centeredStyles.style}">
<div class="svelte-centered-container" style="{centeredStyles.innerStyle}">
<svelte:component this="{OriginalComponent}" {...originalData} />
<div class="svelte-centered-wrapper" style="{style}">
<div class="svelte-centered-container" style="{innerStyle}">
<slot></slot>
</div>
</div>
@ -8,9 +8,8 @@
export default {
data() {
return {
OriginalComponent: null,
originalData: {},
centeredStyles: {}
style: '',
innerStyle: ''
};
}
};

View File

@ -19,13 +19,7 @@ const centeredStyles = {
* @see https://svelte.technology/guide#svelte-component
*/
export default function(storyFn) {
const { Component: OriginalComponent, data: originalData } = storyFn();
const { Component: OriginalComponent, data, on } = storyFn();
const centeredData = {
OriginalComponent,
centeredStyles,
originalData,
};
return { Component: Centered, data: centeredData };
return { Component: OriginalComponent, data, on, Wrapper: Centered, WrapperData: centeredStyles };
}

View File

@ -12,8 +12,24 @@ function cleanUpPreviousStory() {
previousComponent = null;
}
function mountView({ Component, target, data, on }) {
const component = new Component({ target, data });
function mountView({ Component, target, data, on, Wrapper, WrapperData }) {
let component;
if (Wrapper) {
const fragment = document.createDocumentFragment();
component = new Component({ target: fragment, data });
const wrapper = new Wrapper({
target,
slots: { default: fragment },
data: WrapperData || {},
});
component.on('destroy', () => {
wrapper.destroy(true);
});
} else {
component = new Component({ target, data });
}
if (on) {
// Attach svelte event listeners.
@ -40,6 +56,8 @@ export default function render({
data,
/** @type {{[string]: () => {}}} Attach svelte event handlers */
on,
Wrapper,
WrapperData,
} = story();
cleanUpPreviousStory();
@ -61,7 +79,7 @@ export default function render({
target.innerHTML = '';
mountView({ Component, target, data, on });
mountView({ Component, target, data, on, Wrapper, WrapperData });
showMain();
}