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

Svelte: Fix events not being logged in Actions when a story has decorators #28247

Merged
merged 4 commits into from
Aug 22, 2024
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
14 changes: 6 additions & 8 deletions code/renderers/svelte/src/components/PreviewRender.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
props = {},
/** @type {{[string]: () => {}}} Attach svelte event handlers */
on,
/** @type {any} whether this level of the decorator chain is the last, ie. the actual story */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems incorrect.

argTypes,
} = storyFn();

let firstTime = true;
Expand All @@ -29,20 +31,16 @@
Component,
props,
on,
argTypes,
};
}
return storyFn();
}

// reactive, re-render on storyFn change
$: ({ Component, props = {}, on } = getStoryFnValue(storyFn));

const eventsFromArgTypes = Object.fromEntries(
Object.entries(storyContext.argTypes)
.filter(([k, v]) => v.action && props[k] != null)
.map(([k, v]) => [v.action, props[k]])
);
$: ({ Component, props = {}, on, argTypes } = getStoryFnValue(storyFn));

// set the argTypes context, read by the last SlotDecorator that renders the original story
if (!Component) {
showError({
title: `Expecting a Svelte component from the story: "${name}" of "${title}".`,
Expand All @@ -55,4 +53,4 @@
}
</script>

<SlotDecorator {Component} {props} on={{ ...eventsFromArgTypes, ...on }} />
<SlotDecorator {Component} {props} {on} {argTypes} />
28 changes: 22 additions & 6 deletions code/renderers/svelte/src/components/SlotDecorator.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,45 @@
export let Component;
export let props = {};
export let on = undefined;
export let argTypes = undefined;

let instance;
let decoratorInstance;
Comment on lines 11 to 12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are those 2 variables doing? I don't see them anywhere set in the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are set in the markup below in the file, and read on line 39.
They are used to listen to events from the instances.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see, it is like a react ref?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you're confusing them for props? They aren't exported so they aren't props, they are just regular variables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean it is like:

const instance = useRef();

<div ref={instance} />

In react?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you could compare it to a ref, it's just a regular long-living variable.

(my previous message wasn't a response to your react message, we basically posted them at the same time)


const svelteVersion = VERSION[0];

/*
Svelte Docgen will create argTypes for events with the name 'event_eventName'
The Actions addon will convert these to args because they are type: 'action'
We need to filter these args out so they are not passed to the component
*/
let propsWithoutDocgenEvents;
$: propsWithoutDocgenEvents = Object.fromEntries(
Object.entries(props).filter(([key]) => !key.startsWith('event_'))
);

if (on && svelteVersion < 5) {
if (argTypes && svelteVersion < 5) {
const eventsFromArgTypes = Object.fromEntries(
Object.entries(argTypes)
.filter(([key, value]) => value.action && props[key] != null)
.map(([key, value]) => [value.action, props[key]])
);
// Attach Svelte event listeners in Svelte v4
// In Svelte v5 this is not possible anymore as instances are no longer classes with $on() properties, so it will be a no-op
onMount(() => {
Object.entries(on).forEach(([eventName, eventCallback]) => {
// instance can be undefined if a decorator doesn't have <slot/>
Object.entries({ ...eventsFromArgTypes, ...on }).forEach(([eventName, eventCallback]) => {
// instance can be undefined if a decorator doesn't have a <slot/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// instance can be undefined if a decorator doesn't have a

shouldn't that be an error though?

Copy link
Contributor Author

@JReinhold JReinhold Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's debatable, I guess it's similar to not calling storyFn() in a React decorator, to not render the actual story. Would that be invalid? 🤷

const inst = instance ?? decoratorInstance;
inst?.$on?.(eventName, eventCallback)
inst?.$on?.(eventName, eventCallback);
});
});
}
</script>

{#if decorator}
<svelte:component this={decorator.Component} {...decorator.props} bind:this={decoratorInstance}>
<svelte:component this={Component} {...props} bind:this={instance} />
<svelte:component this={Component} {...propsWithoutDocgenEvents} bind:this={instance} />
</svelte:component>
{:else}
<svelte:component this={Component} {...props} bind:this={instance} />
<svelte:component this={Component} {...propsWithoutDocgenEvents} bind:this={instance} />
{/if}
3 changes: 2 additions & 1 deletion code/renderers/svelte/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function prepareStory(
};
}

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

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