Difficulty Level: Advanced
Prerequisite: You should be familiar with Storybook and using storybook tool for components documentation
Takeaways: This article helps to understand, how to create stories for both HTML components and Vue.js components in same storybook project
Why StoryBook?
We use Storybook as a tool for creating and showcasing UI components. It provides a structured environment for developers to build and document the components. In this article you learn about adding custom tags to stories.
Our product uses both HTML components and Vue components.
"Can we create storybook documentation for both HTML components and Vue components in the same application?"
Answer is "Yes"
In this article you learn how to create stories for both HTML and Vue components in same storybook application.
Let's dive into configuring storybook for Vue. Setting up Storybook with Vue is a straightforward process.
Begin by installing Storybook using the command:
|
|
This initializes a Storybook project specifically configured for Vue. Next, create a .storybook directory in your project's root, and within it, configure the main.js file to include Vue-specific settings. Utilize the stories directory to organize component stories. With these steps, you'll have a robust Storybook environment tailored for Vue, empowering you to showcase components documentation.
Structuring the Project
To distinguish the HTML and Vue components documentation, create 2 different parent directories inside stories directory, the final stories directory tree looks like the below image.
The below image is give with example for button component developed in both HTML and Vue
Writing HTML component
Filename -> storybook-project/stories/
html_component_docs/Button/
component.js
|
|
Note: Since we installed storybook-vue, HTML components will not render in storybook documentation, So, before writing story for HTML component component, we need a helper to convert HTML component to Vue component for documentation rendering
Install below npm library
|
|
Add helper file
Filename -> storybook-project/stories/
helpers/prettifyHtml.js
|
|
Writing story for HTML component
Filename -> storybook-project/stories/
html_component_docs/Button/demo.stories.js
|
|
Other than props defined in demo.stories.js, you can also add additional information like list of event emitters, styles overriding options, slots, design details about Button component in separate story in markdown format named overview.stories.mdx
Filename -> storybook-project/stories/html_component_docs/
Button/overview.stories.mdx
|
|
Writing Vue component
Filename -> storybook-project/vue_components/src/
components/Button.vue
|
|
Writing story for Vue component
Writing story for Vue component doesn't require any helper, since its a direct Vue component which can render in story documentation
Filename -> storybook-project/stories/vue_component_docs/
Button/demo.stories.js
|
|
Other than props defined in demo.stories.js, you can also add additional information like list of event emitters, styles overriding options, slots, design details about Vue Button component in separate story in markdown format named overview.stories.mdx
|
|
Note: To view source code of both HTML and Vue component, it requires additional script to configure in .storybook directory.
Filename: .storybook/withSource.js
|
|
Import above script in .storybook/preview.js
|
|
Conclusion
In conclusion, maintaining HTML and Vue components within the same Storybook project offers a versatile and efficient approach to component development. By following the guidelines outlined in this comprehensive guide, developers can seamlessly integrate and showcase both types of components, fostering a cohesive and collaborative development environment
If you need help creating Storybook components or custom tags, don't hesitate to reach out! Whether you're looking to build bulletproof UI components or tailor your design system, our team is ready to assist you. Let's work together to bring your ideas to life — contact us today!
Written by:
Chandra Sekar
Senior Frontend Developer
Chandra is an exceptional Vue.js frontend developer who consistently delivers outstanding results. With a keen eye for detail and a deep understanding of Vue.js, Chandra transforms design concepts into seamless and visually captivating user interfaces.
Read more like this:
Chandra Sekar
Mastering Storybook: A Comprehensive Guide on Adding Custom Tags to Storybook
Ewelina Zwolenik
What is vuejs Transition component and how to use it?
Chandra Sekar
How to Effectively Mock API Calls using Vue.js and Axios
Ewelina Zwolenik
How to Test VueRouter With Vitest
Got idea?Let’s talk about your project
Schedule a call with us