-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Svelte: Fix events not being logged in Actions when a story has decor…
…ators
- Loading branch information
Showing
4 changed files
with
44 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 30 additions & 10 deletions
40
code/renderers/svelte/src/components/SlotDecorator.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,52 @@ | ||
<script> | ||
import { onMount } from 'svelte'; | ||
import { onMount, getContext } from 'svelte'; | ||
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'; | ||
import { ARG_TYPES_CONTEXT_KEY } from './PreviewRender.svelte'; | ||
export let svelteVersion; | ||
export let decorator = undefined; | ||
export let Component; | ||
export let props = {}; | ||
export let on = undefined; | ||
export let isOriginalStory = false; | ||
let instance; | ||
let decoratorInstance; | ||
if (on && svelteVersion < 5) { | ||
const IS_SVELTE_V4 = Number(SVELTE_VERSION[0]) <= 4; | ||
/* | ||
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 = isOriginalStory | ||
? Object.fromEntries(Object.entries(props).filter(([key]) => !key.startsWith('event_'))) | ||
: { ...props }; | ||
if (isOriginalStory && IS_SVELTE_V4) { | ||
const argTypes = getContext(ARG_TYPES_CONTEXT_KEY) ?? {}; | ||
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/> | ||
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters