From 59bb39abc686f688207f4561d1384bce549735ae Mon Sep 17 00:00:00 2001 From: Daniel Duan Date: Wed, 8 Nov 2017 23:20:15 -0500 Subject: [PATCH] good enough improvements to storybook --- .../preview/components/OnDeviceUI/index.js | 112 +++++++++--------- .../preview/components/OnDeviceUI/style.js | 11 +- 2 files changed, 59 insertions(+), 64 deletions(-) diff --git a/app/react-native/src/preview/components/OnDeviceUI/index.js b/app/react-native/src/preview/components/OnDeviceUI/index.js index 6378ca441301..819c63b33143 100644 --- a/app/react-native/src/preview/components/OnDeviceUI/index.js +++ b/app/react-native/src/preview/components/OnDeviceUI/index.js @@ -4,6 +4,7 @@ import { ifIphoneX } from 'react-native-iphone-x-helper'; import { Animated, + Dimensions, Easing, View, TouchableWithoutFeedback, @@ -15,6 +16,14 @@ import style from './style'; import StoryListView from '../StoryListView'; import StoryView from '../StoryView'; +/** + * Returns true if the screen is in portrait mode + */ +const isDeviceInPortrait = () => { + const dim = Dimensions.get('screen'); + return dim.height >= dim.width; +}; + const openMenuImage = require('./menu_open.png'); const closeMenuImage = require('./menu_close.png'); @@ -27,29 +36,41 @@ export default class OnDeviceUI extends Component { isMenuOpen: false, selectedKind: null, selectedStory: null, - menuWidth: 0, + menuWidth: Dimensions.get('screen').width / 2, + isPortrait: isDeviceInPortrait(), }; + } - this.storyChangedHandler = this.handleStoryChanged.bind(this); - this.menuToggledHandler = this.handleToggleMenu.bind(this); - this.menuLayoutHandler = this.handleMenuLayout.bind(this); + componentWillMount = () => { + Dimensions.addEventListener('change', this.handleDeviceRotation); + this.props.events.on('story', this.handleStoryChange); + }; - this.props.events.on('story', this.storyChangedHandler); + componentDidMount() { + StatusBar.setHidden(true); } - componentWillUnmount() { - this.props.events.removeListener('story', this.storyChangedHandler); - } + componentWillUnmount = () => { + Dimensions.removeEventListener('change', this.handleDeviceRotation); + this.props.events.removeListener('story', this.handleStoryChange); + }; - handleStoryChanged(storyFn, selection) { + handleDeviceRotation = () => { + this.setState({ + isPortrait: isDeviceInPortrait(), + menuWidth: Dimensions.get('screen').width / 2, + }); + }; + + handleStoryChange = (storyFn, selection) => { const { kind, story } = selection; this.setState({ selectedKind: kind, selectedStory: story, }); - } + }; - handleToggleMenu() { + handleToggleMenu = () => { const isMenuOpen = !this.state.isMenuOpen; Animated.timing(this.state.menuAnimation, { @@ -61,17 +82,22 @@ export default class OnDeviceUI extends Component { this.setState({ isMenuOpen, }); - } - - handleMenuLayout(e) { - this.setState({ - menuWidth: e.nativeEvent.layout.width, - }); - } + }; render() { const { stories, events, url } = this.props; - const { menuAnimation, selectedKind, selectedStory, menuWidth } = this.state; + const { isPortrait, menuAnimation, selectedKind, selectedStory, menuWidth } = this.state; + + const iPhoneXStyles = ifIphoneX( + isPortrait + ? { + marginVertical: 30, + } + : { + marginHorizontal: 30, + }, + {} + ); const menuStyles = [ style.menuContainer, @@ -84,28 +110,8 @@ export default class OnDeviceUI extends Component { }), }, ], - ...ifIphoneX( - { - paddingTop: 30, - }, - {} - ), - }, - ]; - - const menuSpacerStyles = [ - { - width: menuAnimation.interpolate({ - inputRange: [0, 1], - outputRange: [0, menuWidth], - }), - ...ifIphoneX( - { - paddingTop: 30, - }, - {} - ), }, + iPhoneXStyles, ]; const headerStyles = [ @@ -118,17 +124,9 @@ export default class OnDeviceUI extends Component { }, ]; - const previewContainerStyles = [ - style.previewContainer, - { - ...ifIphoneX( - { - paddingTop: 30, - }, - {} - ), - }, - ]; + const previewContainerStyles = [style.previewContainer, iPhoneXStyles]; + + const previewWrapperStyles = [style.previewWrapper, iPhoneXStyles]; /* Checks if import is a base64 encoded string uri. @@ -145,27 +143,25 @@ export default class OnDeviceUI extends Component { return ( -