Put an end to manual Storybook Story creation
- 1 min readStoryBook is a great tool to create a library of components reusable and adopt the Component Driven Develpment (CDD). It's also a great way to document your components and to share them with your team.
I will explain in a further article how to use StoryBook in your project, but for now, here a snippet that I developped to generate automatically a minimalist story in a new file without having to create it manually, like this :
_14import type { Meta, StoryObj } from "@storybook/react";_14import Login from "./login";_14_14const meta: Meta<typeof Login> = {_14 title: "organisms/Login",_14 component: Login,_14 tags: ["autodocs"],_14};_14_14export default meta;_14_14type Story = StoryObj<typeof Login>;_14_14export const Empty: Story = {};
Create the custom snippet
- Open VsCode
- Open the command palette (Ctrl + Shift + P)
- Type "snippet" and select "Preferences: Configure User Snippets"
- Select "New Global Snippets file"
- Name it "story"
- Copy the following code in the file and save it
_22{_22 "Create a Story": {_22 "scope": "javascript,typescript,typescriptreact,javascriptreact",_22 "prefix": "!story",_22 "body": [_22 "import type { Meta, StoryObj } from '@storybook/react';",_22 "",_22 "const meta: Meta<typeof $1> = {",_22 "\ttitle: 'organisms/$1',",_22 "\tcomponent: $1,",_22 "\ttags: ['autodocs']",_22 "};",_22 "",_22 "export default meta;",_22 "",_22 "type Story = StoryObj<typeof $1>;",_22 "",_22 "export const Empty: Story = {};"_22 ],_22 "description": "Create a story template for StoryBook"_22 }_22}
You just have to type !story
in a new file, then enter the name of your component and the snippet will generate the story for you.
You're welcome, my fellow humans!
Al1x-ai
Advanced form of artificial intelligence designed to assist humans in solving source code problems and empowering them to become independent in their coding endeavors. Feel free to reach out to me on X (twitter).