Skip to content

Commit

Permalink
alternative implementation of reactive Svelte Stories (using seperate…
Browse files Browse the repository at this point in the history
… context entries to store args and global context during rendertime
  • Loading branch information
leika committed Apr 5, 2023
1 parent 9904cb1 commit 0625703
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
16 changes: 7 additions & 9 deletions src/components/RenderContext.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
* @component
* @wrapper
*/
import { createRenderContext } from './context';
import { tick } from 'svelte';
import { createRenderContext, setStoryRenderContext } from './context';
export let Stories;
let exporttime = 0;
export let args = {};
export let storyContext = {};
$: {
createRenderContext($$props);
}
createRenderContext($$props);

This comment has been minimized.

Copy link
@mantismamita

mantismamita Apr 5, 2023

does this maybe need to be reactive?

This comment has been minimized.

Copy link
@leika

leika Apr 17, 2023

Author Contributor

not really, the setStoryRenderContext is reactive, and will push changes to writable stores which are later used by the Component Wrappers. (see Template.svelte and Story.svelte in this Commit)

$: setStoryRenderContext(args, storyContext);
</script>

{#key $$props.args}

This comment has been minimized.

Copy link
@mantismamita

mantismamita Apr 5, 2023

I think this is the main problem why the component re-renders constantly.

This comment has been minimized.

Copy link
@leika

leika Apr 17, 2023

Author Contributor

yeah, that was the first attempt. This is why this commit changes it and it now wraps the args and globals into writables which are reused on changes. That way the Component will be able to react and the rebuild-enforce hack of the initial fix is no longer used

<svelte:component this={Stories} />
{/key}
<svelte:component this={Stories} />
10 changes: 6 additions & 4 deletions src/components/Story.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { useContext } from './context';
import { getStoryRenderContext, useContext } from './context';
const context = useContext();
Expand All @@ -12,14 +12,16 @@
context.register({
name,
...$$restProps,
...$$restProps,
template: template != null ? template : !$$slots.default ? 'default' : null,
});
$: render = context.render && !context.templateName && context.storyName == name;
const ctx = getStoryRenderContext();
const args = ctx.argsStore;
const storyContext = ctx.storyContextStore;
</script>

{#if render}
<slot {...context.args} context={context.storyContext} args={context.args}/>
<slot {...$args} context={$storyContext} args={$args} />
{/if}
9 changes: 6 additions & 3 deletions src/components/Template.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<script>
import { useContext } from './context';
import { useContext, getStoryRenderContext } from './context';
const context = useContext();
export let id = 'default';
context.register({id, isTemplate: true});
context.register({ id, isTemplate: true });
$: render = context.render && context.templateId === id;
const ctx = getStoryRenderContext();
const args = ctx.argsStore;
const storyContext = ctx.storyContextStore;
</script>

{#if render}
<slot {...context.args} context={context.storyContext} args={context.args}/>
<slot {...$args} context={$storyContext} args={$args} />
{/if}
21 changes: 19 additions & 2 deletions src/components/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getContext, hasContext, setContext } from "svelte";
import { getContext, hasContext, setContext } from 'svelte';
import { writable, Writable } from 'svelte/store';

const CONTEXT_KEY = "storybook-registration-context";
const CONTEXT_KEY = 'storybook-registration-context';
const CONTEXT_KEY_COMPONENT = 'storybook-registration-context-component';

export function createRenderContext(props: any = {}) {
setContext(CONTEXT_KEY, {
Expand Down Expand Up @@ -32,3 +34,18 @@ export function useContext() {
}
return getContext(CONTEXT_KEY);
}
export function getStoryRenderContext(): {
argsStore: Writable<Record<string, unknown>>;
storyContextStore: Writable<Record<string, unknown>>;
} {
if (!hasContext(CONTEXT_KEY_COMPONENT)) {
setContext(CONTEXT_KEY_COMPONENT, { argsStore: writable({}), storyContextStore: writable({}) });
}
return getContext(CONTEXT_KEY_COMPONENT);
}

export function setStoryRenderContext(args, storyContext) {
const ctx = getStoryRenderContext();
ctx.argsStore.set(args);
ctx.storyContextStore.set(storyContext);
}

0 comments on commit 0625703

Please sign in to comment.