|
1 |
| -import React from 'react' |
2 |
| -import addonAPI from '@storybook/addons' |
3 |
| - |
4 |
| -class PanelContainer extends React.Component { |
5 |
| - state = { |
6 |
| - theme: null, |
7 |
| - themes: {}, |
8 |
| - initialised: false, |
9 |
| - } |
10 |
| - |
11 |
| - constructor(props) { |
12 |
| - super() |
13 |
| - |
14 |
| - props.channel.on('storybook-styled-components/init', this.onInit) |
15 |
| - } |
16 |
| - |
17 |
| - onInit = themes => { |
18 |
| - const queryTheme = this.props.api.getQueryParam('currentTheme') |
19 |
| - |
20 |
| - const theme = queryTheme |
21 |
| - ? queryTheme |
22 |
| - : this.state.theme ? this.state.theme : Object.keys(themes)[0] |
23 |
| - |
24 |
| - this.setTheme(themes, theme) |
25 |
| - } |
26 |
| - |
27 |
| - updateTheme = e => { |
28 |
| - const newTheme = e.target.value |
29 |
| - this.setTheme(this.state.themes, newTheme) |
30 |
| - } |
31 |
| - |
32 |
| - setTheme(themes, theme) { |
33 |
| - this.setState({ themes, theme }) |
34 |
| - this.props.channel.emit('storybook-styled-components/update', theme) |
35 |
| - this.props.api.setQueryParams({ currentTheme: theme }) |
36 |
| - } |
37 |
| - |
38 |
| - render() { |
39 |
| - const { theme, themes } = this.state |
40 |
| - |
41 |
| - if (!theme) return <div>Addon is initialising</div> |
42 |
| - |
43 |
| - return ( |
44 |
| - <div> |
45 |
| - <select value={this.state.theme} onChange={this.updateTheme}> |
46 |
| - {Object.keys(themes).map(theme => ( |
47 |
| - <option key={theme} value={theme}> |
48 |
| - {theme} |
49 |
| - </option> |
50 |
| - ))} |
51 |
| - </select> |
52 |
| - </div> |
53 |
| - ) |
54 |
| - } |
55 |
| -} |
56 |
| - |
57 |
| -addonAPI.register('storybook-styled-components', storybookAPI => { |
58 |
| - addonAPI.addPanel('storybook-styled-components/panel', { |
59 |
| - title: 'Theme Picker', |
60 |
| - render: () => ( |
61 |
| - <PanelContainer channel={addonAPI.getChannel()} api={storybookAPI} /> |
62 |
| - ), |
63 |
| - }) |
64 |
| -}) |
| 1 | +require('./dist/register'); |
0 commit comments