UNPKG

@storybook/addon-ondevice-backgrounds

Version:

A react-native storybook addon to show different backgrounds for your preview

57 lines (50 loc) 2.63 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { StyleSheet, Text, View } from 'react-native'; import Swatch from './Swatch'; import BackgroundEvents, { PARAM_KEY } from './constants'; const codeSample = ` import React from 'react'; import { ComponentStory, ComponentMeta } from '@storybook/react-native'; import { withBackgrounds } from '@storybook/addon-ondevice-backgrounds'; import { Text, StyleSheet } from 'react-native'; const Background = () => ( <Text style={styles.text}>Change background color via Addons -&gt; Background</Text> ); const styles = StyleSheet.create({ text: { color: 'black' }, }); const BackgroundMeta: ComponentMeta<typeof Background> = { title: 'Background CSF', component: Background, decorators: [withBackgrounds], parameters: { backgrounds: { default: 'plain', values: [ { name: 'plain', value: 'white' }, { name: 'warm', value: 'hotpink' }, { name: 'cool', value: 'deepskyblue' }, ], }, }, }; export default BackgroundMeta; type BackgroundStory = ComponentStory<typeof Background>; export const Basic: BackgroundStory = () => <Background />; `.trim(); const Instructions = () => (_jsxs(View, { children: [_jsx(Text, { style: [styles.paragraph, styles.title], children: "Setup Instructions" }), _jsx(Text, { style: styles.paragraph, children: "Please add the background decorator definition to your story. The background decorate accepts an array of items, which should include a name for your color (preferably the css class name) and the corresponding color / image value." }), _jsx(Text, { style: styles.paragraph, children: "Below is an example of how to add the background decorator to your story definition. Long press the example to copy it." }), _jsx(Text, { selectable: true, children: codeSample })] })); const BackgroundPanel = ({ active, api, channel }) => { const store = api.store(); const storyId = store.getSelection().storyId; const story = store.fromId(storyId); const backgrounds = story.parameters[PARAM_KEY]; const setBackgroundFromSwatch = (background) => { channel.emit(BackgroundEvents.UPDATE_BACKGROUND, background); }; return (_jsx(View, { style: { padding: 10 }, children: backgrounds?.values ? (backgrounds.values.map(({ value, name }) => (_jsx(View, { children: _jsx(Swatch, { value: value, name: name, setBackground: setBackgroundFromSwatch }) }, `${name} ${value}`)))) : (_jsx(Instructions, {})) })); }; export default BackgroundPanel; const styles = StyleSheet.create({ title: { fontSize: 16 }, paragraph: { marginBottom: 8 }, });