Skip to content

Commit 96f28d6

Browse files
committed
fix(generator): add asChild prop to Svelte stories to prevent double-wrapping
The addon-svelte-csf v5+ wraps story content in the meta component by default. Adding asChild to Story components tells Storybook to use children directly without the automatic component wrapper. This fixes the visual double-nesting issue where: - <Card><Card class="max-w-sm">...</Card></Card> - <Button><Button color="primary">...</Button></Button> Now generates correctly with asChild: - <Story name="Default" asChild><Card>...</Card></Story>
1 parent 409a154 commit 96f28d6

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

story-generator/framework-adapters/svelte-adapter.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ The file must use this EXACT structure with defineMeta():
5555
});
5656
</script>
5757
58-
<Story name="Default">
58+
<Story name="Default" asChild>
5959
<ComponentName>Default Story</ComponentName>
6060
</Story>
6161
62-
<Story name="Variant">
62+
<Story name="Variant" asChild>
6363
<ComponentName color="primary">Primary Variant</ComponentName>
6464
</Story>
6565
\`\`\`
@@ -80,19 +80,20 @@ CRITICAL RULES FOR addon-svelte-csf v5+:
8080
- Story title MUST start with "Generated/" (e.g., title: 'Generated/Button')
8181
- Include tags: ['autodocs'] in defineMeta
8282
- Each <Story> needs a unique name attribute
83+
- ALWAYS add asChild prop to <Story> to prevent double-wrapping: <Story name="X" asChild>
8384
- Put component content BETWEEN the component tags, not in props
8485
- DO NOT use 'children' or 'slot' props
8586
- For buttons and components that display text, put text between opening and closing tags
8687
8788
CORRECT EXAMPLES:
8889
\`\`\`svelte
89-
<!-- Button with text content -->
90-
<Story name="Primary">
90+
<!-- Button with text content - asChild prevents double-wrapping -->
91+
<Story name="Primary" asChild>
9192
<Button color="primary">Click Me</Button>
9293
</Story>
9394
9495
<!-- Multiple components in a story -->
95-
<Story name="Colors">
96+
<Story name="Colors" asChild>
9697
<div class="flex gap-2 flex-wrap">
9798
<Button color="primary">Primary</Button>
9899
<Button color="blue">Blue</Button>
@@ -101,14 +102,14 @@ CORRECT EXAMPLES:
101102
</Story>
102103
103104
<!-- Card with content -->
104-
<Story name="Card Example">
105+
<Story name="Card Example" asChild>
105106
<Card class="max-w-sm">
106107
<h5 class="text-2xl font-bold tracking-tight text-gray-900">Card Title</h5>
107108
<p class="font-normal text-gray-700">Card description here.</p>
108109
</Card>
109110
</Story>
110111
111-
<!-- Using args with snippets (Svelte 5 style) -->
112+
<!-- Using args with snippets (Svelte 5 style) - no asChild needed with snippets -->
112113
<Story name="WithArgs" args={{ color: 'primary', size: 'lg' }}>
113114
{#snippet children(args)}
114115
<Button {...args}>Button with Args</Button>

story-generator/selfHealingLoop.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ function getFrameworkSpecificInstructions(framework: string, importPath: string)
213213
instructions.push('2. Import defineMeta: `import { defineMeta } from "@storybook/addon-svelte-csf";`');
214214
instructions.push(`3. Import components: \`import { ComponentName } from "${importPath}";\``);
215215
instructions.push('4. Destructure Story from defineMeta: `const { Story } = defineMeta({ title: "...", component: ... });`');
216-
instructions.push('5. Use `<Story name="StoryName">` components (NOT `export const StoryName`)');
217-
instructions.push('6. Close the script tag properly: `</script>`');
216+
instructions.push('5. Use `<Story name="StoryName" asChild>` components (NOT `export const StoryName`)');
217+
instructions.push('6. ALWAYS add asChild prop to Story to prevent double-wrapping: `<Story name="X" asChild>`');
218+
instructions.push('7. Close the script tag properly: `</script>`');
218219
instructions.push('');
219220
instructions.push('**FORBIDDEN in Svelte stories:**');
220221
instructions.push('- `export const meta = { ... }` (old CSF format)');
@@ -237,7 +238,7 @@ function getFrameworkSpecificInstructions(framework: string, importPath: string)
237238
instructions.push(' });');
238239
instructions.push('</script>');
239240
instructions.push('');
240-
instructions.push('<Story name="Default">');
241+
instructions.push('<Story name="Default" asChild>');
241242
instructions.push(' <Button>Click Me</Button>');
242243
instructions.push('</Story>');
243244
instructions.push('```');

0 commit comments

Comments
 (0)