23
loading...
This website collects cookies to deliver better user experience
npx react-native init AwesomeApp --template react-native-template-typescript
cd AwesomeApp
npx sb init --type react
rm -rf stories/*
yarn add --dev react-dom react-native-web babel-plugin-react-native-web @storybook/addon-react-native-web
module.exports = {
/* existing config */
addons: [/*existing addons,*/ '@storybook/addon-react-native-web'],
};
// stories/MyButton.tsx
import React from 'react';
import {StyleSheet, Text, TouchableOpacity, View} from 'react-native';
export type ButtonProps = {
onPress: () => void;
text: string;
color?: string;
textColor?: string;
};
const styles = StyleSheet.create({
button: {
paddingVertical: 8,
paddingHorizontal: 16,
borderRadius: 4,
alignSelf: 'flex-start',
flexGrow: 0,
backgroundColor: 'purple',
},
buttonText: {
color: 'white',
fontSize: 16,
fontWeight: 'bold',
},
buttonContainer: {
alignItems: 'flex-start',
flex: 1,
},
});
export const MyButton = ({text, onPress, color, textColor}: ButtonProps) => (
<View style={styles.buttonContainer}>
<TouchableOpacity
style={[styles.button, !!color && {backgroundColor: color}]}
onPress={onPress}
activeOpacity={0.8}>
<Text style={[styles.buttonText, !!textColor && {color: textColor}]}>
{text}
</Text>
</TouchableOpacity>
</View>
);
// stories/MyButton.stories.tsx
import React from 'react';
import {ComponentMeta, ComponentStory} from '@storybook/react';
import {MyButton} from './MyButton';
export default {
title: 'components/MyButton',
component: MyButton,
} as ComponentMeta<typeof MyButton>;
export const Basic: ComponentStory<typeof MyButton> = args => (
<MyButton {...args} />
);
Basic.args = {
text: 'Hello World',
color: 'purple',
};
yarn storybook
and it should open in your browser.