-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Docs: Add FAQ about generic Vue components in CSF Next #33687
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,32 +69,57 @@ Sometimes stories need to define args that aren’t included in the component's | |
| {/* prettier-ignore-end */} | ||
|
|
||
| <IfRenderer renderer="vue"> | ||
| ## Vue specific tips | ||
|
|
||
| Vue has excellent support for TypeScript, and we have done our utmost to take advantage of that in the stories files. For example, consider the following strongly typed Vue 3 single file component (SFC): | ||
| ## Vue specific tips | ||
|
|
||
| ```html | ||
| <script setup lang="ts"> | ||
| defineProps<{ count: number; disabled: boolean }>(); | ||
| Vue has excellent support for TypeScript, and we have done our utmost to take advantage of that in the stories files. For example, consider the following strongly typed Vue 3 single file component (SFC): | ||
|
|
||
| const emit = defineEmits<{ | ||
| (e: 'increaseBy', amount: number): void; | ||
| (e: 'decreaseBy', amount: number): void; | ||
| }>(); | ||
| </script> | ||
| ```html | ||
| <script setup lang="ts"> | ||
| defineProps<{ count: number; disabled: boolean }>(); | ||
|
|
||
| <template> | ||
| <div class="card"> | ||
| {{ count }} | ||
| <button @click="emit('increaseBy', 1)" :disabled="disabled">Increase by 1</button> | ||
| <button @click="$emit('decreaseBy', 1)" :disabled="disabled">Decrease by 1</button> | ||
| </div> | ||
| </template> | ||
| ``` | ||
| const emit = defineEmits<{ | ||
| (e: 'increaseBy', amount: number): void; | ||
| (e: 'decreaseBy', amount: number): void; | ||
| }>(); | ||
| </script> | ||
|
|
||
| <template> | ||
| <div class="card"> | ||
| {{ count }} | ||
| <button @click="emit('increaseBy', 1)" :disabled="disabled">Increase by 1</button> | ||
| <button @click="$emit('decreaseBy', 1)" :disabled="disabled">Decrease by 1</button> | ||
| </div> | ||
| </template> | ||
| ``` | ||
|
|
||
| You can type check SFC files with `vue-tsc` and get editor support in VSCode by installing the official [Vue extension](https://marketplace.visualstudio.com/items?itemName=Vue.volar). | ||
|
|
||
| This setup will add type support for `*.vue` imports to your `*.stories.ts` files, providing the same type safety and autocomplete features. | ||
|
|
||
| [CSF Next](../api/csf/csf-next.mdx) adds support for [Generic Vue components](https://vuejs.org/api/sfc-script-setup.html#generics) (using `<script lang="ts" setup generic="T">`). Simply pass the type parameter directly to the component: | ||
|
Contributor
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 have no idea why GitHub is showing 18 changed files instead of just this one. I merged In any case, I moved your CSF Next FAQ entry to the Writing stories with TypeScript guide, because it only pertains to TypeScript users. |
||
|
|
||
| ```ts title="GenericList.stories.ts" | ||
| import preview from '../.storybook/preview'; | ||
|
|
||
| import GenericList from './GenericList.vue'; | ||
|
|
||
| const meta = preview.meta({ | ||
| // 👇 Pass the type parameter directly | ||
| component: GenericList<{ id: number; name: string }>, | ||
| }); | ||
|
|
||
| export const Example = meta.story({ | ||
| args: { | ||
| items: [{ id: 1, name: 'John Doe' }], | ||
| // 👇 item is correctly typed as { id: number; name: string } | ||
| getLabel: (item) => item.name, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| You can type check SFC files with vue-tsc and get editor support in VSCode by installing the official [Vue](https://marketplace.visualstudio.com/items?itemName=Vue.volar) extension. | ||
| Prior CSF versions suffer from a [long-standing type inference issue](https://github.com/storybookjs/storybook/issues/24238) where the generic type `T` would default to `unknown`. | ||
|
|
||
| This setup will add type support for `*.vue` imports to your `*.stories.ts` files, providing the same type safety and autocomplete features. | ||
| </IfRenderer> | ||
|
|
||
| <IfRenderer renderer="svelte"> | ||
|
|
||
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.
Hyphenate “Vue-specific”.
Minor grammar polish for the heading.
✏️ Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[grammar] ~73-~73: Use a hyphen to join words.
Context: ...*/} ## Vue specific tips Vue has excellent support...
(QB_NEW_EN_HYPHEN)
🤖 Prompt for AI Agents