From 51328dc93ff2c65375dd9856c0b405c81fce33ab Mon Sep 17 00:00:00 2001 From: Jorge Date: Fri, 3 Nov 2017 15:14:52 +0000 Subject: [PATCH] Close sidebar on resizing from non-mobile to mobile sizes. The layout component had to be refactored to be a class instead of a function. --- editor/layout/index.js | 93 +++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 24 deletions(-) diff --git a/editor/layout/index.js b/editor/layout/index.js index d1bc65167f4066..2084cf532f43c6 100644 --- a/editor/layout/index.js +++ b/editor/layout/index.js @@ -7,6 +7,7 @@ import classnames from 'classnames'; /** * WordPress dependencies */ +import { Component } from '@wordpress/element'; import { NoticeList, Popover, navigateRegions } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; @@ -21,7 +22,7 @@ import VisualEditor from '../modes/visual-editor'; import UnsavedChangesWarning from '../unsaved-changes-warning'; import DocumentTitle from '../document-title'; import AutosaveMonitor from '../autosave-monitor'; -import { removeNotice } from '../actions'; +import { removeNotice, toggleSidebar } from '../actions'; import MetaBoxes from '../meta-boxes'; import { getEditorMode, @@ -29,31 +30,75 @@ import { getNotices, } from '../selectors'; -function Layout( { mode, isSidebarOpened, notices, ...props } ) { - const className = classnames( 'editor-layout', { - 'is-sidebar-opened': isSidebarOpened, - } ); +const MOBILE_WIDTH = 762; - return ( -
- - - - -
-
-
- { mode === 'text' && } - { mode === 'visual' && } -
-
- +class Layout extends Component { + constructor() { + super( ...arguments ); + this.onUpdateDimensions = this.onUpdateDimensions.bind( this ); + this.saveLastWindowWidth = this.saveLastWindowWidth.bind( this ); + this.state = { + lastWindowWidth: null, + }; + } + + saveLastWindowWidth() { + this.setState( { + lastWindowWidth: window.innerWidth, + } ); + } + + onUpdateDimensions() { + if ( this.props.isSidebarOpened && + this.state.lastWindowWidth > MOBILE_WIDTH && + window.innerWidth < MOBILE_WIDTH + ) { + this.props.toggleSidebar(); + } + this.saveLastWindowWidth(); + } + + componentDidMount() { + if ( window ) { + if ( ! this.state.lastWindowWidth ) { + this.saveLastWindowWidth(); + } + window.addEventListener( 'resize', this.onUpdateDimensions ); + } + } + + componentWillUnmount() { + if ( window ) { + window.removeEventListener( 'resize', this.onUpdateDimensions ); + } + } + + render() { + const { mode, isSidebarOpened, notices } = this.props; + const className = classnames( 'editor-layout', { + 'is-sidebar-opened': isSidebarOpened, + } ); + return ( +
+ + + + +
+
+
+ { mode === 'text' && } + { mode === 'visual' && } +
+
+ +
+ { isSidebarOpened && } +
- { isSidebarOpened && } - -
- ); + ); + } } export default connect( @@ -62,5 +107,5 @@ export default connect( isSidebarOpened: isEditorSidebarOpened( state ), notices: getNotices( state ), } ), - { removeNotice } + { removeNotice, toggleSidebar } )( navigateRegions( Layout ) );