|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# Configuration |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +- [Configuration](#configuration) |
| 10 | + - [Table of Contents](#table-of-contents) |
| 11 | + - [Configuration Files](#configuration-files) |
| 12 | + - [main.ts](#maints) |
| 13 | + - [Options](#options) |
| 14 | + - [preview.tsx](#previewtsx) |
| 15 | + - [Notable Parameters](#notable-parameters) |
| 16 | + - [index.tsx](#indextsx) |
| 17 | + - [Available Options](#available-options) |
| 18 | + |
| 19 | +## Configuration Files |
| 20 | + |
| 21 | +### main.ts |
| 22 | + |
| 23 | +The `main.ts` file is your primary configuration entry point, located in the `.storybook` directory. |
| 24 | + |
| 25 | +```typescript |
| 26 | +import { StorybookConfig } from '@storybook/react-native'; |
| 27 | + |
| 28 | +const main: StorybookConfig = { |
| 29 | + stories: [ |
| 30 | + // Simple glob pattern |
| 31 | + '../components/**/*.stories.?(ts|tsx|js|jsx)', |
| 32 | + |
| 33 | + // Directory configuration with title prefix |
| 34 | + { |
| 35 | + directory: '../packages/ui', |
| 36 | + titlePrefix: 'UI', |
| 37 | + files: '**/*.stories.?(ts|tsx|js|jsx)', |
| 38 | + }, |
| 39 | + ], |
| 40 | + |
| 41 | + addons: [ |
| 42 | + '@storybook/addon-ondevice-controls', |
| 43 | + '@storybook/addon-ondevice-backgrounds', |
| 44 | + '@storybook/addon-ondevice-actions', |
| 45 | + '@storybook/addon-ondevice-notes', |
| 46 | + ], |
| 47 | +}; |
| 48 | + |
| 49 | +export default main; |
| 50 | +``` |
| 51 | + |
| 52 | +#### Options |
| 53 | + |
| 54 | +- `stories`: Array of story patterns or configuration objects |
| 55 | + - `directory`: Base directory for stories |
| 56 | + - `titlePrefix`: Optional prefix for story titles |
| 57 | + - `files`: Glob pattern for story files |
| 58 | +- `addons`: Array of addon packages to include |
| 59 | +- `reactNative.playFn`: Enable/disable story play functions |
| 60 | + |
| 61 | +### preview.tsx |
| 62 | + |
| 63 | +The `preview.tsx` file configures the story rendering environment and global parameters. |
| 64 | + |
| 65 | +```typescript |
| 66 | +import { Preview } from '@storybook/react'; |
| 67 | +import { View } from 'react-native'; |
| 68 | +import { withBackgrounds } from '@storybook/addon-ondevice-backgrounds'; |
| 69 | + |
| 70 | +const preview: Preview = { |
| 71 | + decorators: [ |
| 72 | + // Global wrapper for all stories |
| 73 | + (Story) => ( |
| 74 | + <View style={{ padding: 8 }}> |
| 75 | + <Story /> |
| 76 | + </View> |
| 77 | + ), |
| 78 | + |
| 79 | + // backgrounds addon decorator |
| 80 | + withBackgrounds, |
| 81 | + ], |
| 82 | + |
| 83 | + parameters: { |
| 84 | + // story sorting configuration, should only be configured in preview file. |
| 85 | + options: { |
| 86 | + storySort: { |
| 87 | + method: 'alphabetical', |
| 88 | + includeNames: true, |
| 89 | + order: ['Components', 'Examples'], |
| 90 | + }, |
| 91 | + }, |
| 92 | + |
| 93 | + // UI Configuration |
| 94 | + hideFullScreenButton: false, |
| 95 | + noSafeArea: false, |
| 96 | + |
| 97 | + // Background Configuration |
| 98 | + backgrounds: { |
| 99 | + default: 'light', |
| 100 | + values: [ |
| 101 | + { name: 'light', value: 'white' }, |
| 102 | + { name: 'dark', value: '#333' }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + |
| 106 | + // your own parameters |
| 107 | + my_param: 'anything', |
| 108 | + }, |
| 109 | +}; |
| 110 | + |
| 111 | +export default preview; |
| 112 | +``` |
| 113 | + |
| 114 | +#### Notable Parameters |
| 115 | + |
| 116 | +Other than story sort the other parameters can be overwritten per story. |
| 117 | + |
| 118 | +- `parameters.options.storySort`: Story sorting configuration |
| 119 | +- `parameters.hideFullScreenButton`: Toggle fullscreen button visibility |
| 120 | +- `parameters.noSafeArea`: Disable safe area insets |
| 121 | +- `parameters.backgrounds`: Background addon configuration |
| 122 | + |
| 123 | +### index.tsx |
| 124 | + |
| 125 | +The entry point file configures the Storybook UI and runtime behavior. |
| 126 | + |
| 127 | +```typescript |
| 128 | +import { view } from './storybook.requires'; |
| 129 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 130 | + |
| 131 | +const StorybookUIRoot = view.getStorybookUI({ |
| 132 | + // UI Configuration |
| 133 | + onDeviceUI: true, |
| 134 | + shouldPersistSelection: true, |
| 135 | + |
| 136 | + // Theme Customization |
| 137 | + theme: { |
| 138 | + // theme documentation coming soon, for now use types to understand the available options |
| 139 | + }, |
| 140 | + |
| 141 | + // Story Selection |
| 142 | + initialSelection: { |
| 143 | + kind: 'Components/Button', |
| 144 | + name: 'Primary', |
| 145 | + }, |
| 146 | + |
| 147 | + // Storage Configuration |
| 148 | + storage: { |
| 149 | + getItem: AsyncStorage.getItem, |
| 150 | + setItem: AsyncStorage.setItem, |
| 151 | + }, |
| 152 | + |
| 153 | + // Websocket Options |
| 154 | + enableWebsockets: false, |
| 155 | + host: 'localhost', |
| 156 | + port: 7007, |
| 157 | + secured: false, |
| 158 | + query: '', |
| 159 | +}); |
| 160 | + |
| 161 | +export default StorybookUIRoot; |
| 162 | +``` |
| 163 | + |
| 164 | +#### Available Options |
| 165 | + |
| 166 | +- **UI Options** |
| 167 | + |
| 168 | + - `onDeviceUI`: Enable/disable on-device UI (default: true) |
| 169 | + - `shouldPersistSelection`: Persist last viewed story (default: true) |
| 170 | + |
| 171 | +- **Theme Options** |
| 172 | + |
| 173 | + - `theme`: Customize UI theme and branding |
| 174 | + |
| 175 | +- **Story Selection** |
| 176 | + |
| 177 | + - `initialSelection`: Set initial story to display |
| 178 | + - String format: `'kind--story'` |
| 179 | + - Object format: `{ kind: string, name: string }` |
| 180 | + |
| 181 | +- **Storage Options** |
| 182 | + |
| 183 | + - `storage`: Implementation for story persistence |
| 184 | + - `getItem`: Function to retrieve stored values |
| 185 | + - `setItem`: Function to store values |
| 186 | + |
| 187 | +- **Websocket Options** |
| 188 | + - `enableWebsockets`: Enable remote control (default: false) |
| 189 | + - `host`: Websocket host (default: 'localhost') |
| 190 | + - `port`: Websocket port (default: 7007) |
| 191 | + - `secured`: Use WSS protocol (default: false) |
| 192 | + - `query`: Additional query parameters |
0 commit comments