-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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 decorators with slots #19987
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c912560
works, but only for a single decorator
JReinhold 2589486
refactor decorators in Svelte
JReinhold 2f173c6
cleanup
JReinhold e10534e
add decorator stories to svelte renderer
JReinhold 3d43fae
add decorator story to mdx stories
JReinhold 942e339
cleanup
JReinhold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
42 changes: 42 additions & 0 deletions
42
code/renderers/svelte/template/stories/slot-decorators.stories.js
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import ButtonView from './views/ButtonView.svelte'; | ||
import BorderDecoratorRed from './views/BorderDecoratorRed.svelte'; | ||
import BorderDecoratorBlue from './views/BorderDecoratorBlue.svelte'; | ||
import BorderDecoratorProps from './views/BorderDecoratorProps.svelte'; | ||
|
||
export default { | ||
component: ButtonView, | ||
decorators: [() => BorderDecoratorRed], | ||
args: { | ||
primary: true, | ||
}, | ||
}; | ||
|
||
export const WithDefaultRedBorder = {}; | ||
export const WithBareBlueBorder = { | ||
decorators: [() => BorderDecoratorBlue], | ||
}; | ||
export const WithPreparedBlueBorder = { | ||
decorators: [ | ||
() => ({ | ||
Component: BorderDecoratorBlue, | ||
}), | ||
], | ||
}; | ||
export const WithPropsBasedBorder = { | ||
decorators: [ | ||
() => ({ | ||
Component: BorderDecoratorProps, | ||
props: { color: 'green' }, | ||
}), | ||
], | ||
}; | ||
export const WithArgsBasedBorderUnset = { | ||
argTypes: { | ||
color: { control: 'color' }, | ||
}, | ||
decorators: [(_, { args }) => ({ Component: BorderDecoratorProps, props: args })], | ||
}; | ||
export const WithArgsBasedBorder = { | ||
...WithArgsBasedBorderUnset, | ||
args: { color: 'lightblue' }, | ||
}; |
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
3 changes: 3 additions & 0 deletions
3
code/renderers/svelte/template/stories/views/BorderDecoratorBlue.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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div style="border: 3px solid blue; padding: 10px; margin: 10px;"> | ||
<slot /> | ||
</div> |
7 changes: 7 additions & 0 deletions
7
code/renderers/svelte/template/stories/views/BorderDecoratorProps.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
export let color = 'violet'; | ||
</script> | ||
|
||
<div style="border: 3px solid {color}; padding: 10px; margin: 10px;"> | ||
<slot /> | ||
</div> |
3 changes: 3 additions & 0 deletions
3
code/renderers/svelte/template/stories/views/BorderDecoratorRed.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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div style="border: 3px solid red; padding: 10px; margin: 10px;"> | ||
<slot /> | ||
</div> |
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 |
---|---|---|
|
@@ -15,37 +15,27 @@ | |
props = {}, | ||
/** @type {{[string]: () => {}}} Attach svelte event handlers */ | ||
on, | ||
Wrapper, | ||
WrapperData = {}, | ||
} = storyFn(); | ||
|
||
// reactive, re-render on storyFn change | ||
$: ({ Component, props = {}, on, Wrapper, WrapperData = {} } = storyFn()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe these |
||
$: ({ Component, props = {}, on } = storyFn()); | ||
|
||
const eventsFromArgTypes = Object.fromEntries( | ||
Object.entries(storyContext.argTypes) | ||
.filter(([k, v]) => v.action && props[k] != null) | ||
.map(([k, v]) => [v.action, props[k]]) | ||
); | ||
|
||
const events = { ...eventsFromArgTypes, ...on }; | ||
|
||
if (!Component) { | ||
showError({ | ||
title: `Expecting a Svelte component from the story: "${name}" of "${kind}".`, | ||
description: dedent` | ||
Did you forget to return the Svelte component configuration from the story? | ||
Use "() => ({ Component: YourComponent, data: {} })" | ||
Use "() => ({ Component: YourComponent, props: {} })" | ||
when defining the story. | ||
`, | ||
}); | ||
} | ||
</script> | ||
|
||
<SlotDecorator | ||
decorator={Wrapper} | ||
decoratorProps={WrapperData} | ||
component={Component} | ||
{props} | ||
on={events} | ||
/> | ||
<SlotDecorator {Component} {props} on={{ ...eventsFromArgTypes, ...on }} /> |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work in dev mode with Vite (and maybe with Webpack) because the component is an instance of an empty
ProxyComponent
instead.I asked on the Svelte Discord for alternatives, but we haven't come up with any yet.
https://discord.com/channels/457912077277855764/1046174450749558895/1046174450749558895