Skip to content

Commit

Permalink
Cover Image: Add range slider for background dim (#2815)
Browse files Browse the repository at this point in the history
* Cover Image: Replace hasBackgroundDim toggle with slider control

* Cover Image: Ensure backcompat via default attributes
  • Loading branch information
mcsf authored Oct 2, 2017
1 parent 8df730f commit eb25d78
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
54 changes: 36 additions & 18 deletions blocks/library/cover-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,9 +49,9 @@ registerBlockType( 'core/cover-image', {
type: 'boolean',
default: false,
},
hasBackgroundDim: {
type: 'boolean',
default: true,
dimRatio: {
type: 'number',
default: 50,
},
},

Expand All @@ -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 && [
<BlockControls key="controls">
Expand Down Expand Up @@ -108,10 +113,13 @@ registerBlockType( 'core/cover-image', {
checked={ !! hasParallax }
onChange={ toggleParallax }
/>
<ToggleControl
label={ __( 'Dim Background' ) }
checked={ !! hasBackgroundDim }
onChange={ toggleBackgroundDim }
<RangeControl
label={ __( 'Background Dimness' ) }
value={ dimRatio }
onChange={ setDimRatio }
min={ 0 }
max={ 100 }
step={ 10 }
/>
</InspectorControls>,
];
Expand Down Expand Up @@ -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 (
<section className={ classes } style={ style }>
Expand All @@ -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 ) );
}
8 changes: 7 additions & 1 deletion blocks/library/cover-image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__cover-image.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","hasBackgroundDim":true} -->
<section class="wp-block-cover-image has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg);">
<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","dimRatio":40} -->
<section class="wp-block-cover-image has-background-dim-40 has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg);">
<h2>Guten Berg!</h2>
</section>
<!-- /wp:core/cover-image -->
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__cover-image.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
],
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"hasParallax": false,
"hasBackgroundDim": true
"dimRatio": 40
},
"originalContent": "<section class=\"wp-block-cover-image has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg);\">\n <h2>Guten Berg!</h2>\n</section>"
"originalContent": "<section class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg);\">\n <h2>Guten Berg!</h2>\n</section>"
}
]
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__cover-image.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"blockName": "core/cover-image",
"attrs": {
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"hasBackgroundDim": true
"dimRatio": 40
},
"rawContent": "\n<section class=\"wp-block-cover-image has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg);\">\n <h2>Guten Berg!</h2>\n</section>\n"
"rawContent": "\n<section class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg);\">\n <h2>Guten Berg!</h2>\n</section>\n"
},
{
"attrs": {},
Expand Down
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__cover-image.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg"} -->
<section class="wp-block-cover-image has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg);">
<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","dimRatio":40} -->
<section class="wp-block-cover-image has-background-dim-40 has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg);">
<h2>Guten Berg!</h2>
</section>
<!-- /wp:core/cover-image -->

0 comments on commit eb25d78

Please sign in to comment.