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..48fc275b36d7 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, @@ -46,6 +48,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); @@ -61,8 +65,36 @@ export class Panel extends Component { 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); } + setStoryDefaultViewport = viewport => { + const { viewports } = this.state; + + if (!(viewport in viewports)) { + viewport = Object.keys(viewports)[0]; + } + + this.setState( + { + storyDefaultViewport: viewport, + viewport, + }, + this.updateIframe + ); + }; + + unsetStoryDefaultViewport = () => { + this.setState( + { + storyDefaultViewport: undefined, + viewport: this.state.defaultViewport, + }, + this.updateIframe + ); + }; + configure = ({ viewports = INITIAL_VIEWPORTS, defaultViewport = DEFAULT_VIEWPORT }) => { if (Object.keys(viewports).length === 0) { viewports = INITIAL_VIEWPORTS; @@ -123,9 +155,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 +177,7 @@ export class Panel extends Component {
this.changeViewport(e.target.value)} /> @@ -152,7 +190,7 @@ export class Panel extends Component {