@storybook/addon-ondevice-backgrounds
Version:
A react-native storybook addon to show different backgrounds for your preview
98 lines (91 loc) • 4.56 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_native_1 = require("react-native");
const Swatch_1 = __importDefault(require("./Swatch"));
const constants_1 = __importStar(require("./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 -> 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 = () => ((0, jsx_runtime_1.jsxs)(react_native_1.View, { children: [(0, jsx_runtime_1.jsx)(react_native_1.Text, { style: [styles.paragraph, styles.title], children: "Setup Instructions" }), (0, jsx_runtime_1.jsx)(react_native_1.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." }), (0, jsx_runtime_1.jsx)(react_native_1.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." }), (0, jsx_runtime_1.jsx)(react_native_1.Text, { selectable: true, children: codeSample })] }));
const BackgroundPanel = ({ active, api, channel }) => {
if (!active) {
return null;
}
const store = api.store();
const storyId = store.getSelection().storyId;
const story = store.fromId(storyId);
const backgrounds = story.parameters[constants_1.PARAM_KEY];
const setBackgroundFromSwatch = (background) => {
channel.emit(constants_1.default.UPDATE_BACKGROUND, background);
};
return ((0, jsx_runtime_1.jsx)(react_native_1.View, { style: { padding: 10 }, children: backgrounds ? (backgrounds.values.map(({ value, name }) => ((0, jsx_runtime_1.jsx)(react_native_1.View, { children: (0, jsx_runtime_1.jsx)(Swatch_1.default, { value: value, name: name, setBackground: setBackgroundFromSwatch }) }, `${name} ${value}`)))) : ((0, jsx_runtime_1.jsx)(Instructions, {})) }));
};
exports.default = BackgroundPanel;
const styles = react_native_1.StyleSheet.create({
title: { fontSize: 16 },
paragraph: { marginBottom: 8 },
});