writing-react-native-storybook-stories
React Native Storybookのストーリーをコンポーネントストーリーフォーマット(CSF)を使用して作成・編集します。.stories.tsxファイルの作成、React Nativeコンポーネントへのストーリー追加、Storybookアドオン(controls、actions、backgrounds、notes)の設定、argTypes・decorators・parametersのセットアップ、テスト用のポータブルストーリーの操作など、@storybook/react-nativeによるストーリー執筆に関連するあらゆるタスクに対応できます。
description の原文を見る
Create and edit React Native Storybook stories using Component Story Format (CSF). Use when writing .stories.tsx files, adding stories to React Native components, configuring Storybook addons (controls, actions, backgrounds, notes), setting up argTypes, decorators, parameters, or working with portable stories for testing. Applies to any task involving @storybook/react-native story authoring.
SKILL.md 本文
React Native Storybook Stories
@storybook/react-native v10 と Component Story Format (CSF) を使用して React Native コンポーネントのストーリーを記述します。
クイックスタート
最小限のストーリーファイル:
import type { Meta, StoryObj } from '@storybook/react-native';
import { MyComponent } from './MyComponent';
const meta = {
component: MyComponent,
} satisfies Meta<typeof MyComponent>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Basic: Story = {
args: {
label: 'Hello',
},
};
ファイル規約
- 名前:
ComponentName.stories.tsx(コンポーネントと同じディレクトリに配置) MetaとStoryObjを@storybook/react-nativeからインポート- デフォルトエクスポート:
satisfies Meta<typeof Component>を持つmetaオブジェクト - 名前付きエクスポート: UpperCamelCase のストーリー名で
StoryObj<typeof meta>として型付け - props には
argsを使用、コントロール設定にはargTypesを使用、アドオン設定にはparametersを使用 - カスタムレンダー関数には
renderを使用、ラッパーにはdecoratorsを使用
ストーリーパターン
共有 args を持つ複数のストーリー
export const Primary: Story = {
args: { variant: 'primary', title: 'Click me' },
};
export const Secondary: Story = {
args: { ...Primary.args, variant: 'secondary' },
};
カスタムレンダー関数
export const WithScrollView: Story = {
render: (args) => (
<ScrollView>
<MyComponent {...args} />
</ScrollView>
),
};
フック付きレンダー (名前付き関数である必要があります)
export const Interactive: Story = {
render: function InteractiveRender() {
const [count, setCount] = useReducer((s) => s + 1, 0);
return <Counter count={count} onPress={setCount} />;
},
};
アクション (コールバックのモック)
import { fn } from 'storybook/test';
const meta = {
component: Button,
args: { onPress: fn() },
} satisfies Meta<typeof Button>;
または argTypes 経由:
argTypes: { onPress: { action: 'pressed' } },
カスタムストーリー名
export const MyStory: Story = {
storyName: 'Custom Display Name',
args: { label: 'Hello' },
};
カスタムタイトル / ネスティング
const meta = {
title: 'NestingExample/Message/Bubble',
component: MyComponent,
} satisfies Meta<typeof MyComponent>;
コントロール & ArgTypes
コントロール種別の完全なリファレンスについては references/controls.md を参照してください。
よくあるパターン:
const meta = {
component: MyComponent,
argTypes: {
// セレクトドロップダウン
size: {
options: ['small', 'medium', 'large'],
control: { type: 'select' },
},
// 範囲スライダー
opacity: {
control: { type: 'range', min: 0, max: 1, step: 0.1 },
},
// カラーピッカー
color: { control: { type: 'color' } },
// 条件付きコントロール (`advanced` arg が true の場合のみ表示)
padding: { control: 'number', if: { arg: 'advanced' } },
},
} satisfies Meta<typeof MyComponent>;
自動検出: TypeScript プロパティ型は自動的にコントロールにマップされます (string -> テキスト、boolean -> ブール値、ユニオン型 -> セレクト、number -> 数値)。
パラメータ
アドオンパラメータ
parameters: {
// Notes アドオンタブのマークダウンドキュメント
notes: `# MyComponent\nUsage: \`<MyComponent label="hi" />\``,
// Backgrounds アドオン用の背景オプション
backgrounds: {
default: 'dark',
values: [
{ name: 'light', value: 'white' },
{ name: 'dark', value: '#333' },
],
},
},
RN 固有の UI パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
noSafeArea | boolean | 上部セーフエリアのパディングを削除します。これを使用する場合、Storybook がセーフエリアのパディングを提供しなくなるため、コンポーネント自体がセーフエリアを処理する必要があります。SafeAreaView の代わりに useSafeAreaInsets() を使用し、インセットをコンテナの paddingTop/paddingBottom として適用し、スクロール可能なコンテンツの場合は SafeAreaView でラップする代わりに contentContainerStyle パディングを使用します。 |
storybookUIVisibility | 'visible' | 'hidden' | UI の初期表示状態 |
hideFullScreenButton | boolean | フルスクリーン切り替えボタンを非表示にします |
layout | 'padded' | 'centered' | 'fullscreen' | ストーリーコンテナレイアウト |
パラメータはストーリー、メタ (コンポーネント)、またはグローバル (preview.tsx) レベルで設定できます。
デコレータ
ストーリーをプロバイダー、レイアウト、またはコンテキストでラップします:
const meta = {
component: MyComponent,
decorators: [
(Story) => (
<View style={{ alignItems: 'center', justifyContent: 'center', flex: 1 }}>
<Story />
</View>
),
],
} satisfies Meta<typeof MyComponent>;
グローバルデコレータは .rnstorybook/preview.tsx に記述します:
import { withBackgrounds } from '@storybook/addon-ondevice-backgrounds';
import type { Preview } from '@storybook/react-native';
const preview: Preview = {
decorators: [withBackgrounds],
parameters: {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
backgrounds: {
default: 'plain',
values: [
{ name: 'plain', value: 'white' },
{ name: 'dark', value: '#333' },
],
},
},
};
export default preview;
設定
.rnstorybook/main.ts
import type { StorybookConfig } from '@storybook/react-native';
const main: StorybookConfig = {
stories: ['../components/**/*.stories.?(ts|tsx|js|jsx)'],
deviceAddons: [
'@storybook/addon-ondevice-controls',
'@storybook/addon-ondevice-backgrounds',
'@storybook/addon-ondevice-actions',
'@storybook/addon-ondevice-notes',
],
framework: '@storybook/react-native',
};
export default main;
ストーリーグロブは複数ディレクトリ設定用のオブジェクト形式もサポートしています:
stories: [
'../components/**/*.stories.?(ts|tsx|js|jsx)',
{ directory: '../other_components', files: '**/*.stories.?(ts|tsx|js|jsx)' },
],
ポータブルストーリー (テスト)
Jest テストでストーリーを再利用します:
import { render, screen } from '@testing-library/react-native';
import { composeStories } from '@storybook/react';
import * as stories from './Button.stories';
const { Primary, Secondary } = composeStories(stories);
test('renders primary button', () => {
render(<Primary />);
expect(screen.getByText('Click me')).toBeTruthy();
});
// テスト内で args をオーバーライド
test('renders with custom props', () => {
render(<Primary title="Custom" />);
expect(screen.getByText('Custom')).toBeTruthy();
});
単一のストーリーの場合は composeStory を使用します:
import { composeStory } from '@storybook/react';
import meta, { Primary } from './Button.stories';
const PrimaryStory = composeStory(Primary, meta);
Jest セットアップファイルでテスト用のグローバルアノテーションを設定します:
// setup-portable-stories.ts
import { setProjectAnnotations } from '@storybook/react';
import * as previewAnnotations from '../.rnstorybook/preview';
setProjectAnnotations(previewAnnotations);
アドオンの概要
| アドオン | パッケージ | 目的 |
|---|---|---|
| Controls | @storybook/addon-ondevice-controls | props をインタラクティブに編集 |
| Actions | @storybook/addon-ondevice-actions | コンポーネント操作をログ出力 |
| Backgrounds | @storybook/addon-ondevice-backgrounds | ストーリーの背景を変更 |
| Notes | @storybook/addon-ondevice-notes | マークダウンドキュメントを追加 |
ライセンス: MIT(寛容ライセンスのため全文を引用しています) · 原本リポジトリ
詳細情報
- 作者
- soham2008xyz
- ライセンス
- MIT
- 最終更新
- 2026/5/11
Source: https://github.com/soham2008xyz/trade-tycoon / ライセンス: MIT
関連スキル
doubt-driven-development
重要な判断はすべて、本番環境への展開前に新しい視点から対抗的レビューを実施します。速度より正確性が重要な場合、不慣れなコードを扱う場合、本番環境・セキュリティに関わるロジック・取り消し不可の操作など影響度が高い場合、または後でバグを修正するよりも今検証する方が効率的な場合に活用してください。
apprun-skills
TypeScriptを使用したAppRunアプリケーションのMVU設計に関する総合的なガイダンスが得られます。コンポーネントパターン、イベントハンドリング、状態管理(非同期ジェネレータを含む)、パラメータと保護機能を備えたルーティング・ナビゲーション、vistestを使用したテストに対応しています。AppRunコンポーネントの設計・レビュー、ルートの配線、状態フローの管理、AppRunテストの作成時に活用してください。
desloppify
コードベースのヘルスチェックと技術負債の追跡ツールです。コード品質、技術負債、デッドコード、大規模ファイル、ゴッドクラス、重複関数、コードスメル、命名規則の問題、インポートサイクル、結合度の問題についてユーザーが質問した場合に使用してください。また、ヘルススコアの確認、次の改善項目の提案、クリーンアップ計画の作成をリクエストされた際にも対応します。29言語に対応しています。
debugging-and-error-recovery
テストが失敗したり、ビルドが壊れたり、動作が期待と異なったり、予期しないエラーが発生したりした場合に、体系的な根本原因デバッグをガイドします。推測ではなく、根本原因を見つけて修正するための体系的なアプローチが必要な場合に使用してください。
test-driven-development
テスト駆動開発により実装を進めます。ロジックの実装、バグの修正、動作の変更など、あらゆる場面で活用できます。コードが正常に動作することを証明する必要がある場合、バグ報告を受けた場合、既存機能を修正する予定がある場合に使用してください。
incremental-implementation
変更を段階的に実施します。複数のファイルに影響する機能や変更を実装する場合に使用してください。大量のコードを一度に書こうとしている場合や、タスクが一度では完結できないほど大きい場合に活用します。