0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

React(Typescript)で実装したボタン(Button)をStoryBookで検証する方法

Posted at

image.png

「Vite + React (TypeScript) プロジェクト」でStorybookを使ってButtonコンポーネントの動作確認をする流れを、初心者にも分かるようにステップごとに説明します。

🎯 ゴール

1.Vite + React (TypeScript) プロジェクトに Storybook を導入
2.Button コンポーネントの Story(動作サンプル)を作成
3.Storybook 上で見た目やクリック動作を確認

🧩 ステップ1:プロジェクトを作成(まだない場合)

# 新規作成
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm run dev

🧩 ステップ2:Storybook をセットアップ

Storybook 公式が Vite をサポートしています。

npx storybook@latest init

しばし時間がかかります。

実行すると自動的に Vite + React + TS 用に設定してくれます。
(確認メッセージが出たら基本的に Yes でOK)

🧩 ステップ3:Storybook を起動して確認

npm run storybook

🧩 ステップ4:Shadcn の Button を Storybook に登録

Storybook storyファイルを作成します。
Shadcn のボタンはsrc/components/ui/button.tsxにある想定)

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

type ButtonProps = ComponentProps<typeof Button>;

const meta:Meta<typeof Button> = {
    title:'UI/Button',
    component:Button,
    args:{
        children:'Click me',
    },
    argTypes:{
        variant:{
            control:{type:'select'},
            options:['default', 'destructive', 'outline', 'secondary', 'ghost', 'link'],
        },
        size:{
            control:{type:'select'},
            options:['default', 'sm', 'lg', 'icon'],
        },
        onClick:{action:'clicked'},
    },
};

export default meta;
type Story = StoryObj<typeof Button>;

export const Default:Story = {
    args:{
        variant:'default',
        size:'default',
    },
};

export const Destructive:Story = {
    args:{
       variant:'destructive',
       children:'Delete', 
    },
};

export const Outline:Story = {
    args:{
        variant:'outline',
        children:'Outline',
    },
};

export const WithClickAction:Story = {
    args:{
        children:'Click Action',
    },
}

🧩ステップ5:Storybook を起動

npm run storybook
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?