diff --git a/blocks/library/cover-image/index.js b/blocks/library/cover-image/index.js index b660ecbc26f0b..112f56ac5bd50 100644 --- a/blocks/library/cover-image/index.js +++ b/blocks/library/cover-image/index.js @@ -17,6 +17,7 @@ import BlockControls from '../../block-controls'; import BlockAlignmentToolbar from '../../block-alignment-toolbar'; import InspectorControls from '../../inspector-controls'; import ToggleControl from '../../inspector-controls/toggle-control'; +import RangeControl from '../../inspector-controls/range-control'; import BlockDescription from '../../block-description'; const { children } = source; @@ -48,9 +49,9 @@ registerBlockType( 'core/cover-image', { type: 'boolean', default: false, }, - hasBackgroundDim: { - type: 'boolean', - default: true, + dimRatio: { + type: 'number', + default: 50, }, }, @@ -62,18 +63,22 @@ registerBlockType( 'core/cover-image', { }, edit( { attributes, setAttributes, focus, setFocus, className } ) { - const { url, title, align, id, hasParallax, hasBackgroundDim } = attributes; + const { url, title, align, id, hasParallax, dimRatio } = attributes; const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } ); const onSelectImage = ( media ) => setAttributes( { url: media.url, id: media.id } ); const toggleParallax = () => setAttributes( { hasParallax: ! hasParallax } ); - const toggleBackgroundDim = () => setAttributes( { hasBackgroundDim: ! hasBackgroundDim } ); + const setDimRatio = ( ratio ) => setAttributes( { dimRatio: ratio } ); const style = url ? { backgroundImage: `url(${ url })` } : undefined; - const classes = classnames( className, { - 'has-parallax': hasParallax, - 'has-background-dim': hasBackgroundDim, - } ); + const classes = classnames( + className, + dimRatioToClass( dimRatio ), + { + 'has-background-dim': dimRatio !== 0, + 'has-parallax': hasParallax, + } + ); const controls = focus && [ @@ -108,10 +113,13 @@ registerBlockType( 'core/cover-image', { checked={ !! hasParallax } onChange={ toggleParallax } /> - , ]; @@ -161,14 +169,18 @@ registerBlockType( 'core/cover-image', { }, save( { attributes, className } ) { - const { url, title, hasParallax, hasBackgroundDim } = attributes; + const { url, title, hasParallax, dimRatio } = attributes; const style = url ? { backgroundImage: `url(${ url })` } : undefined; - const classes = classnames( className, { - 'has-parallax': hasParallax, - 'has-background-dim': hasBackgroundDim, - } ); + const classes = classnames( + className, + dimRatioToClass( dimRatio ), + { + 'has-background-dim': dimRatio !== 0, + 'has-parallax': hasParallax, + } + ); return (
@@ -177,3 +189,9 @@ registerBlockType( 'core/cover-image', { ); }, } ); + +function dimRatioToClass( ratio ) { + return ( ratio === 0 || ratio === 50 ) + ? null + : 'has-background-dim-' + ( 10 * Math.round( ratio / 10 ) ); +} diff --git a/blocks/library/cover-image/style.scss b/blocks/library/cover-image/style.scss index a66394768c7a0..a9934a93fbc98 100644 --- a/blocks/library/cover-image/style.scss +++ b/blocks/library/cover-image/style.scss @@ -29,7 +29,13 @@ left: 0; bottom: 0; right: 0; - background: rgba( 0,0,0,.5 ); + background: rgba( black, 0.5 ); + } + + @for $i from 1 through 10 { + &.has-background-dim.has-background-dim-#{ $i * 10 }:before { + background-color: rgba( black, $i * 0.1 ); + } } &.components-placeholder { diff --git a/blocks/test/fixtures/core__cover-image.html b/blocks/test/fixtures/core__cover-image.html index 1cf586ed87ef0..de557f4357676 100644 --- a/blocks/test/fixtures/core__cover-image.html +++ b/blocks/test/fixtures/core__cover-image.html @@ -1,5 +1,5 @@ - -
+ +

Guten Berg!

diff --git a/blocks/test/fixtures/core__cover-image.json b/blocks/test/fixtures/core__cover-image.json index afd626b6a3464..a8b2f95c682ef 100644 --- a/blocks/test/fixtures/core__cover-image.json +++ b/blocks/test/fixtures/core__cover-image.json @@ -9,8 +9,8 @@ ], "url": "https://cldup.com/uuUqE_dXzy.jpg", "hasParallax": false, - "hasBackgroundDim": true + "dimRatio": 40 }, - "originalContent": "
\n

Guten Berg!

\n
" + "originalContent": "
\n

Guten Berg!

\n
" } ] diff --git a/blocks/test/fixtures/core__cover-image.parsed.json b/blocks/test/fixtures/core__cover-image.parsed.json index bab2982cf3631..9d337ce943ef1 100644 --- a/blocks/test/fixtures/core__cover-image.parsed.json +++ b/blocks/test/fixtures/core__cover-image.parsed.json @@ -3,9 +3,9 @@ "blockName": "core/cover-image", "attrs": { "url": "https://cldup.com/uuUqE_dXzy.jpg", - "hasBackgroundDim": true + "dimRatio": 40 }, - "rawContent": "\n
\n

Guten Berg!

\n
\n" + "rawContent": "\n
\n

Guten Berg!

\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core__cover-image.serialized.html b/blocks/test/fixtures/core__cover-image.serialized.html index 9a31a18f8cf4c..de557f4357676 100644 --- a/blocks/test/fixtures/core__cover-image.serialized.html +++ b/blocks/test/fixtures/core__cover-image.serialized.html @@ -1,5 +1,5 @@ - -
+ +

Guten Berg!