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

Fix stories not re-rendering when args change in Controls #99

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
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
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);
}