diff --git a/editor/layout/index.js b/editor/layout/index.js
index d1bc65167f406..2084cf532f43c 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 ) );