You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+52-13Lines changed: 52 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -177,7 +177,6 @@ Similar to regular CSF, you can define a meta-level `render`-function, by refere
177
177
import MyComponent from './MyComponent.svelte';
178
178
179
179
const { Story } = defineMeta({
180
-
// @ts-expect-error -- TypeScript does not know this is valid: https://github.com/sveltejs/language-tools/issues/2653
181
180
render: template,
182
181
// 👆 the name of the snippet as defined below (can be any name)
183
182
});
@@ -204,9 +203,6 @@ Stories can still override this default snippet using any of the methods for def
204
203
> [!NOTE]
205
204
> Svelte has the limitation, that you can't reference a snippet from a `<script module>` if it reference any declarations in a non-module `<script>` (whether directly or indirectly, via other snippets). See [svelte.dev/docs/svelte/snippet#Exporting-snippets](https://svelte.dev/docs/svelte/snippet#Exporting-snippets)
206
205
207
-
> [!IMPORTANT]
208
-
> There is currently a bug in the Svelte language tools, which causes TypeScript to error with `TS(2448): Block-scoped variable 'SNIPPET_NAMAE' used before its declaration.`. Until that is fixed, you have to silent it with `//@ts-ignore` or `//@ts-expect-error`. See https://github.com/sveltejs/language-tools/issues/2653
209
-
210
206
#### Custom export name
211
207
212
208
Behind-the-scenes, each `<Story />` definition is compiled to a variable export like `export const MyStory = ...;`. In most cases you don't have to care about this detail, however sometimes naming conflicts can arise from this. The variable names are simplifications of the story names - to make them valid JavaScript variables.
@@ -237,35 +233,78 @@ If for some reason you need to access the [Story context](https://storybook.js.o
237
233
238
234
### TypeScript
239
235
240
-
Story snippets and args can be type-safe when necessary. The type of the args are inferred from the component props passed to `defineMeta`.
236
+
Story template snippets can be type-safe when necessary. The type of the args are inferred from the `component` or `render` property passed to `defineMeta`.
241
237
242
-
You can make your snippets type-safe with the `Args`and `StoryContext`helper types:
238
+
If you're just rendering the component directly without a custom template, you can use Svelte's `ComponentProps` type and `StoryContext`from the addon to make your template snippet type-safe:
243
239
244
240
```svelte
245
241
<script module lang="ts">
246
-
import { defineMeta, type Args, type StoryContext } from '@storybook/addon-svelte-csf';
247
-
// 👆 👆 import those type helpers from this addon -->
242
+
import { defineMeta, type StoryContext } from '@storybook/addon-svelte-csf';
243
+
import { type ComponentProps } from 'svelte';
248
244
249
245
import MyComponent from './MyComponent.svelte';
250
246
251
247
const { Story } = defineMeta({
252
248
component: MyComponent,
253
249
});
250
+
251
+
type Args = ComponentProps<MyComponent>;
254
252
</script>
255
253
256
-
<!-- 👇 use to infer `args` type from the `Story` component -->
If you need to customize the type of the `args`, you can pass in a generic type parameter to `defineMeta` that will override the types inferred from the component:
259
+
If you use the `render`-property to define a custom template that might use custom args, the args will be inferred from the types of the snippet passed to `render`. This is especially useful when you're converting primitive args to snippets:
See [the `Types.stories.svelte` examples](./examples/Types.stories.svelte) on how to use complex types properly.
307
+
269
308
### Legacy API
270
309
271
310
Version 5 of the addon changes the API from v4 in key areas, as described above. However a feature flag has been introduced to maintain support for the `<Template>`-based legacy API as it was prior to v5.
0 commit comments