Storybook

From G1Joshi/Agent-Skills · View interactive version

Storybook is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Storybook component workshop assistance covering CSF 3 stories, args, controls, and visual regression. Use when developing isolated UI components, building design systems, or documenting frontend widgets.

How to use this skill

Skill file

Storybook

Storybook is a frontend workshop for building UI components and pages in isolation. It enables you to develop UI components without running your entire app (and logic/APIs).

When to Use

Quick Start

npx storybook@latest init
npm run storybook

Snippet:

// Button.stories.tsx
import type { Meta, StoryObj } from "@storybook/react";
import { Button } from "./Button";

const meta: Meta<typeof Button> = {
  component: Button,
};
export default meta;

type Story = StoryObj<typeof Button>;

export const Primary: Story = {
  args: {
    primary: true,
    label: "Button",
  },
};

Core Concepts

Component Story Format (CSF 3)

Defines component variants using declarative, typed JavaScript exports:

// src/components/Button.stories.tsx
import type { Meta, StoryObj } from "@storybook/react";
import { Button } from "./Button";

const meta: Meta<typeof Button> = {
  title: "Components/Button",
  component: Button,
  tags: ["autodocs"],
  argTypes: {
    variant: { control: "select", options: ["primary", "secondary", "danger"] },
    onClick: { action: "clicked" },
  },
};
export default meta;

type Story = StoryObj<typeof Button>;

export const Primary: Story = {
  args: {
    variant: "primary",
    label: "Confirm Payment",
  },
};

export const Disabled: Story = {
  args: {
    variant: "primary",
    label: "Processing...",
    disabled: true,
  },
};

Component Interaction Testing (`play` Function)

Simulates user interactions inside the story using Testing Library and asserts on outcomes:

import { within, userEvent, expect } from "@storybook/test";

export const InteractiveLoginForm: Story = {
  play: async ({ canvasElement }) => {
    const canvas = within(canvasElement);
    await userEvent.type(canvas.getByLabelText(/email/i), "alice@example.com");
    await userEvent.type(canvas.getByLabelText(/password/i), "Secret123!");
    await userEvent.click(canvas.getByRole("button", { name: /log in/i }));

    await expect(canvas.getByText(/dashboard/i)).toBeInTheDocument();
  },
};

Mocking API Calls with MSW Addon

Intercepts component network requests inside the Storybook canvas:

import { http, HttpResponse } from "msw";

export const LoadedUserProfile: Story = {
  parameters: {
    msw: {
      handlers: [
        http.get("/api/user", () =>
          HttpResponse.json({ name: "Alice", role: "Admin" }),
        ),
      ],
    },
  },
};

Common Patterns

Interactive Story with Controls and Play Function

Problem: Static component stories do not verify whether clicks and state transitions function properly.

Solution:

Use CSF 3 with args and play function testing:

import type { Meta, StoryObj } from "@storybook/react";
import { userEvent, within, expect } from "@storybook/test";
import { LoginForm } from "./LoginForm";

const meta: Meta<typeof LoginForm> = {
  title: "Components/LoginForm",
  component: LoginForm,
};
export default meta;

type Story = StoryObj<typeof LoginForm>;

export const ValidSubmission: Story = {
  play: async ({ canvasElement }) => {
    const canvas = within(canvasElement);
    await userEvent.type(canvas.getByLabelText(/email/i), "user@test.com");
    await userEvent.type(canvas.getByLabelText(/password/i), "secret123");
    await userEvent.click(canvas.getByRole("button", { name: /submit/i }));
    await expect(canvas.getByText(/welcome/i)).toBeInTheDocument();
  },
};

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------------ | :--------------------------------------------------------------------- | :----------------------------------------------------------------------- |

| Cannot find module in .storybook/preview | Path alias or CSS import not recognized by Storybook builder. | Add path aliases to .storybook/main.ts webpackFinal or viteFinal. |

| No stories found | Stories glob pattern in main.ts does not match component file paths. | Verify stories: ['../src/**/*.stories.@(js\|jsx\|ts\|tsx)'] in config. |

| Component won't render: hook called outside component | Decorator missing required React context or router provider. | Wrap story with appropriate context provider in preview decorators. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.