From 28f75ecffbd46a66cbf1036a36913afcb8bf6ddd Mon Sep 17 00:00:00 2001 From: Mohamed Shaaban Date: Sun, 25 Feb 2018 22:41:20 +0100 Subject: [PATCH 1/9] Move manager related files to manager directory --- addons/viewport/src/{ => manager}/components/Panel.js | 0 addons/viewport/src/{ => manager}/components/RotateViewport.js | 0 addons/viewport/src/{ => manager}/components/SelectViewport.js | 0 addons/viewport/src/{ => manager}/components/styles.js | 0 .../viewport/src/{ => manager}/components/tests/Panel.test.js | 0 .../src/{ => manager}/components/tests/RotateViewport.test.js | 0 .../src/{ => manager}/components/tests/SelectViewport.test.js | 0 .../src/{ => manager}/components/tests/viewportInfo.spec.js | 0 addons/viewport/src/{ => manager}/components/viewportInfo.js | 0 addons/viewport/src/{manager.js => manager/index.js} | 0 .../src/{tests/manager.test.js => manager/tests/index.test.js} | 2 +- 11 files changed, 1 insertion(+), 1 deletion(-) rename addons/viewport/src/{ => manager}/components/Panel.js (100%) rename addons/viewport/src/{ => manager}/components/RotateViewport.js (100%) rename addons/viewport/src/{ => manager}/components/SelectViewport.js (100%) rename addons/viewport/src/{ => manager}/components/styles.js (100%) rename addons/viewport/src/{ => manager}/components/tests/Panel.test.js (100%) rename addons/viewport/src/{ => manager}/components/tests/RotateViewport.test.js (100%) rename addons/viewport/src/{ => manager}/components/tests/SelectViewport.test.js (100%) rename addons/viewport/src/{ => manager}/components/tests/viewportInfo.spec.js (100%) rename addons/viewport/src/{ => manager}/components/viewportInfo.js (100%) rename addons/viewport/src/{manager.js => manager/index.js} (100%) rename addons/viewport/src/{tests/manager.test.js => manager/tests/index.test.js} (92%) diff --git a/addons/viewport/src/components/Panel.js b/addons/viewport/src/manager/components/Panel.js similarity index 100% rename from addons/viewport/src/components/Panel.js rename to addons/viewport/src/manager/components/Panel.js diff --git a/addons/viewport/src/components/RotateViewport.js b/addons/viewport/src/manager/components/RotateViewport.js similarity index 100% rename from addons/viewport/src/components/RotateViewport.js rename to addons/viewport/src/manager/components/RotateViewport.js diff --git a/addons/viewport/src/components/SelectViewport.js b/addons/viewport/src/manager/components/SelectViewport.js similarity index 100% rename from addons/viewport/src/components/SelectViewport.js rename to addons/viewport/src/manager/components/SelectViewport.js diff --git a/addons/viewport/src/components/styles.js b/addons/viewport/src/manager/components/styles.js similarity index 100% rename from addons/viewport/src/components/styles.js rename to addons/viewport/src/manager/components/styles.js diff --git a/addons/viewport/src/components/tests/Panel.test.js b/addons/viewport/src/manager/components/tests/Panel.test.js similarity index 100% rename from addons/viewport/src/components/tests/Panel.test.js rename to addons/viewport/src/manager/components/tests/Panel.test.js diff --git a/addons/viewport/src/components/tests/RotateViewport.test.js b/addons/viewport/src/manager/components/tests/RotateViewport.test.js similarity index 100% rename from addons/viewport/src/components/tests/RotateViewport.test.js rename to addons/viewport/src/manager/components/tests/RotateViewport.test.js diff --git a/addons/viewport/src/components/tests/SelectViewport.test.js b/addons/viewport/src/manager/components/tests/SelectViewport.test.js similarity index 100% rename from addons/viewport/src/components/tests/SelectViewport.test.js rename to addons/viewport/src/manager/components/tests/SelectViewport.test.js diff --git a/addons/viewport/src/components/tests/viewportInfo.spec.js b/addons/viewport/src/manager/components/tests/viewportInfo.spec.js similarity index 100% rename from addons/viewport/src/components/tests/viewportInfo.spec.js rename to addons/viewport/src/manager/components/tests/viewportInfo.spec.js diff --git a/addons/viewport/src/components/viewportInfo.js b/addons/viewport/src/manager/components/viewportInfo.js similarity index 100% rename from addons/viewport/src/components/viewportInfo.js rename to addons/viewport/src/manager/components/viewportInfo.js diff --git a/addons/viewport/src/manager.js b/addons/viewport/src/manager/index.js similarity index 100% rename from addons/viewport/src/manager.js rename to addons/viewport/src/manager/index.js diff --git a/addons/viewport/src/tests/manager.test.js b/addons/viewport/src/manager/tests/index.test.js similarity index 92% rename from addons/viewport/src/tests/manager.test.js rename to addons/viewport/src/manager/tests/index.test.js index 6f5bf00af8a5..815824c6e603 100644 --- a/addons/viewport/src/tests/manager.test.js +++ b/addons/viewport/src/manager/tests/index.test.js @@ -1,5 +1,5 @@ import addons from '@storybook/addons'; -import { init, addChannel } from '../manager'; +import { init, addChannel } from '../'; jest.mock('@storybook/addons'); From e2daecf4b4878835725fd93d0e2462304479b438 Mon Sep 17 00:00:00 2001 From: Mohamed Shaaban Date: Sun, 25 Feb 2018 22:48:51 +0100 Subject: [PATCH 2/9] Move addon IDs to shared directory --- addons/viewport/src/manager/components/Panel.js | 5 +++-- addons/viewport/src/manager/index.js | 3 +-- addons/viewport/src/shared/index.js | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 addons/viewport/src/shared/index.js diff --git a/addons/viewport/src/manager/components/Panel.js b/addons/viewport/src/manager/components/Panel.js index 39d080912ff2..1056888938d4 100644 --- a/addons/viewport/src/manager/components/Panel.js +++ b/addons/viewport/src/manager/components/Panel.js @@ -6,6 +6,7 @@ import { document } from 'global'; import { viewports, defaultViewport, resetViewport } from './viewportInfo'; import { SelectViewport } from './SelectViewport'; import { RotateViewport } from './RotateViewport'; +import { UPDATE_VIEWPORT_EVENT_ID } from '../../shared'; import * as styles from './styles'; @@ -29,7 +30,7 @@ export class Panel extends Component { isLandscape: false, }; - this.props.channel.on('addon:viewport:update', this.changeViewport); + this.props.channel.on(UPDATE_VIEWPORT_EVENT_ID, this.changeViewport); } componentDidMount() { @@ -37,7 +38,7 @@ export class Panel extends Component { } componentWillUnmount() { - this.props.channel.removeListener('addon:viewport:update', this.changeViewport); + this.props.channel.removeListener(UPDATE_VIEWPORT_EVENT_ID, this.changeViewport); } iframe = undefined; diff --git a/addons/viewport/src/manager/index.js b/addons/viewport/src/manager/index.js index 9f86b6d02f80..77cc056e1b6e 100644 --- a/addons/viewport/src/manager/index.js +++ b/addons/viewport/src/manager/index.js @@ -3,8 +3,7 @@ import addons from '@storybook/addons'; import { Panel } from './components/Panel'; -const ADDON_ID = 'storybook-addon-viewport'; -const PANEL_ID = `${ADDON_ID}/addon-panel`; +import { ADDON_ID, PANEL_ID } from '../shared'; const addChannel = api => { const channel = addons.getChannel(); diff --git a/addons/viewport/src/shared/index.js b/addons/viewport/src/shared/index.js new file mode 100644 index 000000000000..b08d06bc1644 --- /dev/null +++ b/addons/viewport/src/shared/index.js @@ -0,0 +1,3 @@ +export const ADDON_ID = 'storybook-addon-viewport'; +export const PANEL_ID = `${ADDON_ID}/addon-panel`; +export const UPDATE_VIEWPORT_EVENT_ID = `addon:viewport:update`; From 7ad2e8cc9f5a65c37d74c74ff5a8434a531a354a Mon Sep 17 00:00:00 2001 From: Mohamed Shaaban Date: Sun, 25 Feb 2018 23:10:35 +0100 Subject: [PATCH 3/9] Decouple initial viewports from SelectViewport component --- .../viewport/src/manager/components/Panel.js | 9 +++++--- .../src/manager/components/SelectViewport.js | 5 ++-- .../manager/components/tests/Panel.test.js | 23 ++++++++++++++++--- .../components/tests/SelectViewport.test.js | 10 ++++---- .../components/tests/viewportInfo.spec.js | 8 +++---- .../src/manager/components/viewportInfo.js | 2 +- 6 files changed, 40 insertions(+), 17 deletions(-) diff --git a/addons/viewport/src/manager/components/Panel.js b/addons/viewport/src/manager/components/Panel.js index 1056888938d4..122c03472669 100644 --- a/addons/viewport/src/manager/components/Panel.js +++ b/addons/viewport/src/manager/components/Panel.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import { baseFonts } from '@storybook/components'; import { document } from 'global'; -import { viewports, defaultViewport, resetViewport } from './viewportInfo'; +import { initialViewports, defaultViewport, resetViewport } from './viewportInfo'; import { SelectViewport } from './SelectViewport'; import { RotateViewport } from './RotateViewport'; import { UPDATE_VIEWPORT_EVENT_ID } from '../../shared'; @@ -27,6 +27,7 @@ export class Panel extends Component { super(props, context); this.state = { viewport: defaultViewport, + viewports: initialViewports, isLandscape: false, }; @@ -64,7 +65,7 @@ export class Panel extends Component { }; updateIframe = () => { - const { viewport: viewportKey, isLandscape } = this.state; + const { viewports, viewport: viewportKey, isLandscape } = this.state; const viewport = viewports[viewportKey] || resetViewport; if (!this.iframe) { @@ -82,7 +83,7 @@ export class Panel extends Component { }; render() { - const { isLandscape, viewport } = this.state; + const { isLandscape, viewport, viewports } = this.state; const disableDefault = viewport === defaultViewport; const disabledStyles = disableDefault ? styles.disabled : {}; @@ -97,6 +98,8 @@ export class Panel extends Component { return (
this.changeViewport(e.target.value)} /> diff --git a/addons/viewport/src/manager/components/SelectViewport.js b/addons/viewport/src/manager/components/SelectViewport.js index 230e360239d8..0312a06263cd 100644 --- a/addons/viewport/src/manager/components/SelectViewport.js +++ b/addons/viewport/src/manager/components/SelectViewport.js @@ -1,10 +1,9 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { viewports, defaultViewport } from './viewportInfo'; import * as styles from './styles'; -export function SelectViewport({ activeViewport, onChange }) { +export function SelectViewport({ viewports, defaultViewport, activeViewport, onChange }) { return (
); diff --git a/addons/viewport/src/manager/components/tests/SelectViewport.test.js b/addons/viewport/src/manager/components/tests/SelectViewport.test.js index f79ac46645ae..69e7efe855cf 100644 --- a/addons/viewport/src/manager/components/tests/SelectViewport.test.js +++ b/addons/viewport/src/manager/components/tests/SelectViewport.test.js @@ -44,10 +44,12 @@ describe('Viewport/SelectViewport', () => { viewportKeys.forEach(key => { let option; + const { name } = INITIAL_VIEWPORTS[key]; + const expectedText = key === DEFAULT_VIEWPORT ? `(Default) ${name}` : name; - it(`renders an option for ${INITIAL_VIEWPORTS[key].name}`, () => { - option = subject.find(`[value="${key}"]`); - expect(option.text()).toEqual(INITIAL_VIEWPORTS[key].name); + it(`renders an option for ${name}`, () => { + option = subject.find(`option[value="${key}"]`); + expect(option.text()).toEqual(expectedText); }); }); }); diff --git a/addons/viewport/src/manager/components/viewportInfo.js b/addons/viewport/src/manager/components/viewportInfo.js index b101c211ba5c..1953b89ee5c3 100644 --- a/addons/viewport/src/manager/components/viewportInfo.js +++ b/addons/viewport/src/manager/components/viewportInfo.js @@ -19,8 +19,8 @@ export const resetViewport = { export function applyStyles(viewport, styles) { const mixedStyles = { - ...viewport.styles, ...styles, + ...viewport.styles, }; return { diff --git a/addons/viewport/src/shared/index.js b/addons/viewport/src/shared/index.js index dec4a571dfd4..f89ec05751d3 100644 --- a/addons/viewport/src/shared/index.js +++ b/addons/viewport/src/shared/index.js @@ -3,6 +3,17 @@ export const PANEL_ID = `${ADDON_ID}/addon-panel`; export const UPDATE_VIEWPORT_EVENT_ID = 'addon:viewport:update'; export const CONFIGURE_VIEWPORT_EVENT_ID = 'addon:viewport:configure'; export const INITIAL_VIEWPORTS = { + responsive: { + name: 'Responsive', + styles: { + width: '100%', + height: '100%', + border: 'none', + display: 'block', + margin: '0', + boxShadow: 'none', + }, + }, iphone5: { name: 'iPhone 5', styles: { @@ -53,4 +64,4 @@ export const INITIAL_VIEWPORTS = { }, }, }; -export const DEFAULT_VIEWPORT = 'default'; +export const DEFAULT_VIEWPORT = 'responsive'; From 5a2c5a024cb6df47e6e278356b463fb71722058d Mon Sep 17 00:00:00 2001 From: Mohamed Shaaban Date: Sun, 25 Feb 2018 23:49:15 +0100 Subject: [PATCH 6/9] Register all channel listeners on componentDidMount --- addons/viewport/src/manager/components/Panel.js | 3 +-- .../src/manager/components/tests/Panel.test.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/addons/viewport/src/manager/components/Panel.js b/addons/viewport/src/manager/components/Panel.js index 38bea0e3a66c..d27986fac110 100644 --- a/addons/viewport/src/manager/components/Panel.js +++ b/addons/viewport/src/manager/components/Panel.js @@ -37,8 +37,6 @@ export class Panel extends Component { viewports: viewportsTransformer(INITIAL_VIEWPORTS), isLandscape: false, }; - - this.props.channel.on(UPDATE_VIEWPORT_EVENT_ID, this.changeViewport); } componentDidMount() { @@ -46,6 +44,7 @@ export class Panel extends Component { this.iframe = document.getElementById(storybookIframe); + channel.on(UPDATE_VIEWPORT_EVENT_ID, this.changeViewport); channel.on(CONFIGURE_VIEWPORT_EVENT_ID, this.configure); this.unsubscribeFromOnStory = api.onStory(() => { diff --git a/addons/viewport/src/manager/components/tests/Panel.test.js b/addons/viewport/src/manager/components/tests/Panel.test.js index c366ad1c606f..89001cc78117 100644 --- a/addons/viewport/src/manager/components/tests/Panel.test.js +++ b/addons/viewport/src/manager/components/tests/Panel.test.js @@ -33,13 +33,6 @@ describe('Viewport/Panel', () => { isLandscape: false, }); }); - - it('listens on `update` channel', () => { - expect(props.channel.on).toHaveBeenCalledWith( - 'addon:viewport:update', - subject.instance().changeViewport - ); - }); }); describe('componentDidMount', () => { @@ -60,12 +53,19 @@ describe('Viewport/Panel', () => { expect(subject.instance().iframe).toEqual('iframe'); }); - it('listens on the `configure` topic', () => { + it('listens on `configure` channel', () => { expect(props.channel.on).toHaveBeenCalledWith( 'addon:viewport:configure', subject.instance().configure ); }); + + it('listens on `update` channel', () => { + expect(props.channel.on).toHaveBeenCalledWith( + 'addon:viewport:update', + subject.instance().changeViewport + ); + }); }); describe('componentWillUnmount', () => { From 29c81a738acaaf4b4b4ae41ebae4c551f771806d Mon Sep 17 00:00:00 2001 From: Mohamed Shaaban Date: Mon, 26 Feb 2018 00:00:28 +0100 Subject: [PATCH 7/9] Add custom viewports to the official-storybook example --- examples/official-storybook/config.js | 12 ++++++++++++ examples/official-storybook/extra-viewports.json | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 examples/official-storybook/extra-viewports.json diff --git a/examples/official-storybook/config.js b/examples/official-storybook/config.js index e53df3b35d7e..1a106e18eb0c 100644 --- a/examples/official-storybook/config.js +++ b/examples/official-storybook/config.js @@ -1,7 +1,12 @@ import { configure } from '@storybook/react'; import { setOptions } from '@storybook/addon-options'; +import { + configure as configureViewport, + INITIAL_VIEWPORTS +} from '@storybook/addon-viewport'; import 'react-chromatic/storybook-addon'; import addHeadWarning from './head-warning'; +import extraViewports from './extra-viewports.json'; addHeadWarning('Preview head not loaded', 'preview-head-not-loaded'); addHeadWarning('Dotenv file not loaded', 'dotenv-file-not-loaded'); @@ -11,6 +16,13 @@ setOptions({ hierarchyRootSeparator: /\|/, }); +configureViewport({ + viewports: { + ...INITIAL_VIEWPORTS, + ...extraViewports + } +}); + function importAll(req) { req.keys().forEach(filename => req(filename)); } diff --git a/examples/official-storybook/extra-viewports.json b/examples/official-storybook/extra-viewports.json new file mode 100644 index 000000000000..27bc68170b0f --- /dev/null +++ b/examples/official-storybook/extra-viewports.json @@ -0,0 +1,16 @@ +{ + "kindleFire2": { + "name": "Kindle Fire 2", + "styles": { + "width": "600px", + "height": "963px" + } + }, + "kindleFireHD": { + "name": "Kindle Fire HD", + "styles": { + "width": "533px", + "height": "801px" + } + } +} From 8a0d5b49a554cee16266d294d233e6c24df2e4be Mon Sep 17 00:00:00 2001 From: Mohamed Shaaban Date: Mon, 26 Feb 2018 20:59:18 +0100 Subject: [PATCH 8/9] Update viewport addon documentation --- addons/viewport/README.md | 97 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 91 insertions(+), 6 deletions(-) diff --git a/addons/viewport/README.md b/addons/viewport/README.md index 5f8705db9c9e..6838d5aaa42d 100644 --- a/addons/viewport/README.md +++ b/addons/viewport/README.md @@ -16,7 +16,26 @@ or with yarn: yarn add -D @storybook/addon-viewport -## Basic Usage + +## Configuration + +Import and use the `configure` function in your `config.js` file. + +```js +import { configure } from '@storybook/addon-viewport'; +``` + +### defaultViewport : String +---- +Setting this property to, let say `iphone6`, will make `iPhone 6` the default device/viewport for all stories. Default is `'responsive'` which fills 100% of the preview area. + +### viewports : Object +---- +A key-value pair of viewport's key and properties for all viewports to be displayed. Default is [`INITIAL_VIEWPORTS`](src/shared/index.js) + +## Examples + +### Basic Usage Simply import the Storybook Viewport Addon in the `addons.js` file in your `.storybook` directory. @@ -26,12 +45,78 @@ import '@storybook/addon-viewport/register' This will register the Viewport Addon to Storybook and will show up in the action area. -## FAQ -#### How do I add a new device? +### Use Custom Set of Devices -Unfortunately, this is currently not supported. +This will replace all previous devices with `Kindle Fire 2` and `Kindle Fire HD` by simply calling `configure` with the two devices as `viewports` in `config.js` file in your `.storybook` directory. + +```js +import { configure } from '@storybook/addon-viewport'; + +const newViewports = { + kindleFire2: { + name: 'Kindle Fire 2', + styles: { + width: '600px', + height: '963px' + } + }, + kindleFireHD: { + name: 'Kindle Fire HD', + styles: { + width: '533px', + height: '801px' + } + } +}; + +configure({ + viewports: newViewports +}); +``` -#### How can I make a custom screen size? -Unfortunately, this is currently not supported. +### Add New Device + +This will add both `Kindle Fire 2` and `Kindle Fire HD` to the list of devices. This is acheived by making use of the exported [`INITIAL_VIEWPORTS`](src/shared/index.js) property, by merging it with the new viewports and pass the result as `viewports` to `configure` function + +```js +import { configure, INITIAL_VIEWPORTS } from '@storybook/addon-viewport'; + +const newViewports = { + kindleFire2: { + name: 'Kindle Fire 2', + styles: { + width: '600px', + height: '963px' + } + }, + kindleFireHD: { + name: 'Kindle Fire HD', + styles: { + width: '533px', + height: '801px' + } + } +}; + +configure({ + viewports: { + ...INITIAL_VIEWPORTS, + ...newViewports + } +}); +``` + + +### Change The Default Viewport + +This will make `iPhone 6` the default viewport for all stories. + +```js +import { configure } from '@storybook/addon-viewport'; + +configure({ + defaultViewport: 'iphone6' +}); +``` From d3046596bac2bbb6fd01b56f24426aad97560286 Mon Sep 17 00:00:00 2001 From: Mohamed Shaaban Date: Fri, 9 Mar 2018 00:51:35 +0100 Subject: [PATCH 9/9] Allow setting custom default viewport per story Fix linting issues --- addons/viewport/preview.js | 2 + .../viewport/src/manager/components/Panel.js | 67 ++++++-- .../manager/components/tests/Panel.test.js | 152 ++++++++++++++---- .../components/tests/viewportInfo.spec.js | 16 +- addons/viewport/src/preview/index.js | 37 ++++- .../viewport/src/preview/tests/index.test.js | 43 ++++- addons/viewport/src/shared/index.js | 2 + examples/official-storybook/config.js | 9 +- examples/official-storybook/package.json | 1 + .../addon-viewport.stories.storyshot | 33 ++++ .../stories/addon-viewport.stories.js | 27 ++++ 11 files changed, 323 insertions(+), 66 deletions(-) create mode 100644 examples/official-storybook/stories/__snapshots__/addon-viewport.stories.storyshot create mode 100644 examples/official-storybook/stories/addon-viewport.stories.js diff --git a/addons/viewport/preview.js b/addons/viewport/preview.js index 1a2ee6e366df..c78f8bab3217 100644 --- a/addons/viewport/preview.js +++ b/addons/viewport/preview.js @@ -3,3 +3,5 @@ const preview = require('./dist/preview'); exports.configure = preview.configure; exports.DEFAULT_VIEWPORT = preview.DEFAULT_VIEWPORT; exports.INITIAL_VIEWPORTS = preview.INITIAL_VIEWPORTS; +exports.withViewport = preview.withViewport; +exports.Viewport = preview.Viewport; diff --git a/addons/viewport/src/manager/components/Panel.js b/addons/viewport/src/manager/components/Panel.js index d27986fac110..c3fc43d88961 100644 --- a/addons/viewport/src/manager/components/Panel.js +++ b/addons/viewport/src/manager/components/Panel.js @@ -7,6 +7,8 @@ import { resetViewport, viewportsTransformer } from './viewportInfo'; import { SelectViewport } from './SelectViewport'; import { RotateViewport } from './RotateViewport'; import { + SET_STORY_DEFAULT_VIEWPORT_EVENT_ID, + UNSET_STORY_DEFAULT_VIEWPORT_EVENT_ID, CONFIGURE_VIEWPORT_EVENT_ID, UPDATE_VIEWPORT_EVENT_ID, INITIAL_VIEWPORTS, @@ -23,12 +25,23 @@ const containerStyles = { ...baseFonts, }; +const getDefaultViewport = (viewports, candidateViewport) => + candidateViewport in viewports ? candidateViewport : Object.keys(viewports)[0]; + +const getViewports = viewports => + Object.keys(viewports).length > 0 ? viewports : INITIAL_VIEWPORTS; + export class Panel extends Component { static propTypes = { channel: PropTypes.shape({}).isRequired, api: PropTypes.shape({}).isRequired, }; + static defaultOptions = { + viewports: INITIAL_VIEWPORTS, + defaultViewport: DEFAULT_VIEWPORT, + }; + constructor(props, context) { super(props, context); this.state = { @@ -46,6 +59,8 @@ export class Panel extends Component { channel.on(UPDATE_VIEWPORT_EVENT_ID, this.changeViewport); channel.on(CONFIGURE_VIEWPORT_EVENT_ID, this.configure); + channel.on(SET_STORY_DEFAULT_VIEWPORT_EVENT_ID, this.setStoryDefaultViewport); + channel.on(UNSET_STORY_DEFAULT_VIEWPORT_EVENT_ID, this.unsetStoryDefaultViewport); this.unsubscribeFromOnStory = api.onStory(() => { this.changeViewport(this.state.defaultViewport); @@ -58,19 +73,39 @@ export class Panel extends Component { if (this.unsubscribeFromOnStory) { this.unsubscribeFromOnStory(); } - + channel.removeListener(UPDATE_VIEWPORT_EVENT_ID, this.changeViewport); channel.removeListener(CONFIGURE_VIEWPORT_EVENT_ID, this.configure); + channel.removeListener(SET_STORY_DEFAULT_VIEWPORT_EVENT_ID, this.setStoryDefaultViewport); + channel.removeListener(UNSET_STORY_DEFAULT_VIEWPORT_EVENT_ID, this.unsetStoryDefaultViewport); } - configure = ({ viewports = INITIAL_VIEWPORTS, defaultViewport = DEFAULT_VIEWPORT }) => { - if (Object.keys(viewports).length === 0) { - viewports = INITIAL_VIEWPORTS; - } + setStoryDefaultViewport = viewport => { + const { viewports } = this.state; + const defaultViewport = getDefaultViewport(viewports, viewport); - if (!(defaultViewport in viewports)) { - defaultViewport = Object.keys(viewports)[0]; - } + this.setState( + { + storyDefaultViewport: defaultViewport, + viewport: defaultViewport, + }, + this.updateIframe + ); + }; + + unsetStoryDefaultViewport = () => { + this.setState( + { + storyDefaultViewport: undefined, + viewport: this.state.defaultViewport, + }, + this.updateIframe + ); + }; + + configure = (options = Panel.defaultOptions) => { + const viewports = getViewports(options.viewports); + const defaultViewport = getDefaultViewport(viewports, options.defaultViewport); this.setState( { @@ -123,9 +158,15 @@ export class Panel extends Component { }; render() { - const { isLandscape, defaultViewport, viewport, viewports } = this.state; - - const disableDefault = viewport === defaultViewport; + const { + isLandscape, + defaultViewport, + storyDefaultViewport = defaultViewport, + viewport, + viewports, + } = this.state; + + const disableDefault = viewport === storyDefaultViewport; const disabledStyles = disableDefault ? styles.disabled : {}; const buttonStyles = { @@ -139,7 +180,7 @@ export class Panel extends Component {
this.changeViewport(e.target.value)} /> @@ -152,7 +193,7 @@ export class Panel extends Component {