Repository navigation
[RFC] Experimental (labs) Vue Native CSF (.stories.vue)
#37542
Replies: 1 comment 3 replies
|
Thanks for the RFC @huang-julien ! As the maintainer of sb-addon-vue-csf, I'm glad to see Vue native CSF heading to core. The authoring API lines up closely with ours, and 1. Compile strategy. We parse the SFC with 2. Extraction mount context. 3. Vitest / portable stories. This deserves its own section. Which 4. Static-only props. Hope these helps! |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
This RFC propose a new format to write stories in Vue, based on Svelte Native CSF.
Users would write
.stories.vuefiles where they'd declare the meta using CSF v4 which returns a<Story>and<Template>.Problem Statement
This is how a Vue Storybook user would write stories:
.vuelike Svelte. It is not how Vue is usually written. Returning component definition from a render function is an advanced case in Vue.unsafe-evalif CSP is applied and is bigger than the runtime bundle.Non-goals
.stories.tsfiles stay fully supported and a project should be able to mix both formats.Implementation
In short: let's mirror what Svelte has done. Implement Vue native CSF with CSF v4.
User facing API
This is what users would write:
In a .stories.vue files,
preview.meta()would return 2 additionnal components:<Tempalte>and<Story><Story>Each
<Story>takes the story annotations as props: name, args, play, tags, parameters, decorators, loaders, and so on.<Story>Children would work that way:#templateslot takes over rendering. It receives { args, context } and can use anything from<script setup>. It's basically thetemplate: stringfrom the render function in the current TS shapeextend="Other story"inherits args, annotations, slot content and the play function from another story in the file, like story.extend() in CSF<Template>Template set the default template for all stories, it's the equivalent of passing a snippet to meta render in Svelte CSF
Rules
Storybook reads parts of a stories file without running it, so some things must be static.
preview.meta()call per file, at the top level of<script setup>. It works like a Vue macro: it can't reference variables declared in<script setup>, same rule asdefinePropsanddefineOptions. Imports are fine.<Story>nor<Template>.name,export-name,extendandtagson a<Story>, andcomponent,titleandtagsin the meta. Anything else, including a bound:tags, is a compile error.<script>block can be added next to<script setup>for constants or state the meta needs. It can't export anything.How it would work
Build time
Before the Vue compiler runs, we move the
preview.meta()call out of<script setup>into module-level code.This is safe because the call has nothing local to depend on -> it's the macro rule. The Vue compiler then compiles the file as usual:
The meta is now plain module code, like in CSF.
setup()only holds per-story code.We touch the file in three places:
preview.meta()call to module level.<Story>we found in the template.Basically, the
.stories.vuefile will look like:Runtime
There's a helper as you've seen from the example above:
__vueCsf_createRuntimeStory.The first time it is called for a file, it mounts the component off screen:
setup()runs.<Story>gives its props to the helper instead of rendering.The next calls for the same file reuse that result, so the script runs once here, not once per story.
A few things to know about this first mount:
<script setup>is mounted inside<Suspense>.setup(app)in the preview (Pinia, the router, i18n) is available.<Story>only says "here are my props" and stops there.When this helper has ran, the next variable will get their values. example:
And at this point in runtime, Storybook would see the module as a "normal" CSF
Storybook's preview calls Counter’s render, which mounts the same component again, this time inside a container that says “the active story is Counter”.
<Button label="Button" @click="count++">Clicked 0</Button><Story>rendered 'on-screen' then checks its ownargs. If they differ from what the first mount read, it sends them to Storybook, which re-renders the story with them. So state or a callback passed inargsis the one from the story you see, and stays reactive. Svelte CSF does the same.play function :
The play function used is the clickTwice from the instance on screen, not the one captured. The double click therefore increments the count the button displays, and the story shows “Clicked 2”.
Testing
Stories written this way run in the Vitest addon like any other story. What we generate looks like a regular CSF file and our transform will run before Vitests.
The play function that runs is the one from the story on screen. So if your play function clicks a button and your markup shows a counter, they are talking about the same counter.
**
composeStoriesshould work too ** because native CSf still exports ordinary stories. For types, TypeScript doesn't know yet that a.stories.vuefile exports stories, so we can plan a small plugin for Vue's language tools that declares them.Deliverables
templatestring at built-time tooRisks
argsare shared between stories the same way they are in a.stories.tsfile today. If one story mutates a list passed as an arg, the others see the change.Its
beforeEach, loaders and decorators are module code, like in a.stories.tsfile. State they need to share with the stories goes in a plain<script>block, and is then shared by every story in the file.<Story>are read during the first off-screen mount, and the story you see is a second mount with its own state.argsare re-sent from the story on screen, and the play function runs there too. But abeforeEach,loadersordecoratorsprop that changes a<script setup>variable changes the off-screen copy, not the one on screen. Svelte CSF has the same limit.argsuse script state, the story renders one extra time right after mounting, because of the args sync.Unresolved Questions
<docs>within the same CSF filedefineMetawhich would compile into CSF3 ? -> NObeforeEachandloaderson a<Story>run against the story on screen? It would be possible by creating the story's instance before Storybook's loaders run, but the<script setup>would then run before any loader orbeforeEach.Alternatives considered / Abandoned Ideas
Compilation output
sb-addon-vue-csfdoes). Simpler, and nothing is mounted up front. But it only works for plain values: you can't pass a function, a spy, or anything declared in<script setup>as a prop or in the meta.<script setup>as plain module code. The easiest to build. But arefwould then be shared by every story in the file, vue hooks would not run and shared ref in addon-docs would cause leaks between stories<Story>into its own small component. The file becomes a normal CSF module and nothing is mounted up front. But props and the meta could then only use what is declared in a normal<script>block, so almost every file with a play function would need two script blocks.<script setup>or<script>In Vue a plain <script> on its own does not expose Story to the template, so every file would need both blocks and this would be a huge DX issue.
Leave
preview.meta()insidesetup()and capture it during the first mount. The meta could then use anything from<script setup>, but itsbeforeEach, loaders and decorators silently acted on the first mount's state instead of the story on screen.Runtime render
injectwhat existed when it was created, so decorators would not reach the story or the component under testRelease strategy
The implementation of this RFC will be released as an addon within
storybookjs/labsto:Long term goal (if this goes into core)
This RFC proposes a Vue CSF addon that mirrors Svelte CSF. Once it is implemented, feedback from both Vue and Svelte users may shape Storybook towards native support for framework SFCs.
Right now, we adapt SFCs and their compilation to Storybook, instead of making Storybook accept this kind of input as stories. That is why the stories file is mounted twice, and why a single mount comes with limitations.
For example, if Storybook could accept a story's args, parameters and decorators when the story renders, instead of when the file is imported, the first mount would not be needed.
Prior Art
sb-addon-vue-csfhas a very similar authoring format but emits CSF v3storybook-vue-addonwraps stories in a element that carries the meta as attributes..story.vuefiles and is the tool Vue users most often compare Storybook with on this point.Differences with Svelte CSF
On the <script> :
Vue and Svelte both have this <script> tags, Vue CSF is the reverse of what Svelte does.
In Svelte → <script> = story level code. <script module> = module level code
In vue → <script setup> = story level code. <script> = module level code
On defineMeta :
The RFC doesn’t propose any defineMeta , we’re going straight with CSF v4
On the “default” render:
Svelte use a {#snippet template} , this RFC would sugest to have preview.meta return a Template component
On slots:
This RFC makes accept any slot which would be forwarded to the actual rendered component.
All reactions