Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cover image markup and CSS #3444

Merged
merged 6 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions blocks/library/cover-image/editor.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
.wp-block-cover-image {
margin: 0;

.blocks-rich-text__tinymce[data-is-empty="true"]:before {
position: inherit;
}

.blocks-rich-text__tinymce a {
color: white;
}

.blocks-rich-text__tinymce:focus a[data-mce-selected] {
padding: 0 2px;
margin: 0 -2px;
Expand Down
118 changes: 78 additions & 40 deletions blocks/library/cover-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ import InspectorControls from '../../inspector-controls';

const validAlignments = [ 'left', 'center', 'right', 'wide', 'full' ];

const blockAttributes = {
title: {
type: 'array',
source: 'children',
selector: 'p',
},
url: {
type: 'string',
},
align: {
type: 'string',
},
contentAlign: {
type: 'string',
default: 'center',
},
id: {
type: 'number',
},
hasParallax: {
type: 'boolean',
default: false,
},
dimRatio: {
type: 'number',
default: 50,
},
};

export const name = 'core/cover-image';

export const settings = {
Expand All @@ -37,34 +66,7 @@ export const settings = {

category: 'common',

attributes: {
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
url: {
type: 'string',
},
align: {
type: 'string',
},
contentAlign: {
type: 'string',
default: 'center',
},
id: {
type: 'number',
},
hasParallax: {
type: 'boolean',
default: false,
},
dimRatio: {
type: 'number',
default: 50,
},
},
attributes: blockAttributes,

transforms: {
from: [
Expand Down Expand Up @@ -101,9 +103,7 @@ export const settings = {
const toggleParallax = () => setAttributes( { hasParallax: ! hasParallax } );
const setDimRatio = ( ratio ) => setAttributes( { dimRatio: ratio } );

const style = url ?
{ backgroundImage: `url(${ url })` } :
undefined;
const style = backgroundImageStyles( url );
const classes = classnames(
className,
contentAlign !== 'center' && `has-${ contentAlign }-content`,
Expand Down Expand Up @@ -190,31 +190,30 @@ export const settings = {

return [
controls,
<section
<div
key="preview"
data-url={ url }
style={ style }
className={ classes }
>
{ title || isSelected ? (
<RichText
tagName="h2"
tagName="p"
className="wp-block-cover-image-text"
placeholder={ __( 'Write title…' ) }
value={ title }
onChange={ ( value ) => setAttributes( { title: value } ) }
isSelected={ isSelected }
inlineToolbar
/>
) : null }
</section>,
</div>,
];
},

save( { attributes, className } ) {
const { url, title, hasParallax, dimRatio, align, contentAlign } = attributes;
const style = url ?
{ backgroundImage: `url(${ url })` } :
undefined;
const style = backgroundImageStyles( url );
const classes = classnames(
className,
dimRatioToClass( dimRatio ),
Expand All @@ -227,15 +226,54 @@ export const settings = {
);

return (
<section className={ classes } style={ style }>
<h2>{ title }</h2>
</section>
<div className={ classes } style={ style }>
{ title && title.length > 0 && (
<p className="wp-block-cover-image-text">{ title }</p>
) }
</div>
);
},

deprecated: [ {
attributes: {
...blockAttributes,
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
},

save( { attributes, className } ) {
const { url, title, hasParallax, dimRatio, align } = attributes;
const style = backgroundImageStyles( url );
const classes = classnames(
className,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
},
align ? `align${ align }` : null,
);

return (
<section className={ classes } style={ style }>
<h2>{ title }</h2>
</section>
);
},
} ],
};

function dimRatioToClass( ratio ) {
return ( ratio === 0 || ratio === 50 ) ?
null :
'has-background-dim-' + ( 10 * Math.round( ratio / 10 ) );
}

function backgroundImageStyles( url ) {
return url ?
{ backgroundImage: `url(${ url })` } :
undefined;
}
29 changes: 21 additions & 8 deletions blocks/library/cover-image/style.scss
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
.wp-block-cover-image {
position: relative;
background-size: cover;
height: 430px;
min-height: 430px;
width: 100%;
margin: 0;
margin: 0 0 1.5em 0;
display: flex;
justify-content: center;
align-items: center;

&.has-left-content {
justify-content: flex-start;
h2 {

h2,
.wp-block-cover-image-text {
margin-left: 0;
text-align: left;
}
}

&.has-right-content {
justify-content: flex-end;
h2 {

h2,
.wp-block-cover-image-text {
margin-right: 0;
text-align: right;
}
}

h2 {
h2,
.wp-block-cover-image-text {
color: white;
font-size: 24pt;
line-height: 1em;
font-size: 2em;
line-height: 1.25;
z-index: 1;
margin-bottom: 0;
max-width: $visual-editor-max-width;
padding: $block-padding;
text-align: center;

a,
a:hover,
a:focus,
a:active {
color: white;
}
}

&.has-parallax {
Expand All @@ -45,7 +58,7 @@
left: 0;
bottom: 0;
right: 0;
background: rgba( black, 0.5 );
background-color: rgba( black, 0.5 );
}

@for $i from 1 through 10 {
Expand Down
6 changes: 3 additions & 3 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","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>
<div class="wp-block-cover-image has-background-dim-40 has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<p class="wp-block-cover-image-text">Guten Berg!</p>
</div>
<!-- /wp:core/cover-image -->
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__cover-image.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"dimRatio": 40
},
"innerBlocks": [],
"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>"
"originalContent": "<div class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <p class=\"wp-block-cover-image-text\">Guten Berg!</p>\n</div>"
}
]
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__cover-image.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dimRatio": 40
},
"innerBlocks": [],
"innerHTML": "\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"
"innerHTML": "\n<div class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <p class=\"wp-block-cover-image-text\">Guten Berg!</p>\n</div>\n"
},
{
"attrs": {},
Expand Down
6 changes: 3 additions & 3 deletions blocks/test/fixtures/core__cover-image.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp: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>
<div class="wp-block-cover-image has-background-dim-40 has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<p class="wp-block-cover-image-text">Guten Berg!</p>
</div>
<!-- /wp:cover-image -->
2 changes: 1 addition & 1 deletion post-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ window._wpGutenbergPost.title = {
window._wpGutenbergPost.content = {
raw: [
'<!-- wp:cover-image {"url":"https://cldup.com/Fz-ASbo2s3.jpg","align":"wide"} -->',
'<section class="wp-block-cover-image has-background-dim alignwide" style="background-image:url(https://cldup.com/Fz-ASbo2s3.jpg)"><h2>Of Mountains &amp; Printing Presses</h2></section>',
'<div class="wp-block-cover-image has-background-dim alignwide" style="background-image:url(https://cldup.com/Fz-ASbo2s3.jpg)"><p class="wp-block-cover-image-text">Of Mountains &amp; Printing Presses</p></div>',
'<!-- /wp:cover-image -->',

'<!-- wp:paragraph -->',
Expand Down