/
BackgroundPanel.tsx
99 lines (84 loc) · 2.84 KB
/
BackgroundPanel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* eslint-disable react/destructuring-assignment, import/no-extraneous-dependencies */
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import Events from '@storybook/core-events';
import { AddonStore } from '@storybook/addons';
import { API } from '@storybook/api';
import { StoryStore } from '@storybook/client-api';
import Swatch from './Swatch';
import BackgroundEvents, { PARAM_KEY } from './constants';
import { Background } from './index';
const codeSample = `
import { storiesOf } from '@storybook/react-native';
import { withBackgrounds } from '@storybook/addon-ondevice-backgrounds';
addDecorator(withBackgrounds);
storiesOf('First Component', module)
.addParameters({
backgrounds: [
{ name: 'warm', value: 'hotpink', default: true },
{ name: 'cool', value: 'deepskyblue' },
],
})
.add("First Button", () => <Button>Click me</Button>);
`.trim();
const Instructions = () => (
<View>
<Text style={{ fontSize: 16 }}>Setup Instructions</Text>
<Text>
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.
</Text>
<Text>
Below is an example of how to add the background decorator to your story definition.
</Text>
<Text>{codeSample}</Text>
</View>
);
export type Channel = ReturnType<AddonStore['getChannel']>;
type Selection = ReturnType<StoryStore['fromId']>;
interface BackgroundPanelProps {
channel: Channel;
api: API;
active: boolean;
}
interface BackgroundPanelState {
selection: Selection;
}
export default class BackgroundPanel extends Component<BackgroundPanelProps, BackgroundPanelState> {
componentDidMount() {
this.props.channel.on(Events.SELECT_STORY, this.onStorySelected);
}
componentWillUnmount() {
this.props.channel.removeListener(Events.SELECT_STORY, this.onStorySelected);
}
setBackgroundFromSwatch = (background: string) => {
this.props.channel.emit(BackgroundEvents.UPDATE_BACKGROUND, background);
};
onStorySelected = (selection: Selection) => {
this.setState({ selection });
};
render() {
const { active, api } = this.props;
if (!active || !this.state) {
return null;
}
const story = api
.store()
.getStoryAndParameters(this.state.selection.kind, this.state.selection.story);
const backgrounds: Background[] = story.parameters[PARAM_KEY];
return (
<View>
{backgrounds ? (
backgrounds.map(({ value, name }) => (
<View key={`${name} ${value}`}>
<Swatch value={value} name={name} setBackground={this.setBackgroundFromSwatch} />
</View>
))
) : (
<Instructions />
)}
</View>
);
}
}