Skip to content

Commit

Permalink
Working Storybook version
Browse files Browse the repository at this point in the history
This comment helped resolve it:
storybookjs/react-native#405 (comment)
  • Loading branch information
peterfication committed Mar 16, 2023
1 parent 0c0a701 commit eb58744
Show file tree
Hide file tree
Showing 11 changed files with 3,105 additions and 110 deletions.
7 changes: 7 additions & 0 deletions .storybook/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getStorybookUI } from '@storybook/react-native';

import './storybook.requires';

const StorybookUIRoot = getStorybookUI({});

export default StorybookUIRoot;
4 changes: 4 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
stories: ['./stories/**/*.stories.?(ts|tsx|js|jsx)'],
addons: ['@storybook/addon-ondevice-controls', '@storybook/addon-ondevice-actions'],
};
8 changes: 8 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const parameters = {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
20 changes: 20 additions & 0 deletions .storybook/stories/Button/Button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import { TouchableOpacity, Text, StyleSheet } from 'react-native';

export const MyButton = ({ onPress, text }) => {
return (
<TouchableOpacity style={styles.container} onPress={onPress} activeOpacity={0.8}>
<Text style={styles.text}>{text}</Text>
</TouchableOpacity>
);
};

const styles = StyleSheet.create({
container: {
paddingHorizontal: 16,
paddingVertical: 8,
backgroundColor: 'purple',
borderRadius: 8,
},
text: { color: 'white' },
});
31 changes: 31 additions & 0 deletions .storybook/stories/Button/Button.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { View } from 'react-native';
import { MyButton } from './Button';

const MyButtonMeta = {
title: 'MyButton',
component: MyButton,
argTypes: {
onPress: { action: 'pressed the button' },
},
args: {
text: 'Hello world',
},
decorators: [
(Story) => (
<View style={{ alignItems: 'center', justifyContent: 'center', flex: 1 }}>
<Story />
</View>
),
],
};

export default MyButtonMeta;

export const Basic = {};

export const AnotherExample = {
args: {
text: 'Another example',
},
};
58 changes: 58 additions & 0 deletions .storybook/storybook.requires.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* do not change this file, it is auto generated by storybook. */

import {
configure,
addDecorator,
addParameters,
addArgsEnhancer,
clearDecorators,
} from '@storybook/react-native';

import '@storybook/addon-ondevice-controls/register';
import '@storybook/addon-ondevice-actions/register';

import { argsEnhancers } from '@storybook/addon-actions/dist/modern/preset/addArgs';

// eslint-disable-next-line import/named
import { decorators, parameters } from './preview';

global.STORIES = [
{
titlePrefix: '',
directory: './.storybook/stories',
files: '**/*.stories.?(ts|tsx|js|jsx)',
importPathMatcher:
'^\\.[\\\\/](?:\\.storybook\\/stories(?:\\/(?!\\.)(?:(?:(?!(?:^|\\/)\\.).)*?)\\/|\\/|$)(?!\\.)(?=.)[^/]*?\\.stories\\.(?:ts|tsx|js|jsx)?)$',
},
];

if (decorators) {
// eslint-disable-next-line no-undef
if (__DEV__) {
// stops the warning from showing on every HMR
require('react-native').LogBox.ignoreLogs([
'`clearDecorators` is deprecated and will be removed in Storybook 7.0',
]);
}
// workaround for global decorators getting infinitely applied on HMR, see https://github.com/storybookjs/react-native/issues/185
clearDecorators();
decorators.forEach((decorator) => addDecorator(decorator));
}

if (parameters) {
addParameters(parameters);
}

try {
argsEnhancers.forEach((enhancer) => addArgsEnhancer(enhancer));
} catch {
/* workaround for occasional crash */
}

const getStories = () => {
return {
'./.storybook/stories/Button/Button.stories.js': require('./stories/Button/Button.stories.js'),
};
};

configure(getStories, module, false);
3 changes: 2 additions & 1 deletion App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export const App = () => {
);
};

