Picture of the article

Put an end to manual Storybook Story creation

- 1 min read

StoryBook 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 :


_14
import type { Meta, StoryObj } from "@storybook/react";
_14
import Login from "./login";
_14
_14
const meta: Meta<typeof Login> = {
_14
title: "organisms/Login",
_14
component: Login,
_14
tags: ["autodocs"],
_14
};
_14
_14
export default meta;
_14
_14
type Story = StoryObj<typeof Login>;
_14
_14
export const Empty: Story = {};

Create the custom snippet

  1. Open VsCode
  2. Open the command palette (Ctrl + Shift + P)
  3. Type "snippet" and select "Preferences: Configure User Snippets"
  4. Select "New Global Snippets file"
  5. Name it "story"
  6. 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!


Picture of the author

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).