Skip to content
Danny's Blog
Twitter

Introducing React Native Web Storybook

react native, storybook, react4 min read

Storybook is a UI development workshop for components and pages. It's used by many teams for all kinds of reasons. You can develop components in isolation, run visual tests and even validate the accessibility of your UI.

Storybook started off web focused and has expanded to many different platforms since then. One of those platforms is React Native.

Storybook for React Native is a catalog of all your components and states, that runs inside your iOS or Android app, on your mobile device. This is great for seeing your React Native components in their production setting, but also has some disadvantages when compared to Storybook's web UI.

That's why I'm excited to announce Storybook for React Native Web, a new complementary approach to developing and sharing React Native components in Storybook.

A different take on Storybook for React Native

I've been working together with Michael Shilman on a new approach for React Native Storybook that has all the functionality of Storybook on the web. This is achieved by using an addon we built that uses the react-native-web library to make your components available in the browser. All you have to do is set up the addon in your project.

In the video below I am interacting with React Native components directly in the browser. The components displayed use features from popular React Native libraries such as the animation library react-native-reanimated, and the common library for handling interactions react-native-gesture-handler.

Heres the example storybook embedded below (use the link if the embed doesn't work)

The challenge of React Native

React Native has its own Storybook implementation at @storybook/react-native, however it has some limitations.

Storybook for React Native is built on a different architecture than Storybook for web frameworks (React, Vue, Angular, etc.), and it is built as a separate project. Because of this architectural difference a lot of effort is required to achieve feature parity with Storybook for web.

Storybook for React Native supports only a small portion of the addons and features that exist for Storybook on the web platform. This also means external tools like visual testing for Storybook need a custom implementation in order to support react native. Unfortunately, this leads to users having an incomplete experience for React Native in comparison with the web

Ideally, Storybook for React Native should support all popular addons and existing tools used on Storybook for Web. Is this possible?

Well, if you're here you probably guessed that the answer is yes. There are of course some limitations, however I think that for many use cases it will be worth it. The main use case that comes to mind is UI library maintainers that want to show off their React Native components in an interactive way.

React Native components on the Web?

You might be surprised at the number of React Native libraries that already fully support the web platform. Also if you've been following the React Native scene you might know that facebook is putting a strong emphasis on the many platform vision.

These are some of the top downloaded libraries for React Native (based on npm numbers):

  • react-navigation
  • react-native-screens
  • react-native-gesture-handler
  • react-native-svg
  • react-native-reanimated
  • react-native-vector-icons

All of these libraries have support for the web platform in some way. In fact I've tested many of these popular libraries with this new addon and they all worked very well on the web.

Now, I would like to share with you the project I've been working on, which should help bridge the gap today for React Native and Storybook, allowing you to have full functionality that you know and love from Storybook for web.

Introducing @storybook/addon-react-native-web

This addon provides you with a simple setup for using the ReactJS Storybook in a React Native project. You can run it locally or easily deploy it as a website for others to see. The best part is that since it's running on the web with the ReactJS configuration most addons should work with no changes necessary.

That means:

  • Much better UI/UX.
  • Popular addons such as Docs supported out of the box.
  • Chromatic and other visual testing tools supported.
  • Easily publish your Storybook and share it with stakeholders such as designers and product owners.
  • Full support for the latest Storybook features.

There are however a few drawbacks:

  • Some libraries won't have web support
  • The components may not display exactly as they would on a native device
  • Some extra config needed for libraries that aren't transpiled

If those drawbacks aren't dealbreakers, then I think you will be happy with the results. However If you are unable to use this package because you need a native mobile experience then consider using @storybook/react-native. You could even use both if it makes sense for your use case.

If you are concerned about components being different on the web please see the FAQ where I have tried to address this.

I've also provided some example config in the project readme

Now the fun part!

Getting started

Heres the steps to follow if you want to try this out yourself.

Create your react native project if you haven't already

1npx react-native init AwesomeApp --template react-native-template-typescript
2cd AwesomeApp

Initialize Storybook.

1npx sb init --type react

You can delete the example stories if you like (not necessary).

1rm -rf stories/*

Now add @storybook/addon-react-native-web and it's dependencies

1yarn add --dev react-dom react-native-web babel-plugin-react-native-web @storybook/addon-react-native-web

Open up .storybook/main.js and add the addon to the addons list.

1module.exports = {
2 /* existing config */
3 addons: [/*existing addons,*/ "@storybook/addon-react-native-web"],
4};

Now that the setup is done, let's add a React Native component and a story in the stories folder.

Here's an example of a button component:

1// stories/MyButton.tsx
2import React from "react";
3import { StyleSheet, Text, TouchableOpacity, View } from "react-native";
4
5export type ButtonProps = {
6 onPress: () => void;
7 text: string;
8 color?: string;
9 textColor?: string;
10};
11
12const styles = StyleSheet.create({
13 button: {
14 paddingVertical: 8,
15 paddingHorizontal: 16,
16 borderRadius: 4,
17 alignSelf: "flex-start",
18 flexGrow: 0,
19 backgroundColor: "purple",
20 },
21 buttonText: {
22 color: "white",
23 fontSize: 16,
24 fontWeight: "bold",
25 },
26 buttonContainer: {
27 alignItems: "flex-start",
28 flex: 1,
29 },
30});
31
32export const MyButton = ({ text, onPress, color, textColor }: ButtonProps) => (
33 <View style={styles.buttonContainer}>
34 <TouchableOpacity
35 style={[styles.button, !!color && { backgroundColor: color }]}
36 onPress={onPress}
37 activeOpacity={0.8}
38 >
39 <Text style={[styles.buttonText, !!textColor && { color: textColor }]}>
40 {text}
41 </Text>
42 </TouchableOpacity>
43 </View>
44);

And a story for that component:

1// stories/MyButton.stories.tsx
2import React from "react";
3import { ComponentMeta, ComponentStory } from "@storybook/react";
4
5import { MyButton } from "./MyButton";
6
7export default {
8 title: "components/MyButton",
9 component: MyButton,
10} as ComponentMeta<typeof MyButton>;
11
12export const Basic: ComponentStory<typeof MyButton> = (args) => (
13 <MyButton {...args} />
14);
15
16Basic.args = {
17 text: "Hello World",
18 color: "purple",
19};

Now run the Storybook with yarn storybook and it should open in your browser.

You should see something like this:

Running on the web without a single div or span in sight! 🎉

Thanks for reading this far, I would appreciate hearing your thoughts on this! If you want to see an example project showcasing Storybook for React Native and web using libraries such as reanimated and gesture handler, make sure to check this repository out.

I'm one of the maintainers of @storybook/react-native, currently working hard on improving it and preparing a 6.0 release, so keep an eye out! I would love to talk more about my vision for the upcoming release of Storybook for React Native in a future post, so let me know if you're interested!

You can find me on twitter or discord in the #react-native channel of the Storybook discord server:

Big thank you to Michael Shilman for setting up the repository and helping me figure out all the Storybook config. Also for the time spent and guidance. This wouldn't have been possible without his hard work!

Thanks to everyone who helped me edit this post and gave feedback.