Skip to content

Commit 039f2e8

Browse files
authored
Merge pull request #28247 from storybookjs/jeppe/fix-svelte-decorator-actions
Svelte: Fix events not being logged in Actions when a story has decorators
2 parents 1dddc61 + de8c666 commit 039f2e8

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

code/renderers/svelte/src/components/PreviewRender.svelte

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
props = {},
1616
/** @type {{[string]: () => {}}} Attach svelte event handlers */
1717
on,
18+
/** @type {any} whether this level of the decorator chain is the last, ie. the actual story */
19+
argTypes,
1820
} = storyFn();
1921
2022
let firstTime = true;
@@ -29,20 +31,16 @@
2931
Component,
3032
props,
3133
on,
34+
argTypes,
3235
};
3336
}
3437
return storyFn();
3538
}
3639
3740
// reactive, re-render on storyFn change
38-
$: ({ Component, props = {}, on } = getStoryFnValue(storyFn));
39-
40-
const eventsFromArgTypes = Object.fromEntries(
41-
Object.entries(storyContext.argTypes)
42-
.filter(([k, v]) => v.action && props[k] != null)
43-
.map(([k, v]) => [v.action, props[k]])
44-
);
41+
$: ({ Component, props = {}, on, argTypes } = getStoryFnValue(storyFn));
4542
43+
// set the argTypes context, read by the last SlotDecorator that renders the original story
4644
if (!Component) {
4745
showError({
4846
title: `Expecting a Svelte component from the story: "${name}" of "${title}".`,
@@ -55,4 +53,4 @@
5553
}
5654
</script>
5755

58-
<SlotDecorator {Component} {props} on={{ ...eventsFromArgTypes, ...on }} />
56+
<SlotDecorator {Component} {props} {on} {argTypes} />

code/renderers/svelte/src/components/SlotDecorator.svelte

+22-6
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,45 @@
66
export let Component;
77
export let props = {};
88
export let on = undefined;
9+
export let argTypes = undefined;
910
1011
let instance;
1112
let decoratorInstance;
1213
1314
const svelteVersion = VERSION[0];
15+
16+
/*
17+
Svelte Docgen will create argTypes for events with the name 'event_eventName'
18+
The Actions addon will convert these to args because they are type: 'action'
19+
We need to filter these args out so they are not passed to the component
20+
*/
21+
let propsWithoutDocgenEvents;
22+
$: propsWithoutDocgenEvents = Object.fromEntries(
23+
Object.entries(props).filter(([key]) => !key.startsWith('event_'))
24+
);
1425
15-
if (on && svelteVersion < 5) {
26+
if (argTypes && svelteVersion < 5) {
27+
const eventsFromArgTypes = Object.fromEntries(
28+
Object.entries(argTypes)
29+
.filter(([key, value]) => value.action && props[key] != null)
30+
.map(([key, value]) => [value.action, props[key]])
31+
);
1632
// Attach Svelte event listeners in Svelte v4
1733
// In Svelte v5 this is not possible anymore as instances are no longer classes with $on() properties, so it will be a no-op
1834
onMount(() => {
19-
Object.entries(on).forEach(([eventName, eventCallback]) => {
20-
// instance can be undefined if a decorator doesn't have <slot/>
35+
Object.entries({ ...eventsFromArgTypes, ...on }).forEach(([eventName, eventCallback]) => {
36+
// instance can be undefined if a decorator doesn't have a <slot/>
2137
const inst = instance ?? decoratorInstance;
22-
inst?.$on?.(eventName, eventCallback)
38+
inst?.$on?.(eventName, eventCallback);
2339
});
2440
});
2541
}
2642
</script>
2743
2844
{#if decorator}
2945
<svelte:component this={decorator.Component} {...decorator.props} bind:this={decoratorInstance}>
30-
<svelte:component this={Component} {...props} bind:this={instance} />
46+
<svelte:component this={Component} {...propsWithoutDocgenEvents} bind:this={instance} />
3147
</svelte:component>
3248
{:else}
33-
<svelte:component this={Component} {...props} bind:this={instance} />
49+
<svelte:component this={Component} {...propsWithoutDocgenEvents} bind:this={instance} />
3450
{/if}

code/renderers/svelte/src/decorators.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function prepareStory(
7474
};
7575
}
7676

77-
return preparedStory;
77+
// no innerStory means this is the last story in the decorator chain, so it should create events from argTypes
78+
return { ...preparedStory, argTypes: context.argTypes };
7879
}
7980

8081
export function decorateStory(storyFn: any, decorators: any[]) {

0 commit comments

Comments
 (0)