Skip to content

Commit

Permalink
Merge pull request #99 from marx-merten/fix-property-update
Browse files Browse the repository at this point in the history
Fix stories not re-rendering when args change in Controls
  • Loading branch information
JReinhold authored Apr 21, 2023
2 parents af6b787 + 0625703 commit 9498e50
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/components/RenderContext.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
* @component
* @wrapper
*/
import { createRenderContext } from './context';
import { createRenderContext, setStoryRenderContext } from './context';
export let Stories;
export let args = {};
export let storyContext = {};
createRenderContext($$props);
$: setStoryRenderContext(args, storyContext);
</script>

<svelte:component this={Stories}/>
<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 9498e50

Please sign in to comment.