export default App;
// export default App;
export {default} from './.storybook';
28 changes: 28 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ PODS:
- glog
- react-native-encrypted-storage (4.0.3):
- React-Core
- react-native-safe-area-context (4.5.0):
- RCT-Folly
- RCTRequired
- RCTTypeSafety
- React-Core
- ReactCommon/turbomodule/core
- react-native-slider (4.4.2):
- React-Core
- React-perflogger (0.71.4)
- React-RCTActionSheet (0.71.4):
- React-Core/RCTActionSheetHeaders (= 0.71.4)
Expand Down Expand Up @@ -417,6 +425,10 @@ PODS:
- React-perflogger (= 0.71.4)
- ReactNativeUiLib (4.0.0):
- React
- RNCAsyncStorage (1.17.12):
- React-Core
- RNDateTimePicker (6.7.5):
- React-Core
- RNGestureHandler (2.9.0):
- React-Core
- RNReanimated (3.0.2):
Expand Down Expand Up @@ -498,6 +510,8 @@ DEPENDENCIES:
- React-jsinspector (from `../node_modules/react-native/ReactCommon/jsinspector`)
- React-logger (from `../node_modules/react-native/ReactCommon/logger`)
- react-native-encrypted-storage (from `../node_modules/react-native-encrypted-storage`)
- react-native-safe-area-context (from `../node_modules/react-native-safe-area-context`)
- "react-native-slider (from `../node_modules/@react-native-community/slider`)"
- React-perflogger (from `../node_modules/react-native/ReactCommon/reactperflogger`)
- React-RCTActionSheet (from `../node_modules/react-native/Libraries/ActionSheetIOS`)
- React-RCTAnimation (from `../node_modules/react-native/Libraries/NativeAnimation`)
Expand All @@ -512,6 +526,8 @@ DEPENDENCIES:
- React-runtimeexecutor (from `../node_modules/react-native/ReactCommon/runtimeexecutor`)
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
- ReactNativeUiLib (from `../node_modules/react-native-ui-lib`)
- "RNCAsyncStorage (from `../node_modules/@react-native-async-storage/async-storage`)"
- "RNDateTimePicker (from `../node_modules/@react-native-community/datetimepicker`)"
- RNGestureHandler (from `../node_modules/react-native-gesture-handler`)
- RNReanimated (from `../node_modules/react-native-reanimated`)
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)
Expand Down Expand Up @@ -577,6 +593,10 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/logger"
react-native-encrypted-storage:
:path: "../node_modules/react-native-encrypted-storage"
react-native-safe-area-context:
:path: "../node_modules/react-native-safe-area-context"
react-native-slider:
:path: "../node_modules/@react-native-community/slider"
React-perflogger:
:path: "../node_modules/react-native/ReactCommon/reactperflogger"
React-RCTActionSheet:
Expand Down Expand Up @@ -605,6 +625,10 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon"
ReactNativeUiLib:
:path: "../node_modules/react-native-ui-lib"
RNCAsyncStorage:
:path: "../node_modules/@react-native-async-storage/async-storage"
RNDateTimePicker:
:path: "../node_modules/@react-native-community/datetimepicker"
RNGestureHandler:
:path: "../node_modules/react-native-gesture-handler"
RNReanimated:
Expand Down Expand Up @@ -647,6 +671,8 @@ SPEC CHECKSUMS:
React-jsinspector: 1f51e775819199d3fe9410e69ee8d4c4161c7b06
React-logger: 0d58569ec51d30d1792c5e86a8e3b78d24b582c6
react-native-encrypted-storage: db300a3f2f0aba1e818417c1c0a6be549038deb7
react-native-safe-area-context: 39c2d8be3328df5d437ac1700f4f3a4f75716acc
react-native-slider: 33b8d190b59d4f67a541061bb91775d53d617d9d
React-perflogger: 0bb0522a12e058f6eb69d888bc16f40c16c4b907
React-RCTActionSheet: bfd675a10f06a18728ea15d82082d48f228a213a
React-RCTAnimation: 2fa220b2052ec75b733112aca39143d34546a941
Expand All @@ -661,6 +687,8 @@ SPEC CHECKSUMS:
React-runtimeexecutor: 8fa50b38df6b992c76537993a2b0553d3b088004
ReactCommon: b49a4b00ca6d181ff74b17c12b2d59ac4add0bde
ReactNativeUiLib: 511a5eb03809a0b27f6aefa2b4d6f7c2d6ae4053
RNCAsyncStorage: 09fc8595e6d6f6d5abf16b23a56b257d9c6b7c5b
RNDateTimePicker: 65e1d202799460b286ff5e741d8baf54695e8abd
RNGestureHandler: 071d7a9ad81e8b83fe7663b303d132406a7d8f39
RNReanimated: f0dd6b881808e635ef0673f89642937d6c141314
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
Expand Down
3 changes: 3 additions & 0 deletions metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

module.exports = {
resolver: {
resolverMainFields: ['sbmodern', 'react-native', 'browser', 'main'],
},
transformer: {
getTransformOptions: async () => ({
transform: {
Expand Down
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"test": "jest",
"test:coverage": "jest --verbose --coverage && open ./coverage/lcov-report/index.html",
"test:watch": "jest --watch",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"storybook-generate": "sb-rn-get-stories",
"storybook-watch": "sb-rn-watcher"
},
"dependencies": {
"react": "18.2.0",
Expand All @@ -30,7 +32,15 @@
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@prettier/plugin-xml": "^2.2.0",
"@react-native-async-storage/async-storage": "^1.17.12",
"@react-native-community/datetimepicker": "^6.7.5",
"@react-native-community/eslint-config": "^3.2.0",
"@react-native-community/slider": "^4.4.2",
"@storybook/addon-actions": "^6.5.14",
"@storybook/addon-controls": "^6.5.14",
"@storybook/addon-ondevice-actions": "6.5.0-rc.0",
"@storybook/addon-ondevice-controls": "6.5.0-rc.0",
"@storybook/react-native": "6.5.0-rc.0",
"@testing-library/react-hooks": "^8.0.1",
"@testing-library/react-native": "^12.0.0",
"@tsconfig/react-native": "^2.0.2",
Expand All @@ -40,6 +50,7 @@
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"babel-jest": "^29.2.1",
"babel-loader": "^8.3.0",
"cspell": "^6.29.3",
"eslint": "^8.19.0",
"eslint-config-alloy": "^4.9.0",
Expand All @@ -50,7 +61,12 @@
"jest": "^29.2.1",
"metro-react-native-babel-preset": "0.73.8",
"prettier": "^2.8.4",
"react-dom": "18.2.0",
"react-native-safe-area-context": "^4.5.0",
"react-test-renderer": "18.2.0",
"typescript": "4.8.4"
},
"volta": {
"node": "16.18.1"
}
}

0 comments on commit eb58744

Please sign in to comment.