diff --git a/blocks/layout-grid/front.scss b/blocks/layout-grid/front.scss index 77da87e9..a5aed5d0 100644 --- a/blocks/layout-grid/front.scss +++ b/blocks/layout-grid/front.scss @@ -135,5 +135,5 @@ // Ensure inner blocks with deliberate overflows are still constrained to column. .wp-block-jetpack-layout-grid-column { - max-width: 100%; + max-width: 100%; } diff --git a/blocks/layout-grid/src/constants.js b/blocks/layout-grid/src/constants.js index aa9aee1b..f4aa8b50 100644 --- a/blocks/layout-grid/src/constants.js +++ b/blocks/layout-grid/src/constants.js @@ -14,15 +14,17 @@ function getSpacingValues() { ]; } -export const getPaddingValues = () => ( [ - { value: 'none', label: __( 'No padding', 'layout-grid' ) }, -].concat( getSpacingValues() ) ); +export const getPaddingValues = () => + [ { value: 'none', label: __( 'No padding', 'layout-grid' ) } ].concat( + getSpacingValues() + ); -export const getGutterValues = () => ( [ - { value: 'none', label: __( 'No gutter', 'layout-grid' ) }, -].concat( getSpacingValues() ) ); +export const getGutterValues = () => + [ { value: 'none', label: __( 'No gutter', 'layout-grid' ) } ].concat( + getSpacingValues() + ); -export const getColumns = () => ( [ +export const getColumns = () => [ { label: __( '1 column', 'layout-grid' ), value: 1, @@ -39,17 +41,29 @@ export const getColumns = () => ( [ label: __( '4 columns', 'layout-grid' ), value: 4, }, -] ); +]; export const DEVICE_DESKTOP = 'Desktop'; export const DEVICE_TABLET = 'Tablet'; export const DEVICE_MOBILE = 'Mobile'; -export const getLayouts = () => ( [ - { value: DEVICE_DESKTOP, label: __( 'Desktop', 'layout-grid' ), icon: desktop }, - { value: DEVICE_TABLET, label: __( 'Tablet', 'layout-grid' ), icon: tablet }, - { value: DEVICE_MOBILE, label: __( 'Mobile', 'layout-grid' ), icon: mobile }, -] ); +export const getLayouts = () => [ + { + value: DEVICE_DESKTOP, + label: __( 'Desktop', 'layout-grid' ), + icon: desktop, + }, + { + value: DEVICE_TABLET, + label: __( 'Tablet', 'layout-grid' ), + icon: tablet, + }, + { + value: DEVICE_MOBILE, + label: __( 'Mobile', 'layout-grid' ), + icon: mobile, + }, +]; export const MAX_COLUMNS = 4; diff --git a/blocks/layout-grid/src/grid/edit.js b/blocks/layout-grid/src/grid/edit.js index a1a0ec1c..c537db0b 100644 --- a/blocks/layout-grid/src/grid/edit.js +++ b/blocks/layout-grid/src/grid/edit.js @@ -12,12 +12,10 @@ import classnames from 'classnames'; import { InnerBlocks, InspectorControls, -} from '@wordpress/block-editor'; -import { Component, createRef } from '@wordpress/element'; -import { BlockControls, BlockVerticalAlignmentToolbar, } from '@wordpress/block-editor'; +import { Component, createRef } from '@wordpress/element'; import { PanelBody, TextControl, @@ -29,10 +27,6 @@ import { ToggleControl, SelectControl, Disabled, - ToolbarGroup, - MenuGroup, - MenuItem, - Dropdown, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { ENTER, SPACE } from '@wordpress/keycodes'; @@ -50,23 +44,30 @@ import { getGutterClasses, } from './css-classname'; import ColumnIcon from '../icons'; -import { getLayouts, getColumns, DEVICE_BREAKPOINTS, getSpanForDevice, getOffsetForDevice, getGutterValues } from '../constants'; +import { + getLayouts, + getColumns, + DEVICE_BREAKPOINTS, + getSpanForDevice, + getOffsetForDevice, + getGutterValues, +} from '../constants'; import { getGridWidth, getDefaultSpan } from './grid-defaults'; import ResizeGrid from './resize-grid'; import LayoutGrid from './layout-grid'; +import PreviewDevice from './preview-device'; const ALLOWED_BLOCKS = [ 'jetpack/layout-grid-column' ]; const MINIMUM_RESIZE_SIZE = 50; // Empirically determined to be a good size -// Note this uses __experimentalGetPreviewDeviceType, but has a fallback for older versions of Gutenberg. -// The fallback will be removed once WordPress contains supports for __experimentalGetPreviewDeviceType class Edit extends Component { constructor( props ) { super( props ); this.overlayRef = createRef(); this.state = { - selectedDevice: getLayouts()[ 0 ].value, + inspectorDeviceType: 'Desktop', + viewPort: 'Desktop', }; } @@ -77,60 +78,66 @@ class Edit extends Component { const columnValues = {}; for ( let pos = 0; pos < columns; pos++ ) { - for ( let device = 0; device < DEVICE_BREAKPOINTS.length; device++ ) { - const defaultSpan = getDefaultSpan( DEVICE_BREAKPOINTS[ device ], columns, pos ); - - columnValues[ getSpanForDevice( pos, DEVICE_BREAKPOINTS[ device ] ) ] = defaultSpan; - columnValues[ getOffsetForDevice( pos, DEVICE_BREAKPOINTS[ device ] ) ] = 0; + for ( + let device = 0; + device < DEVICE_BREAKPOINTS.length; + device++ + ) { + const defaultSpan = getDefaultSpan( + DEVICE_BREAKPOINTS[ device ], + columns, + pos + ); + + columnValues[ + getSpanForDevice( pos, DEVICE_BREAKPOINTS[ device ] ) + ] = defaultSpan; + columnValues[ + getOffsetForDevice( pos, DEVICE_BREAKPOINTS[ device ] ) + ] = 0; } } this.props.updateColumns( this.props.columns, columns, columnValues ); }; - getDeviceType() { - return this.props.deviceType - ? this.props.deviceType - : this.state.selectedDevice; - } - - setDeviceType = ( deviceType ) => { - if ( this.props.deviceType ) { - this.props.setDeviceType( deviceType ); - } else { - this.setState( { selectedDevice: deviceType } ); - } - }; - onResize = ( column, adjustment ) => { const { attributes, columns } = this.props; - const grid = new LayoutGrid( attributes, this.getDeviceType(), columns ); + const grid = new LayoutGrid( + attributes, + this.getPreviewMode(), + columns + ); const adjustedGrid = grid.getAdjustedGrid( column, adjustment ); if ( adjustedGrid ) { this.adjustGrid( adjustedGrid ); } - } + }; onChangeSpan = ( column, device, value ) => { const { attributes, columns } = this.props; const grid = new LayoutGrid( attributes, device, columns ); - const adjustedGrid = grid.getAdjustedGrid( column, { span: parseInt( value, 10 ) } ); + const adjustedGrid = grid.getAdjustedGrid( column, { + span: parseInt( value, 10 ), + } ); if ( adjustedGrid ) { this.adjustGrid( adjustedGrid ); } - } + }; onChangeOffset = ( column, device, value ) => { const { attributes, columns } = this.props; const grid = new LayoutGrid( attributes, device, columns ); - const adjustedGrid = grid.getAdjustedGrid( column, { start: grid.convertOffsetToStart( column, parseInt( value, 10 ) ) } ); + const adjustedGrid = grid.getAdjustedGrid( column, { + start: grid.convertOffsetToStart( column, parseInt( value, 10 ) ), + } ); if ( adjustedGrid ) { this.adjustGrid( adjustedGrid ); } - } + }; adjustGrid( grid ) { const { setAttributes, attributes } = this.props; @@ -146,12 +153,16 @@ class Edit extends Component { const settings = []; for ( let column = 0; column < columns; column++ ) { - const span = grid.getSpan( column ) || getDefaultSpan( device, columns, column ); + const span = + grid.getSpan( column ) || + getDefaultSpan( device, columns, column ); const offset = grid.getOffset( column ) || 0; - settings.push( ( + settings.push(
- { __( 'Column', 'layout-grid' ) } { column + 1 } + + { __( 'Column', 'layout-grid' ) } { column + 1 } +
this.onChangeOffset( column, device, value ) } + onChange={ ( value ) => + this.onChangeOffset( column, device, value ) + } /> this.onChangeSpan( column, device, value ) } + onChange={ ( value ) => + this.onChangeSpan( column, device, value ) + } />
- ) ); + ); } return settings; @@ -187,6 +202,37 @@ class Edit extends Component { return false; } + updateInspectorDevice( device ) { + this.setState( { inspectorDeviceType: device } ); + + // Only update if not on mobile + if ( this.state.viewPort !== 'Mobile' ) { + this.props.setPreviewDeviceType( device ); + } + } + + getPreviewMode() { + // If we're on desktop, or the preview is set to mobile, then return the preview mode + if ( + this.state.viewPort === 'Desktop' || + this.props.previewDeviceType === 'Mobile' + ) { + return this.props.previewDeviceType; + } + + // Return something appropriate for the viewport (mobile or tablet) + return this.state.viewPort; + } + + getInspectorMode() { + if ( this.state.viewPort === 'Desktop' ) { + return this.props.previewDeviceType; + } + + // Return something appropriate for the viewport (mobile or tablet) + return this.state.inspectorDeviceType; + } + render() { const { className, @@ -196,25 +242,28 @@ class Edit extends Component { setAttributes, updateAlignment, columnAttributes, + previewDeviceType, } = this.props; - const deviceType = this.getDeviceType(); + const { viewPort } = this.state; + const previewMode = this.getPreviewMode(); + const inspectorDeviceType = this.getInspectorMode(); const extra = getAsEditorCSS( - deviceType, + previewMode, columns, attributes, columnAttributes ); const { gutterSize, addGutterEnds, verticalAlignment } = attributes; - const layoutGrid = new LayoutGrid( attributes, deviceType, columns ); + const layoutGrid = new LayoutGrid( attributes, previewMode, columns ); const classes = classnames( removeGridClasses( className ), extra, { - 'wp-block-jetpack-layout-tablet': deviceType === 'Tablet', - 'wp-block-jetpack-layout-desktop': deviceType === 'Desktop', - 'wp-block-jetpack-layout-mobile': deviceType === 'Mobile', + 'wp-block-jetpack-layout-tablet': previewMode === 'Tablet', + 'wp-block-jetpack-layout-desktop': previewMode === 'Desktop', + 'wp-block-jetpack-layout-mobile': previewMode === 'Mobile', 'wp-block-jetpack-layout-resizable': this.canResizeBreakpoint( - deviceType + previewMode ), [ `are-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment, }, @@ -226,7 +275,10 @@ class Edit extends Component {