RFC: Stories #30628
Replies: 7 comments 8 replies
-
|
The Storybook team (@shilman, @tmeasday, @ndelangen and myself, among others) would be happy to work out the details for integration with Next.js. In fact we are working on better Next.js integration already, as @mrmckeb is aware. Awesome stuff by the way 🙌 |
Beta Was this translation helpful? Give feedback.
-
|
Amazing RFC @nataliemarleny! How to integrate Next and Storybook is a super popular request in the Storybook ecosystem. Storybook can also be built as a statically via the |
Beta Was this translation helpful? Give feedback.
-
|
This is such a great idea @nataliemarleny . Potentially, no more having to setup this piece of UI infrastructure from scratch every-time when starting a new web project. Thought(s) |
Beta Was this translation helpful? Give feedback.
-
|
How flexible will the directory structure be? I'm currently very happy with using atomic design and colocating stories with the components: |
Beta Was this translation helpful? Give feedback.
-
|
I would suggest also some way of exporting out the component library. I've run into several instances were the components I've wanted to build in Next would need to be used outside of the system. This has lead to many of my component libraries being built outside of Next, losing out on all the awesome Next builder and optimizations. |
Beta Was this translation helpful? Give feedback.
-
|
Awesome RFC! The recording of complex component seems to be, well, the most complex part. I don't like the idea of code annotation much, I don't want references to the testing tools in my code. Maybe the solution could be something a bit more generic, based on common standard? Generating Redux state and network response are the most tedious task I currently suffer from while writing stories, I think you got that perfectly well in this RFC, but it also affects testing. You may want to use this recording pattern for Storybook, Jest, and maybe even involve Cypress (eg getting recordings from e2e tests?). Right now in my apps Jest and Storybook share the same setup, mocked functions, fixture data, TypeScript setup etc., and even if Cypress is a tad different, I also reuse a part of the testing setup (at least mock data, TypeScript config, assertion libraries etc.). Writing a story is very similar to writing a unit test, as Storybook proved by all the recently added features and packages. For the recording of fetch requests, what I tried earlier was using HAR: https://github.com/exogen/node-fetch-har, and it has a next.js version. Coupled with Mock Service Worker it could be awesome. Question: what about TypeScript? Could the generated stories have the right typings? Extracting the props themselves could also be a funny way to generate typed test fixtures. |
Beta Was this translation helpful? Give feedback.
-
|
Now that Next.js 13 has been released and Turbopack is out, I wonder if these plans changed at all. We recently released an experimental package for Next.js projects: @storybook/nextjs and will be working on providing better support for Next.js projects out of the box. We at the Storybook team are still happy to help this integration happen! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Next.js Stories
Authored by: Fouad Matin (@fouadmatin), Natalie Marleny (@nataliemarleny)
Blog post: https://indent.com/blog/next-stories-rfc
Goals
/componentsdirectory similar to/pagesBackground
When working in existing Next.js codebases which would benefit from the addition of a component library, we've found the process of introducing and maintaining a component library to be very involved and time consuming. The authors have worked with Storybook and Styleguidist, which are referred to as "existing solutions" in the following paragraphs.
Creating stories with the existing solutions
Creating stories for the components with the existing solutions is time consuming and tedious. The code describing stories is often repetitive and distracting.
By far the biggest problem of adding an existing stateful component to a component library is handling the data that the component depends on, whether it being: props, data from the API, data from localStorage or any other external source. The process of handling data with the existing solutions adds another level of repetitive and distracting code to the codebase.
Component Driven Development
Component Driven Development (or Component Driven User Interfaces) makes sense as an ideal, but often doesn't work as well in practice, especially when retroactively applied to an already existing codebase. This methodology seems to require from the developers:
Build system of the existing solutions
Existing solutions have a build system which is completely independent from Next.js, meaning that:
next dev)Proposal
The proposal consists of these three sections:
1. Automatically creating stories for components and pages
A Next.js developer should be able to easily create stories for their components regardless of whether the component is:
A component library system that is tightly coupled with Next.js would enable easy creation and maintenance of component libraries for all Next.js applications.
We'll make a distinction between the following React component types:
useStatefor functions orthis.statefor classes)getStaticProps,getServerSideProps,getStaticPathsuseEffectfor functions or incomponentDidMountfor classes)useState+ context API,Redux)We'll call the process of automatically creating stories "recording stories", since it's more precise description of what is happening.
Recording simple stories
For example, let's consider an application with the following page in
pages/index.js:... and the following component in
components/ComponentFoo.js:Next.js can easily hook into all of the data sources (props, Next.js data fetching functions) without any developer configuration, so recording a story for these components could be initiated with starting Next.js in development mode with an additional flag:
This starts the development server on http://localhost:3000. After this the developer visits http://localhost:3000/, which results in Next.js recording these stories for the page and for the component:
in
stories/pages/index/2021-07-22T18:51:13.661Z.jsin
stories/components/ComponentFoo/2021-07-22T18:51:13.661Z.jsWhile the developer is browsing the page, every time any of the component's data sources changes (e.g. props change), all of the affected components and pages get a new story generated for them.
The developer has full freedom to update or delete the recorded stories. Updating the stories would be done for the purpose of removing sensitive data or adding additional metadata (e.g. description). The stories are intended to be version controlled with code.
Recording stories for complex components
Next.js would not be able to automatically recognize all of the instances of external data retrieval other than props and Next.js data fetching functions, so a little bit of annotation of the code from the developer's side would be required.
Next.js would provide the
useStoryDatawrapper hook that would be used to tell Next.js which additional sources of data need to be recorded. For example, if a component fetches data client-side with:The developer would be able to tell Next.js that this data source needs to be recorded with:
useStoryDatahook would behave differently based on how Next.js is executed:next --record-stories, the hook would:useSWRhook withapply()methoduseSWRhook into the appropriate storyuseSWRhooknext stories, the hook would:useSWRhookuseSWRhookuseStoryDatawould just run theapply()method on theuseSWRhook2. Browsing stories and exporting a component library
To serve the component library UI locally so that the developer can browse the existing stories, the developer would run:
The stories would be browsable through the Storybook based UI on http://localhost:6006 and would leverage all of the amazing Storybook features: story search, component documentation and viewing and modification of the mock data.
While serving the component library, Next.js would automatically inject the data from the story recordings into the components and pages. This means that while the developer is viewing the story
stories/pages/index/2021-07-22T18:51:13.661Z.jsin the component library UI,getStaticPropsinpages/index.jswill always return{ propBar: "some value" }.The component library UI would attempt to render every component or the page that has the corresponding
stories/<path>directory and at least one story within that directory. Every story that doesn't throw an error while rendering will be listed and searchable in the UI.3. Next.js and Storybook integration
The Component library UI would leverage the Storybook project. Changes would be required to both Next.js and Storybook to accomplish the desired result. The following changes would be required:
Considerations
next buildis executed. Additionally, stories would be statically generated whennext exportis executed. The developer could choose to opt-out of building and statically generating these stories with a dedicated flag.Thanks
We're grateful to the following people for reviewing drafts of this proposal:
Beta Was this translation helpful? Give feedback.
All reactions