Storybook setup for desiloed development with react-native(iOS, Android, Web)

In the previous post , we saw how we could setup a react native project for web development. In this post we will see how to setup the project for storybook development. If you follow the react channels, you might have already heard of storybook. Storybook is a really nice way of developing UI components for your app. It provides a development environment that is tailored for effective, faster iterative development and to easily showcase UI components. It also provides a feature called addons which can be used to enhance the storybook features. Check out the addon gallery at https://storybook.js.org/addons/addon-gallery/

Storybook is available for react, react-native, vue, angular. The cli experience is one of the smoothest to get started with.

cd my-project-directory
npm i -g @storybook/cli
getstorybook

In short, you navigate to the project directory and run . It automatically detects the project type and adds storybook configuration to your project. It also provides a way to customise the setup which we will be needing for this demo.

The storybook cli and some scripts differs based on the project type i.e or etc. In our case since we will be using project for web as well we need to customise a bit. Let’s see how we can do that.

  1. Install storybook-cli.
npm i -g @storybook/cli

2. Create a new react native project

react-native init StorybookSetup

3. Run inside project directory. This will add storybook configuration for the react-native project

cd StorybookSetup$ getstorybookgetstorybook - the simplest way to add a storybook to your project. • Detecting project type. ✓
• Adding storybook support to your "React Native" app. ✓
• Preparing to install dependencies. ✓

After this you should be able to see a storybook inside of your project

There should be a new command in scripts section of package.json

"storybook": "storybook start -p 7007"

The commands internally starts

a. Storybook server which can be used as remote control to navigate storybook on the device

b. It also starts the react native packager with in the storybook directory as the entry point

Run the command

yarn run storybook

And depending on whether you want to run the app on iOS or android run the appropriate command to launch the app

react-native run-ios // or react-native run-android

After this you should have the default storybook app running on the device with remote control running at http://localhost:7007

4. Let’s add storybook react web config to this project. Add the following modules to your project

yarn add @storybook/react babel-plugin-module-resolver babel-preset-react-app --dev
yarn add react-native-web react-art

5. Copy the directory from a project created by create-react-app into the root directory of your react-native project or download from here

6. Create a new directory with following files. is the default storybook config directory for react web project. Add the following files to that directory

a.

import { configure } from '@storybook/react';function loadStories() {
require('../storybook/stories');
}
configure(loadStories, module);

b.

import '@storybook/addon-actions/register';
import '@storybook/addon-links/register';

c.

6. is a module that differs for

import { storiesOf } from '@storybook/react-native';

and project.

import { storiesOf } from '@storybook/react';

We need to make this load differently based on the platform i.e android, ios or web. There is a feature of where we can load different files based on the platform. One of the ways to do that is to use platform suffix for the file or . We create a directory called and create the following files

a.

import { storiesOf } from '@storybook/react-native';export {
storiesOf
}

b.

import { storiesOf } from '@storybook/react-native';export {
storiesOf
}

c.

import { storiesOf } from '@storybook/react';export {
storiesOf
}

Replace all references of

import { storiesOf } from '@storybook/react-native';

in

import { storiesOf } from '../helpers/storiesOf';

7. We also need to make similar changes for module. By default when was run in the project directory, it created and . We will add a file to this directory. This will be loaded for react-web storybook

import React from 'react';
import PropTypes from 'prop-types';
import { TouchableHighlight } from 'react-native';
export default function Button(props) {
return <TouchableHighlight onPress={props.onPress}>{props.children}</TouchableHighlight>;
}
Button.defaultProps = {
children: null,
onPress: () => {},
};
Button.propTypes = {
children: PropTypes.node,
onPress: PropTypes.func,
};

8. Finally, we add the following script commands to package.json

"storybook-web": "start-storybook -p 9009 -s public",
"build-storybook-web": "build-storybook -s public"

When we run you should be able to see the react-web storybook running at http://localhost:9009

You can also export a static website using the command.

You can checkout a working project at https://github.com/agenthunt/rn-dev-ios-android-web/tree/master/StorybookSetup

Solving Problems. Making Software Better. Intrigued by Elegant Solutions. https://twitter.com/agent_hunt