Gathering detailed insights and metrics for @storybook/addon-react-native-web
Gathering detailed insights and metrics for @storybook/addon-react-native-web
Gathering detailed insights and metrics for @storybook/addon-react-native-web
Gathering detailed insights and metrics for @storybook/addon-react-native-web
Build react-native-web projects in Storybook for React
npm install @storybook/addon-react-native-web
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
83 Stars
110 Commits
24 Forks
11 Watching
16 Branches
109 Contributors
Updated on 30 Oct 2024
Minified
Minified + Gzipped
TypeScript (81.48%)
JavaScript (18.34%)
CSS (0.18%)
Cumulative downloads
Total Downloads
Last day
-2.2%
18,564
Compared to previous day
Last week
-2.6%
84,548
Compared to previous week
Last month
19.3%
364,574
Compared to previous month
Last year
57.3%
2,956,254
Compared to previous year
6
54
This addon configures @storybook/react
to display React Native (RN) projects using React Native for Web (RNW)
See the FAQ for common questions.
You can read more about this package in this blog post.
To contribute see the contributing guide here
Heres a screen shot of how you could use this alongside storybook/react-native, the image is taken from the following starter code
Assuming you've got an existing RN project, run the following from the project root:
1npx sb init --type react 2yarn add react-dom react-native-web babel-plugin-react-native-web @storybook/addon-react-native-web @react-native/babel-preset --dev
Then edit your .storybook/main.js
:
1module.exports = { 2 addons: [/*existing addons,*/ '@storybook/addon-react-native-web'], 3};
From here, you should be able to write stories incorporating your RN components according to the Storybook for React instructions.
Please see the FAQ for common issues like the "loader not found" error.
Most packages should work without extra changes however in some cases extra steps are needed. One common example is "reanimated" which requires some babel config and extra transpilation.
Options | Type | Description |
---|---|---|
modulesToTranspile | Array<string> | node_modules that need transpiling |
modulesToAlias | {[key: string]: string} | node_modules that need aliasing |
babelPlugins | Array<string | [string, Record<string, string>]> | Babel plugins you want to apply |
projectRoot | string | Path to the root of your project, if in a mono repo you might need to set this. |
babelPresets | Array<string | [string, Record<string, string>]> | Babel presets you want to apply |
babelPresetReactOptions | Record<string, any> | Options to pass options to @babel/preset-react |
babelPresetReactNativeOptions | Record<string, any> | Options to pass options to @react-native/babel-preset |
Many react-native packages are shipped untranspiled and this doesn't work for the web platform. If you receive errors like "proper loader not found" after adding a package try adding it to the modulesToTranspile
option for this addon.
You can do that like this:
1module.exports = { 2 addons: [ 3 /*existing addons,*/ 4 { 5 name: '@storybook/addon-react-native-web', 6 options: { 7 modulesToTranspile: ['react-native-package-name'], 8 }, 9 }, 10 ], 11};
Some react-native packages recommend module aliasing as a means of importing and using the web variant of an existing package. If you need to add additional key:value pairs to webpack's config.resolve.alias
, use the modulesToAlias
option for this addon. You don't need to add react-native-web to this list as it is already included by default.
You can do that like this:
1module.exports = { 2 addons: [ 3 /*existing addons,*/ 4 { 5 name: '@storybook/addon-react-native-web', 6 options: { 7 modulesToAlias: { 8 'react-native-package-name': 'react-native-web-package-name', 9 }, 10 }, 11 }, 12 ], 13};
Replace react-native-package-name
with the name of a real package.
It's common to provide a babel plugin for certain packages in the react native eco system and you can pass a list of these to the addon.
1module.exports = { 2 addons: [ 3 /*existing addons,*/ 4 { 5 name: '@storybook/addon-react-native-web', 6 options: { 7 babelPlugins: ['babel-plugin-name'], 8 }, 9 }, 10 ], 11};
Many libraries work without extra config, heres some examples of config required for some packages.
Note: react-native-vector-icons requires some extra steps due to fonts required and there will be a future addon with that config included.
Package | Required config |
react-native-svg | No extra config needed |
react-native-gesture-handler | No extra config needed |
react-native-reanimated |
Click to here to see the config
|
native-base |
Click to here to see the configDue to the vector icons dependency add the following
|
react-native-paper |
Click to here to see the configDue to the vector icons dependency add the following
|
nativewind |
Click to here to see the configNativewind requires some additional babel config to work correctly. You can see an example of this config below.
|
Install @svgr/webpack
and url-loader
1module.exports = { 2 /*existing config*/ 3 // to provide a public export for assets 4 staticDirs: ['<path_to_assets>'], 5 webpackFinal: async (config) => { 6 const fileLoaderRule = config.module.rules.find( 7 (rule) => rule.test && rule.test.test('.svg'), 8 ); 9 10 if (fileLoaderRule) { 11 fileLoaderRule.exclude = /\.svg$/; 12 } 13 14 config.module.rules.push({ 15 test: /\.svg$/, 16 use: ['@svgr/webpack', 'url-loader'], 17 }); 18 19 return config; 20 }, 21};
install node-polyfill-webpack-plugin
1const NodePolyfillPlugin = require('node-polyfill-webpack-plugin'); 2 3module.exports = { 4 /*existing config*/ 5 core: { 6 builder: 'webpack5', 7 }, 8 webpackFinal: async (config) => { 9 config.plugins.push(new NodePolyfillPlugin()); 10 11 return config; 12 }, 13};
<div>
and span
)
No vulnerabilities found.
No security vulnerabilities found.
@storybook/addon-ondevice-controls
Display storybook controls on your device.
@storybook/addon-ondevice-knobs
Display storybook story knobs on your deviced.
@storybook/addon-ondevice-backgrounds
A react-native storybook addon to show different backgrounds for your preview
@storybook/addon-ondevice-actions
Action Logger addon for react-native storybook