• Stars
    star
    942
  • Rank 46,680 (Top 1.0 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ““ Storybook for React Native!

Storybook for React Native

With Storybook for React Native you can design and develop individual React Native components without running your app.

This readme is for the 6.5 version, find the 5.3 readme here

For more information about storybook visit: storybook.js.org

NOTE: @storybook/react-native requires atleast 6.5.14, please set other storybook packages (like @storybook/addons) to ^6.5.14 or newer

If you want to help out or are just curious then check out the project board to see the open issues related to v6+.

picture of storybook

Pictured is from the template mentioned in getting started

Table of contents

Getting Started

New project

There is some project boilerplate with @storybook/react-native and @storybook/addons-react-native-web both already configured with a simple example.

For expo you can use this template with the following command

# With NPM
npx create-expo-app --template expo-template-storybook AwesomeStorybook

# With Yarn
yarn create expo-app --template expo-template-storybook AwesomeStorybook

# With pnpm
pnpm create expo-app --template expo-template-storybook AwesomeStorybook

For react native cli you can use this template

npx react-native init MyApp --template react-native-template-storybook

Existing project

Run init to setup your project with all the dependencies and configuration files:

npx sb@latest init --type react_native

The only thing left to do is return Storybook's UI in your app entry point (such as App.js) like this:

export { default } from './.storybook';

If you want to be able to swap easily between storybook and your app, have a look at this blog post

If you want to add everything yourself check out the the manual guide here.

Additional steps: Update your metro config

We use the sbmodern resolver field in order to resolve the modern version of storybook packages. Doing this removes the polyfills that ship in commonjs modules and fixes multiple long standing issues such as the promises never resolving bug and more (caused by corejs promises polyfill).

Expo

First create metro config file if you don't have it yet.

npx expo customize metro.config.js

Then add sbmodern to the start of the resolver.resolverMainFields list.

// metro.config.js

const { getDefaultConfig } = require('expo/metro-config');

--module.exports = getDefaultConfig(__dirname);
++const defaultConfig = getDefaultConfig(__dirname);

++defaultConfig.resolver.resolverMainFields.unshift('sbmodern');

++module.exports = defaultConfig;

React native

module.exports = {
  /* existing config */
  resolver: {
    resolverMainFields: ['sbmodern', 'react-native', 'browser', 'main'],
  },
};

Writing stories

In v6 you can use the CSF syntax that looks like this:

import { MyButton } from './Button';

export default {
  title: 'components/MyButton',
  component: MyButton,
};

export const Basic = (args) => <MyButton {...args} />;

Basic.args = {
  text: 'Hello World',
  color: 'purple',
};

You should configure the path to your story files in the main.js config file from the .storybook folder.

// .storybook/main.js

module.exports = {
  stories: ['../components/**/*.stories.?(ts|tsx|js|jsx)'],
  addons: [],
};

Decorators and Parameters

For stories you can add decorators and parameters on the default export or on a specifc story.

export default {
  title: 'Button',
  component: Button,
  decorators: [
    (Story) => (
      <View style={{ alignItems: 'center', justifyContent: 'center', flex: 1 }}>
        <Story />
      </View>
    ),
  ],
  parameters: {
    backgrounds: {
      values: [
        { name: 'red', value: '#f00' },
        { name: 'green', value: '#0f0' },
        { name: 'blue', value: '#00f' },
      ],
    },
  },
};

For global decorators and parameters, you can add them to preview.js inside your .storybook folder.

// .storybook/preview.js

import { withBackgrounds } from '@storybook/addon-ondevice-backgrounds';
export const decorators = [
  withBackgrounds,
  (Story) => (
    <View style={{ flex: 1, color: 'blue' }}>
      <Story />
    </View>
  ),
];
export const parameters = {
  backgrounds: {
    default: 'plain',
    values: [
      { name: 'plain', value: 'white' },
      { name: 'warm', value: 'hotpink' },
      { name: 'cool', value: 'deepskyblue' },
    ],
  },
};

Addons

The cli will install some basic addons for you such as controls and actions. Ondevice addons are addons that can render with the device ui that you see on the phone.

Currently the addons available are:

Install each one you want to use and add them to the main.js addons list as follows:

// .storybook/main.js

addons: [
  '@storybook/addon-ondevice-notes',
  '@storybook/addon-ondevice-controls',
  '@storybook/addon-ondevice-backgrounds',
  '@storybook/addon-ondevice-actions',
],

Using the addons in your story

For details of each ondevice addon you can see the readme:

Hide/Show storybook

Storybook on react native is a normal React Native component that can be used or hidden anywhere in your RN application based on your own logic.

You can also create a separate app just for storybook that also works as a package for your visual components. Some have opted to toggle the storybook component by using a custom option in the react native developer menu.

getStorybookUI options

You can pass these parameters to getStorybookUI call in your storybook entry point:

{
    tabOpen: Number (0)
        -- which tab should be open. -1 Sidebar, 0 Canvas, 1 Addons
    initialSelection: string | Object (undefined)
        -- initialize storybook with a specific story.  eg: `mybutton--largebutton` or `{ kind: 'MyButton', name: 'LargeButton' }`
    shouldDisableKeyboardAvoidingView: Boolean (false)
        -- Disable KeyboardAvoidingView wrapping Storybook's view
    keyboardAvoidingViewVerticalOffset: Number (0)
        -- With shouldDisableKeyboardAvoidingView=true, this will set the keyboardverticaloffset (https://facebook.github.io/react-native/docs/keyboardavoidingview#keyboardverticaloffset) value for KeyboardAvoidingView wrapping Storybook's view
}

Contributing

We welcome contributions to Storybook!

  • πŸ“₯ Pull requests and 🌟 Stars are always welcome.
  • Read our contributing guide to get started, or find us on Discord and look for the react-native channel.

Looking for a first issue to tackle?

  • We tag issues with Good First Issue when we think they are well suited for people who are new to the codebase or OSS in general.
  • Talk to us, we'll find something to suits your skills and learning interest.

Examples

Here are some example projects to help you get started

More Repositories

1

storybook

Storybook is a frontend workshop for building UI components and pages in isolation. Made for UI development, testing, and documentation.
TypeScript
82,383
star
2

design-system

πŸ—ƒ Storybook Design System
TypeScript
1,862
star
3

builder-vite

A builder plugin to run and build Storybooks with Vite
TypeScript
884
star
4

addon-designs

A Storybook addon that embeds Figma, websites, or images in the addon panel.
TypeScript
851
star
5

react-inspector

πŸ” Power of Browser DevTools inspectors right inside your React app
TypeScript
740
star
6

testing-react

Testing utilities that allow you to reuse your Storybook stories in your React unit tests!
TypeScript
589
star
7

presets

🧩 Presets for Storybook
TypeScript
424
star
8

marksy

πŸ“‘ A markdown to custom VDOM components library
JavaScript
354
star
9

vue-cli-plugin-storybook

Vue CLI plugin for Storybook
JavaScript
280
star
10

addon-jsx

This Storybook addon show you the JSX / template of the story
TypeScript
237
star
11

eslint-plugin-storybook

πŸŽ—Official ESLint plugin for Storybook
TypeScript
228
star
12

frontpage

🌐 The website for storybook.js.org
TypeScript
209
star
13

test-runner

πŸš• Turn stories into executable tests
TypeScript
202
star
14

storybook-addon-console

storybook-addon. Redirects console output into action logger panel
TypeScript
198
star
15

native

πŸ“± Storybook for Native: iOS, Android, Flutter
TypeScript
176
star
16

babel-plugin-react-docgen

πŸ“ Babel plugin to add react-docgen info into your code.
JavaScript
162
star
17

telejson

πŸ›° JSON parse & stringify with support for cyclic objects, functions, dates, regex, infinity, undefined, null, NaN, Classes, Instances
TypeScript
158
star
18

vs-code-plugin

Aesop: a VSCode Extension to stage Storybook stories inside your IDE.
TypeScript
130
star
19

addon-kit

Everything you need to build a Storybook addon
TypeScript
122
star
20

brand

🎨 Materials for your articles and talks about storybook
89
star
21

addon-svelte-csf

[Incubation] CSF using Svelte components.
TypeScript
86
star
22

desktop

πŸ’» Desktop app for all your Storybooks
JavaScript
71
star
23

addon-react-native-web

Build react-native-web projects in Storybook for React
TypeScript
71
star
24

testing-library

Instrumented version of Testing Library for Storybook Interactions
TypeScript
56
star
25

require-context.macro

πŸ–‡ A Babel macro needed for some advanced Storybook setups. Used to mock webpack's context function in test environments.
JavaScript
48
star
26

addon-styling

A base addon for configuring popular styling tools
TypeScript
44
star
27

ember-cli-storybook

πŸ“’ Ember storybook adapter
JavaScript
36
star
28

solidjs

SolidJS integration for Storybook, maintained by the community
TypeScript
35
star
29

aem

Adobe Experience Manager Storybook app with events, knobs, docs, addons, and more
JavaScript
33
star
30

paths.macro

πŸ‘£ Babel plugin that returns an object containing paths like __dirname and __filename as static values
JavaScript
32
star
31

shout-out-kit

An image generation API template
JavaScript
30
star
32

linter-config

πŸ“ ESlint config, Prettier config, Remark config for storybook
JavaScript
27
star
33

SBNext

A future SB
JavaScript
26
star
34

sandboxes

MDX
25
star
35

addon-measure

JavaScript
20
star
36

addon-postcss

This Storybook addon can be used to run the PostCSS preprocessor against your stories.
JavaScript
20
star
37

addon-styling-webpack

Successor to @storybook/addon-styling. Configure the styles of your webpack storybook with ease!
TypeScript
18
star
38

jest

Instrumented version of Jest for Storybook Interactions
TypeScript
18
star
39

addon-coverage

TypeScript
18
star
40

nextjs-example

Example app showing Storybook integrated with Next.js (v11) pages
JavaScript
17
star
41

testing-vue3

Testing utilities that allow you to reuse your stories in your unit tests
TypeScript
16
star
42

bench

Storybook benchmark
TypeScript
15
star
43

testing-angular

TypeScript
13
star
44

mdx2-csf

MDX to CSF compiler using MDXv2
TypeScript
13
star
45

addon-knobs

Storybook addon prop editor component
TypeScript
10
star
46

testing-vue

TypeScript
10
star
47

jest-storybook

Test storybook stories automagically using Jest. πŸ§™πŸ»β€β™€οΈ
JavaScript
9
star
48

addon-google-analytics

Storybook addon for google analytics
JavaScript
9
star
49

expect

Browser-compatible version of Jest's `expect`
JavaScript
7
star
50

storybook-nuxt

Storybook integration for Nuxt Framework - Unleashing the power of Storybook in Nuxt/Vue Land!
TypeScript
6
star
51

raycast-extension-sandboxes

A Raycast Extension to quickly create sandboxes online or locally
TypeScript
6
star
52

icons

Library of icons used in apps and marketing sites
TypeScript
6
star
53

vue3-code-examples

it is a repo to show Vuetify implementation with new @storybook/vue3 reactive mode
TypeScript
6
star
54

marko

Storybook framework support for marko
TypeScript
6
star
55

addon-graphql

Storybook addon to display the GraphiQL IDE
TypeScript
6
star
56

addon-onboarding

TypeScript
6
star
57

action

🚒 WIP, storybook github action - build your storybook from github
JavaScript
6
star
58

addon-queryparams

parameter addon for storybook
TypeScript
6
star
59

storybook-day

Storybook day website
TypeScript
5
star
60

addon-ie11

JavaScript
5
star
61

addon-design-assets

Design asset preview for storybook
JavaScript
5
star
62

addon-cssresources

A storybook addon to switch between css resources at runtime for your story
TypeScript
5
star
63

create-svelte-with-args

A small CLI wrapper around the create-svelte package that enables you to replace the interactive prompts with CLI arguments.
JavaScript
5
star
64

addon-events

Add events to your Storybook stories.
TypeScript
4
star
65

governance

βš–οΈ Storybook governance and community policies
4
star
66

community

πŸ™Œ Storybook community resources: monthly meetings, meetups, conferences
4
star
67

mdx1-csf

MDX to CSF compiler using MDXv1
TypeScript
4
star
68

addon-react-native-server

A replacement for @storybook/react-native-server which will enable multiple devices to sync over websockets
TypeScript
4
star
69

docs-mdx

TypeScript
3
star
70

addon-angular-ivy

TypeScript
3
star
71

babel-plugin-named-exports-order

Babel plugin for preserving exports order across transforms
JavaScript
3
star
72

nextjs-server

Embedded Storybook in Next.js
TypeScript
3
star
73

addon-bench

Storybook benchmarking helper
JavaScript
3
star
74

repro-react-cra

Reproduction template for Create React App
JavaScript
3
star
75

react-native-demo

Storybook for React Native monorepo
2
star
76

ember

Storybook for Ember
TypeScript
2
star
77

docs2-prototype

TypeScript
2
star
78

components-marketing

TypeScript
2
star
79

repro-templates

WIP
JavaScript
2
star
80

eslint-config-storybook

πŸ”— wrapper around our @storybook/linter-config package
JavaScript
2
star
81

.github

Repo community health files
1
star
82

regression-test-other

1
star
83

e2e-testing-starter

JavaScript
1
star
84

vue3-autogen-controls

TypeScript
1
star
85

stencil-builder-test

TypeScript
1
star
86

auto-config

TypeScript
1
star
87

external-sandboxes

MDX
1
star
88

create-webpack5-react

JavaScript
1
star
89

playwright-ct

Playwright component testing against a Storybook instance
TypeScript
1
star
90

regression-test-preact

1
star
91

addon-webpack5-compiler-babel

Adds babel as a Webpack5 compiler to Storybook
TypeScript
1
star
92

web

TypeScript
1
star