-
Notifications
You must be signed in to change notification settings - Fork 32
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 CSF Stories #1
Conversation
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 is tremendous @j3rem1e -- I'm thrilled you were able to get it working without modifying Storybook core 🙌
@j3rem1e would it be possible, based on your knowledge gained from working on this epically awesome PR here, to actually include a storybook definition within a svelte component in future? Just curious, not even a suggestion. I admit the documentation part of this PR had me initially thinking that would be possible, till I read it more carefully. @j3rem1e you sir are a champion, can't wait to use this. |
@Anthropic I don't think it will be easy to do that. maybe with a preprocessor, but mixin a component and his stories is probably not a good idea :
|
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.
Had a few questions about how this works 😄
.map(([id]) => `export const ${id} = __storiesMetaData.stories[${JSON.stringify(id)}]`) | ||
.join('\n'); | ||
|
||
const codeWithoutDefaultExport = code.replace('export default ', '//export default'); |
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.
What's this for?
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.
In svelte, the component are exported as a default export. this is not valid in CSF, the default export should be the tag.
This line is just a 'cheap' way to transform the js generated by svelte by removing this default export.
|
||
return dedent`${codeWithoutDefaultExport} | ||
const { default: parser } = require('${parser}'); | ||
const __storiesMetaData = parser(${componentName}, ${JSON.stringify(stories)}); |
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.
So the parser is still run dynamically, even though the stories were already parsed?
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.
Yes, the Stories are executed in a context which disable rendering, but collect every args/loaders/parameters
i just checked out this branch and ran
it is working for me with the following patch: diff --git a/src/parser/extract-id.ts b/src/parser/extract-id.ts
index e47f87a..cb54669 100644
--- a/src/parser/extract-id.ts
+++ b/src/parser/extract-id.ts
@@ -4,5 +4,5 @@ export function extractId({ id, name }: { id?: string; name?: string }): string
return id;
}
- return name.replaceAll(/[^a-zA-Z0-9_]/g, '_');
+ return name.replace(/[^a-zA-Z0-9_]/g, '_');
}
|
@betaboon this should be fixed, I removed |
i can confirm that it is fixed (after the force-push) :) thank you |
just fyi: using <script>
import { Meta, Story, Template } from "@storybook/addon-svelte-csf";
import Button from "./Button.svelte";
const meta = {
title: "Example/Button",
component: Button,
argTypes: {
backgroundColor: { control: "color" },
size: {
control: {
type: "inline-radio",
options: ["small", "medium", "large"],
},
},
onClick: { action: "onClick" },
},
};
</script>
<Meta {...meta} />
<Template let:args>
<Button {...args} on:click={args.onClick} />
</Template>
<Story name="Primary" args={{ label: 'Button', primary: true }} />
<Story name="Secondary" args={{ label: 'Button' }} />
<Story name="Large" args={{ label: 'Button', size: 'large' }} />
<Story name="Small" args={{ label: 'Button', size: 'small' }} />
PS: would it be possible to get a pre-release on npm ? :) |
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.
@shilman I'm inclined to say this is good-to-go unless you have any feedback on my couple of comments.
Sorry just seeing the notification now--will review in the morning 🙏 |
@@ -0,0 +1,25 @@ | |||
const svelte = require('svelte/compiler'); | |||
|
|||
const parser = require.resolve('./parser/collect-stories').replace(/[/\\]/g, '/'); |
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.
Why [/\\]
and not just [\\]
?
Implementation of a svelte story format