Last active
November 26, 2023 11:14
-
-
Save dannyhw/9b84973dcc6ff4fa2e86e32d571d294e to your computer and use it in GitHub Desktop.
Setup a new project to test the 6.0 alpha of react native storybook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
npx react-native init RnSBSixAlpha --template react-native-template-typescript; | |
cd RnSBSixAlpha; | |
yarn add @storybook/react-native@next \ | |
@react-native-async-storage/async-storage \ | |
@storybook/addon-ondevice-actions@next \ | |
@storybook/addon-ondevice-controls@next \ | |
@storybook/addon-ondevice-backgrounds@next \ | |
@storybook/addon-ondevice-notes@next \ | |
@storybook/[email protected] \ | |
@react-native-community/datetimepicker \ | |
@react-native-community/slider \ | |
@storybook/[email protected] | |
echo "/** | |
* Metro configuration for React Native | |
* https://github.com/facebook/react-native | |
* | |
* @format | |
*/ | |
module.exports = { | |
transformer: { | |
getTransformOptions: async () => ({ | |
transform: { | |
experimentalImportSupport: false, | |
inlineRequires: false, | |
}, | |
}), | |
}, | |
resolver: { | |
resolverMainFields: ['sbmodern', 'react-native', 'browser', 'main'], | |
}, | |
};" > metro.config.js; | |
mkdir .storybook; | |
mkdir components; | |
echo "module.exports = { | |
stories: [ | |
'../components/**/*.stories.?(ts|tsx|js|jsx)' | |
], | |
addons: [ | |
'@storybook/addon-ondevice-notes', | |
'@storybook/addon-ondevice-controls', | |
'@storybook/addon-ondevice-backgrounds', | |
'@storybook/addon-ondevice-actions', | |
], | |
};" > .storybook/main.js; | |
echo "import {withBackgrounds} from '@storybook/addon-ondevice-backgrounds'; | |
export const decorators = [withBackgrounds]; | |
export const parameters = { | |
backgrounds: [ | |
{name: 'plain', value: 'white', default: true}, | |
{name: 'warm', value: 'hotpink'}, | |
{name: 'cool', value: 'deepskyblue'}, | |
], | |
};" > .storybook/preview.js; | |
echo "import { getStorybookUI } from '@storybook/react-native'; | |
import './storybook.requires'; | |
const StorybookUIRoot = getStorybookUI({}); | |
export default StorybookUIRoot;" > .storybook/Storybook.tsx; | |
echo "import StorybookUIRoot from './.storybook/Storybook'; | |
export { StorybookUIRoot as default };" > App.tsx; | |
node -e 'const fs = require("fs"); | |
const packageJSON = require("./package.json"); | |
packageJSON.scripts = { | |
...packageJSON.scripts, | |
prestart: "sb-rn-get-stories", | |
"storybook-watcher": "sb-rn-watcher" | |
}; | |
fs.writeFile("./package.json", JSON.stringify(packageJSON, null, 2), function writeJSON(err) { | |
if (err) return console.log(err); | |
console.log(JSON.stringify(packageJSON)); | |
console.log("writing to " + "./package.json"); | |
});'; | |
if [[ "$(uname)" == "Darwin" ]]; then | |
cd ios; pod install; cd ..; | |
fi | |
mkdir components/Button; | |
echo "import React from 'react'; | |
import {TouchableOpacity, Text, StyleSheet} from 'react-native'; | |
interface MyButtonProps { | |
onPress: () => void; | |
text: string; | |
} | |
export const MyButton = ({onPress, text}: MyButtonProps) => { | |
return ( | |
<TouchableOpacity style={styles.container} onPress={onPress}> | |
<Text style={styles.text}>{text}</Text> | |
</TouchableOpacity> | |
); | |
}; | |
const styles = StyleSheet.create({ | |
container: { | |
paddingHorizontal: 16, | |
paddingVertical: 8, | |
backgroundColor: 'violet', | |
}, | |
text: {color: 'black'}, | |
}); | |
" > components/Button/Button.tsx; | |
echo "import React from 'react'; | |
import {ComponentStory, ComponentMeta} from '@storybook/react-native'; | |
import {MyButton} from './Button'; | |
const MyButtonMeta: ComponentMeta<typeof MyButton> = { | |
title: 'MyButton', | |
component: MyButton, | |
argTypes: { | |
onPress: {action: 'pressed the button'}, | |
}, | |
args: { | |
text: 'Hello world', | |
}, | |
}; | |
export default MyButtonMeta; | |
type MyButtonStory = ComponentStory<typeof MyButton>; | |
export const Basic: MyButtonStory = args => <MyButton {...args} />; | |
" > components/Button/Button.stories.tsx; | |
yarn sb-rn-get-stories; |
Yes, exactly that!
@diegodorado I had to change it to
if [[ "$(uname)" == "Darwin" ]]; then
cd ios; pod install; cd ..;
fi
can you test it for linux to make sure it doesn't run?
Yes, that syntax also worked for me. Thanks!
Need to change default CLI option for background addon as
echo "import {withBackgrounds} from '@storybook/addon-ondevice-backgrounds';
export const decorators = [withBackgrounds];
export const parameters = {
backgrounds: {
default: 'plain',
values: [
{ name: 'plain', value: 'white' },
{ name: 'warm', value: 'hotpink' },
{ name: 'cool', value: 'deepskyblue' },
],
},
};" > .storybook/preview.js
@JeffGuKang yes thanks for the reminder, I haven't updated this since the most recent updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@diegodorado hey sure I can add that got any suggestions?
Also this script is intended more as a temporary thing so you can expect a more feature complete version to be included in the storybook cli.
Maybe this?