|
| 1 | +# Storybook |
| 2 | + |
| 3 | +Storybook is a tool for developing and testing UI components in isolation. It provides a sandboxed environment where you can build, view, and test components without running the full Electron application. |
| 4 | + |
| 5 | +## Starting Storybook |
| 6 | + |
| 7 | +```bash |
| 8 | +make storybook |
| 9 | +# or |
| 10 | +bun run storybook |
| 11 | +``` |
| 12 | + |
| 13 | +This will start the Storybook development server at `http://localhost:6006`. |
| 14 | + |
| 15 | +## Building Static Storybook |
| 16 | + |
| 17 | +To build a static version of Storybook that can be deployed: |
| 18 | + |
| 19 | +```bash |
| 20 | +make storybook-build |
| 21 | +# or |
| 22 | +bun run storybook:build |
| 23 | +``` |
| 24 | + |
| 25 | +The output will be in `storybook-static/`. |
| 26 | + |
| 27 | +## Writing Stories |
| 28 | + |
| 29 | +Stories are colocated with their components. For example, `ErrorMessage.tsx` has its stories in `ErrorMessage.stories.tsx` in the same directory. |
| 30 | + |
| 31 | +### Basic Story Structure |
| 32 | + |
| 33 | +```typescript |
| 34 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 35 | +import { MyComponent } from "./MyComponent"; |
| 36 | + |
| 37 | +const meta = { |
| 38 | + title: "Components/MyComponent", |
| 39 | + component: MyComponent, |
| 40 | + parameters: { |
| 41 | + layout: "centered", // or "fullscreen" or "padded" |
| 42 | + }, |
| 43 | + tags: ["autodocs"], // Enables automatic documentation |
| 44 | +} satisfies Meta<typeof MyComponent>; |
| 45 | + |
| 46 | +export default meta; |
| 47 | +type Story = StoryObj<typeof meta>; |
| 48 | + |
| 49 | +export const Default: Story = { |
| 50 | + args: { |
| 51 | + prop1: "value1", |
| 52 | + prop2: "value2", |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +export const Variant: Story = { |
| 57 | + args: { |
| 58 | + prop1: "different value", |
| 59 | + prop2: "another value", |
| 60 | + }, |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +### Component Examples |
| 65 | + |
| 66 | +See the existing stories for reference: |
| 67 | + |
| 68 | +- `src/components/ErrorMessage.stories.tsx` - Simple component with multiple states |
| 69 | +- `src/components/Modal.stories.tsx` - Complex component with children and multiple variants |
| 70 | + |
| 71 | +## Global Styles |
| 72 | + |
| 73 | +Storybook automatically applies the same global styles as the main app: |
| 74 | + |
| 75 | +- Color variables (`GlobalColors`) |
| 76 | +- Font definitions (`GlobalFonts`) |
| 77 | +- Scrollbar styles (`GlobalScrollbars`) |
| 78 | + |
| 79 | +These are configured in `.storybook/preview.tsx`. |
| 80 | + |
| 81 | +## Handling Electron APIs |
| 82 | + |
| 83 | +Some components depend on `window.api` for Electron IPC communication. For these components: |
| 84 | + |
| 85 | +1. **Preferred**: Extract the component logic to accept props instead of calling IPC directly |
| 86 | +2. **Alternative**: Mock the `window.api` object in `.storybook/preview.tsx` |
| 87 | + |
| 88 | +Example mock structure: |
| 89 | + |
| 90 | +```typescript |
| 91 | +window.api = { |
| 92 | + workspace: { |
| 93 | + create: async () => ({ success: true, metadata: { ... } }), |
| 94 | + list: async () => ({ success: true, workspaces: [...] }), |
| 95 | + // ... |
| 96 | + }, |
| 97 | + // ... other IPC channels |
| 98 | +}; |
| 99 | +``` |
| 100 | + |
| 101 | +## Benefits |
| 102 | + |
| 103 | +- **Isolated Development**: Build components without running the full Electron app |
| 104 | +- **Visual Testing**: See all component states at once |
| 105 | +- **Documentation**: Stories serve as living documentation with `autodocs` |
| 106 | +- **Faster Iteration**: Hot reload is faster than Electron rebuilds |
| 107 | +- **Accessibility**: Storybook addons can check accessibility issues |
| 108 | + |
| 109 | +## Configuration |
| 110 | + |
| 111 | +- `.storybook/main.ts` - Main Storybook configuration |
| 112 | +- `.storybook/preview.tsx` - Global decorators and parameters |
| 113 | +- `tsconfig.json` - Includes `.storybook/**/*.ts` for type checking |
| 114 | + |
| 115 | +## Tips |
| 116 | + |
| 117 | +- Keep stories simple and focused on visual states |
| 118 | +- Use Storybook's Controls addon to make props interactive |
| 119 | +- Add multiple stories for different states (loading, error, success, etc.) |
| 120 | +- Use the `tags: ["autodocs"]` option to generate automatic documentation |
0 commit comments