From c79d35bf46026283d444e46095f1e7d3953b10e8 Mon Sep 17 00:00:00 2001 From: retrofox Date: Thu, 5 Nov 2020 17:54:48 -0300 Subject: [PATCH 01/10] cover: add BackgroundSizeControl control --- packages/block-library/src/cover/edit.js | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js index ff9d9f8cd6eb47..b0deea868f10d9 100644 --- a/packages/block-library/src/cover/edit.js +++ b/packages/block-library/src/cover/edit.js @@ -20,6 +20,7 @@ import { Spinner, ToggleControl, withNotices, + ButtonGroup, __experimentalBoxControl as BoxControl, } from '@wordpress/components'; import { compose, withInstanceId, useInstanceId } from '@wordpress/compose'; @@ -82,6 +83,47 @@ function retrieveFastAverageColor() { return retrieveFastAverageColor.fastAverageColor; } +const backgroundSizesOptions = [ + { + slug: 'cover', + label: __( 'Cover' ), + }, + { + slug: 'contain', + label: __( 'Contain' ), + }, + { + slug: 'initial', + label: __( 'Original' ), + }, + { + slug: 'custom', + label: __( 'Custom' ), + }, +]; + +function BackgroundSizeControl( { size = 'cover', onSelect } ) { + return ( + + + { backgroundSizesOptions.map( ( { slug, label } ) => ( + + ) ) } + + + ); +} + function CoverHeightInput( { onChange, onUnitChange, @@ -360,6 +402,8 @@ function CoverEdit( { checked={ isRepeated } onChange={ toggleIsRepeated } /> + + ) } { showFocalPointPicker && ( From 4ff49a26243b141b4bc25f1e2e9ff2900d2acb38 Mon Sep 17 00:00:00 2001 From: retrofox Date: Thu, 5 Nov 2020 17:59:47 -0300 Subject: [PATCH 02/10] cover: add backgrouns size attr --- packages/block-library/src/cover/block.json | 4 ++++ packages/block-library/src/cover/edit.js | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/cover/block.json b/packages/block-library/src/cover/block.json index 29f1b17d7bb235..727677f36b8996 100644 --- a/packages/block-library/src/cover/block.json +++ b/packages/block-library/src/cover/block.json @@ -17,6 +17,10 @@ "type": "boolean", "default": false }, + "backgroundSize": { + "type": "string", + "enum": [ "cover", "contain", "initial", "custom" ] + }, "dimRatio": { "type": "number", "default": 50 diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js index b0deea868f10d9..264322f81b08b8 100644 --- a/packages/block-library/src/cover/edit.js +++ b/packages/block-library/src/cover/edit.js @@ -289,6 +289,7 @@ function CoverEdit( { contentPosition, id, backgroundType, + backgroundSize, dimRatio, focalPoint, hasParallax, @@ -341,6 +342,7 @@ function CoverEdit( { ...( isImageBackground ? backgroundImageStyles( url ) : {} ), backgroundColor: overlayColor.color, minHeight: temporaryMinHeight || minHeightWithUnit || undefined, + backgroundSize, }; if ( gradientValue && ! url ) { @@ -403,7 +405,14 @@ function CoverEdit( { onChange={ toggleIsRepeated } /> - + + setAttributes( { + backgroundSize: newSize, + } ) + } + /> ) } { showFocalPointPicker && ( From 35e451da6464a59d43276d349de7a0145383fc39 Mon Sep 17 00:00:00 2001 From: retrofox Date: Fri, 6 Nov 2020 20:10:55 -0300 Subject: [PATCH 03/10] cover: re-organize default image styles --- packages/block-library/src/cover/block.json | 1 + packages/block-library/src/cover/style.scss | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/cover/block.json b/packages/block-library/src/cover/block.json index 727677f36b8996..811a3cb57df82b 100644 --- a/packages/block-library/src/cover/block.json +++ b/packages/block-library/src/cover/block.json @@ -19,6 +19,7 @@ }, "backgroundSize": { "type": "string", + "default": "cover", "enum": [ "cover", "contain", "initial", "custom" ] }, "dimRatio": { diff --git a/packages/block-library/src/cover/style.scss b/packages/block-library/src/cover/style.scss index 1f4f09f9fc9c14..b19949fd83308b 100644 --- a/packages/block-library/src/cover/style.scss +++ b/packages/block-library/src/cover/style.scss @@ -1,10 +1,6 @@ .wp-block-cover-image, .wp-block-cover { position: relative; - background-size: cover; - background-position: center center; - min-height: 430px; - height: 100%; width: 100%; display: flex; justify-content: center; @@ -13,6 +9,13 @@ // This block has customizable padding, border-box makes that more predictable. box-sizing: border-box; + // Default settings values. + min-height: 430px; + height: 100%; + background-size: cover; + background-position: center center; + background-repeat: no-repeat; + &.has-parallax { background-attachment: fixed; @@ -31,7 +34,6 @@ &.is-repeated { background-repeat: repeat; - background-size: auto; } /** From ed37b01704dcab10fad387da8e70178558cd0fd2 Mon Sep 17 00:00:00 2001 From: retrofox Date: Fri, 13 Nov 2020 11:48:42 -0300 Subject: [PATCH 04/10] cover: add custom mode to bgsize --- packages/block-library/src/cover/block.json | 3 +- packages/block-library/src/cover/edit.js | 118 +++++++++++++------- packages/block-library/src/cover/shared.js | 19 ++++ 3 files changed, 100 insertions(+), 40 deletions(-) diff --git a/packages/block-library/src/cover/block.json b/packages/block-library/src/cover/block.json index 811a3cb57df82b..e2e544a2e49af6 100644 --- a/packages/block-library/src/cover/block.json +++ b/packages/block-library/src/cover/block.json @@ -19,8 +19,7 @@ }, "backgroundSize": { "type": "string", - "default": "cover", - "enum": [ "cover", "contain", "initial", "custom" ] + "default": "cover" }, "dimRatio": { "type": "number", diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js index 264322f81b08b8..1f090bc33f3456 100644 --- a/packages/block-library/src/cover/edit.js +++ b/packages/block-library/src/cover/edit.js @@ -21,6 +21,7 @@ import { ToggleControl, withNotices, ButtonGroup, + __experimentalUnitControl as BaseUnitControl, __experimentalBoxControl as BoxControl, } from '@wordpress/components'; import { compose, withInstanceId, useInstanceId } from '@wordpress/compose'; @@ -53,6 +54,7 @@ import { VIDEO_BACKGROUND_TYPE, COVER_MIN_HEIGHT, CSS_UNITS, + SIZE_OPTIONS, backgroundImageStyles, dimRatioToClass, isContentPositionCenter, @@ -83,52 +85,82 @@ function retrieveFastAverageColor() { return retrieveFastAverageColor.fastAverageColor; } -const backgroundSizesOptions = [ - { - slug: 'cover', - label: __( 'Cover' ), - }, - { - slug: 'contain', - label: __( 'Contain' ), - }, - { - slug: 'initial', - label: __( 'Original' ), - }, - { - slug: 'custom', - label: __( 'Custom' ), - }, -]; +function CustomSizeControl( { value, onSelect } ) { + if ( [ 'cover', 'contain', 'initial'].indexOf( value ) >= 0 ) { + return null; + } + + // Strip up and get value + units from the CSS inline. + let inlineStyleValues = value && value.split( /(%|px)/ ); + if ( inlineStyleValues.length < 2 ) { + inlineStyleValues = []; + } + + const xValue = inlineStyleValues[ 0 ] || 50; + const xUnit = inlineStyleValues[ 1 ] || 'px'; + + return ( + { + onSelect( `${ sizeValue }${ xUnit }` ); + } } + onUnitChange={ ( unit ) => { + onSelect( `${ xValue }${ unit }` ); + } } + customUnits= { false } + /> + ); +} function BackgroundSizeControl( { size = 'cover', onSelect } ) { + const isCustomSize = + SIZE_OPTIONS.map( ( opt ) => opt.slug ).indexOf( size ) < 0 || + size === 'custom'; + return ( - { backgroundSizesOptions.map( ( { slug, label } ) => ( + { SIZE_OPTIONS.map( ( { slug, label } ) => ( ) ) } + + { isCustomSize && ( + + ) } ); } -function CoverHeightInput( { +function CoverValueUnitInput( { onChange, onUnitChange, unit = 'px', value = '', + + label, + units = CSS_UNITS, + customUnits = true, } ) { const [ temporaryInput, setTemporaryInput ] = useState( null ); const instanceId = useInstanceId( UnitControl ); @@ -161,21 +193,29 @@ function CoverHeightInput( { const inputValue = temporaryInput !== null ? temporaryInput : value; const min = isPx ? COVER_MIN_HEIGHT : 0; + const controlUnitProps = { + id: inputId, + min, + onBlur: handleOnBlur, + onChange: handleOnChange, + onUnitChange, + step: '1', + style: { maxWidth: 80 }, + unit, + units, + value: inputValue, + }; + return ( - - + + { customUnits ? ( + + ) : ( + + ) } ); } @@ -407,11 +447,12 @@ function CoverEdit( { + onSelect={ ( newSize ) => { + console.log( { newSize } ); setAttributes( { backgroundSize: newSize, } ) - } + } } /> ) } @@ -452,7 +493,7 @@ function CoverEdit( { { hasBackground && ( <> - @@ -463,6 +504,7 @@ function CoverEdit( { minHeightUnit: nextUnit, } ); } } + label={ __( 'Minimum height of cover' ) } /> Date: Fri, 13 Nov 2020 13:34:59 -0300 Subject: [PATCH 05/10] cover: bgsize -> replace with icons --- packages/block-library/src/cover/edit.js | 17 +++++--- packages/block-library/src/cover/shared.js | 48 +++++++++++++++++++++ packages/block-library/src/cover/style.scss | 9 ++++ 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js index 1f090bc33f3456..3a9be11ba09895 100644 --- a/packages/block-library/src/cover/edit.js +++ b/packages/block-library/src/cover/edit.js @@ -21,7 +21,7 @@ import { ToggleControl, withNotices, ButtonGroup, - __experimentalUnitControl as BaseUnitControl, + __experimentalUnitControl as BaseUnitControl, __experimentalBoxControl as BoxControl, } from '@wordpress/components'; import { compose, withInstanceId, useInstanceId } from '@wordpress/compose'; @@ -129,19 +129,22 @@ function BackgroundSizeControl( { size = 'cover', onSelect } ) { label={ __( ' Background size' ) } id={ 'background-size' } > - - { SIZE_OPTIONS.map( ( { slug, label } ) => ( + + { SIZE_OPTIONS.map( ( { slug, label, icon: sizeIcon } ) => ( + /> ) ) } diff --git a/packages/block-library/src/cover/shared.js b/packages/block-library/src/cover/shared.js index 010583dccc4999..7b5d43eeb8862b 100644 --- a/packages/block-library/src/cover/shared.js +++ b/packages/block-library/src/cover/shared.js @@ -4,6 +4,7 @@ import { getBlobTypeByURL, isBlobURL } from '@wordpress/blob'; import { __ } from '@wordpress/i18n'; import { Platform } from '@wordpress/element'; +import { SVG, Rect, Path } from '@wordpress/components'; const POSITION_CLASSNAMES = { 'top left': 'is-position-top-left', @@ -59,18 +60,65 @@ export const SIZE_OPTIONS = [ { slug: 'cover', label: __( 'Cover' ), + icon: ( + + + + ), }, { slug: 'contain', label: __( 'Contain' ), + icon: ( + + + + + ), }, { slug: 'initial', label: __( 'Original' ), + icon: ( + + + + + ), }, { slug: 'custom', label: __( 'Custom' ), + icon: ( + + + + + ), }, ]; diff --git a/packages/block-library/src/cover/style.scss b/packages/block-library/src/cover/style.scss index b19949fd83308b..f7f23e5a626b76 100644 --- a/packages/block-library/src/cover/style.scss +++ b/packages/block-library/src/cover/style.scss @@ -182,6 +182,15 @@ object-fit: cover; } +// Tweak sidebar settings +.components-button-group.background-size-options { + display: flex; + + .components-button { + box-shadow: none; + } +} + // Styles bellow only exist to support older versions of the block. // Versions that not had inner blocks and used an h2 heading had a section (and not a div) with a class wp-block-cover-image (and not a wp-block-cover). // We are using the previous referred differences to target old versions. From 86887b37721f4d68454f2ab509a83d554e0c3fb9 Mon Sep 17 00:00:00 2001 From: retrofox Date: Fri, 13 Nov 2020 15:41:03 -0300 Subject: [PATCH 06/10] cover: bgs: store prev value --- packages/block-library/src/cover/edit.js | 44 +++++++++++++++++------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js index 3a9be11ba09895..df1ef612043688 100644 --- a/packages/block-library/src/cover/edit.js +++ b/packages/block-library/src/cover/edit.js @@ -86,18 +86,14 @@ function retrieveFastAverageColor() { } function CustomSizeControl( { value, onSelect } ) { - if ( [ 'cover', 'contain', 'initial'].indexOf( value ) >= 0 ) { - return null; - } - - // Strip up and get value + units from the CSS inline. + // Split and pick up values + units from the CSS inline. let inlineStyleValues = value && value.split( /(%|px)/ ); if ( inlineStyleValues.length < 2 ) { inlineStyleValues = []; } const xValue = inlineStyleValues[ 0 ] || 50; - const xUnit = inlineStyleValues[ 1 ] || 'px'; + const xUnit = inlineStyleValues[ 1 ] || '%'; return ( opt.slug ).indexOf( size ) < 0 || - size === 'custom'; + size === 'custom' + ); +} +function BackgroundSizeControl( { size = 'cover', onSelect } ) { return ( onSelect( slug ) } /> @@ -350,6 +355,16 @@ function CoverEdit( { const onSelectMedia = attributesFromMedia( setAttributes ); const isBlogUrl = isBlobURL( url ); + // Store the previous background custom size. + const [ prevBackgroundSize, setPrevbackgroundSize ] = useState(); + useEffect( () => { + if ( ! isCustomSize( backgroundSize ) ) { + return; + } + + setPrevbackgroundSize( backgroundSize ); + }, [ backgroundSize ] ); + const toggleParallax = () => { setAttributes( { hasParallax: ! hasParallax, @@ -451,10 +466,13 @@ function CoverEdit( { { - console.log( { newSize } ); setAttributes( { - backgroundSize: newSize, - } ) + backgroundSize: + newSize === 'custom' && + !! prevBackgroundSize + ? prevBackgroundSize + : newSize, + } ); } } /> From ba1652e58dbd1084debd46e5de1f343885c53cdf Mon Sep 17 00:00:00 2001 From: retrofox Date: Fri, 13 Nov 2020 15:43:01 -0300 Subject: [PATCH 07/10] cover: bgs: tweak custom icon --- packages/block-library/src/cover/shared.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/cover/shared.js b/packages/block-library/src/cover/shared.js index 7b5d43eeb8862b..f5b55698f3dba7 100644 --- a/packages/block-library/src/cover/shared.js +++ b/packages/block-library/src/cover/shared.js @@ -116,7 +116,7 @@ export const SIZE_OPTIONS = [ stroke="currentColor" strokeWidth="2" /> - + ), }, From 09aebb49e304165b2e65683b149a4ed1ed69a004 Mon Sep 17 00:00:00 2001 From: retrofox Date: Fri, 13 Nov 2020 20:54:59 -0300 Subject: [PATCH 08/10] cover: bgsize first editor approach --- packages/block-library/src/cover/edit.js | 173 +++++++++++++++----- packages/block-library/src/cover/shared.js | 8 +- packages/block-library/src/cover/style.scss | 27 ++- 3 files changed, 156 insertions(+), 52 deletions(-) diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js index df1ef612043688..2acc4139065a1c 100644 --- a/packages/block-library/src/cover/edit.js +++ b/packages/block-library/src/cover/edit.js @@ -21,6 +21,7 @@ import { ToggleControl, withNotices, ButtonGroup, + SelectControl, __experimentalUnitControl as BaseUnitControl, __experimentalBoxControl as BoxControl, } from '@wordpress/components'; @@ -86,32 +87,113 @@ function retrieveFastAverageColor() { } function CustomSizeControl( { value, onSelect } ) { + let initMode = value.search( /auto/ ); + if ( initMode < 1 ) { + initMode = 'both'; + } else if ( initMode === 0 ) { + initMode = 'width'; + } else { + initMode = 'height'; + } + + const [ currentMode, setMode ] = useState( initMode ); + + const customUnits = [ + { value: 'px', label: 'px', default: 0 }, + { value: '%', label: '%', default: 10 }, + ]; + // Split and pick up values + units from the CSS inline. - let inlineStyleValues = value && value.split( /(%|px)/ ); - if ( inlineStyleValues.length < 2 ) { - inlineStyleValues = []; + let wh = + value && + value + .replace( /auto/, '50%' ) + .split( /(\d+|%|px|auto)/ ) + .filter( ( v ) => !! v && v !== ' ' ); + + // default values. + if ( wh.length < 2 ) { + wh = [ '50', '%', '50', '%' ]; } - const xValue = inlineStyleValues[ 0 ] || 50; - const xUnit = inlineStyleValues[ 1 ] || '%'; + function customSizeHandler( data ) { + const { mode, w, wu, h, hu } = data; + + if ( mode ) { + setMode( mode ); + } + + let inlineStyle = ''; + switch ( mode || currentMode ) { + case 'width': + inlineStyle = `${ w || wh[ 0 ] }${ wu || wh[ 1 ] } auto`; + break; + + case 'height': + inlineStyle = `auto ${ h || wh[ 2 ] }${ hu || wh[ 3 ] }`; + break; + + case 'both': + inlineStyle = + `${ w || wh[ 0 ] }${ wu || wh[ 1 ] } ` + + `${ h || wh[ 2 ] }${ hu || wh[ 3 ] }`; + break; + } + + onSelect( inlineStyle ); + } return ( - { - onSelect( `${ sizeValue }${ xUnit }` ); - } } - onUnitChange={ ( unit ) => { - onSelect( `${ xValue }${ unit }` ); - } } - customUnits= { false } - /> + + + customSizeHandler( { mode: newMode } ) + } + labelPosition="top" + /> + +
+ { ( currentMode === 'width' || currentMode === 'both' ) && ( + customSizeHandler( { w } ) } + onUnitChange={ ( wu ) => customSizeHandler( { wu } ) } + customUnits={ false } + /> + ) } + + { ( currentMode === 'height' || currentMode === 'both' ) && ( + customSizeHandler( { h } ) } + onUnitChange={ ( hu ) => customSizeHandler( { hu } ) } + customUnits={ false } + /> + ) } +
+
); } @@ -130,33 +212,34 @@ function isCustomSize( size ) { function BackgroundSizeControl( { size = 'cover', onSelect } ) { return ( - - + - { SIZE_OPTIONS.map( ( { slug, label, icon: sizeIcon } ) => ( -