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

Post Featured Image block: Enable gradient overlay #43838

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
38d1b54
Feat: Add block attributes related to the overlay
RahiDroid Sep 2, 2022
6dc42cf
Feat: Add overlay settings and markup for editor
RahiDroid Sep 2, 2022
360194c
Feat: Add overlay markup for the front-end
RahiDroid Sep 2, 2022
1bbb77b
Feat: Add styles for the overlay
RahiDroid Sep 2, 2022
9725c48
Feat: Add custom gradient on front-end
RahiDroid Sep 2, 2022
d45e191
Refact: Use overlay instead of background for variable names
RahiDroid Sep 2, 2022
d24db87
Chore: Remove default opacity
RahiDroid Sep 3, 2022
285e1bd
Feat: Export the Overlay settings as a component
RahiDroid Sep 3, 2022
97294df
Feat: Add the Overlay settings in the site editor
RahiDroid Sep 3, 2022
e3f710c
Refact: Set default overlay opacity to 0
RahiDroid Sep 3, 2022
7519d44
Refact: Export the overlay markup in the separate component
RahiDroid Sep 4, 2022
c4f28e1
Feat: Export the utility function to a separate file
RahiDroid Sep 4, 2022
a67c73a
Fix: Add missing custom overlay attribute
RahiDroid Sep 4, 2022
3734817
Fix: Links not working because of the overlay
RahiDroid Sep 4, 2022
def7a85
Fix: The upload button on placeholder not working
RahiDroid Sep 4, 2022
fb88967
Merge branch 'WordPress:trunk' into add/overlay-on-featured-image-block
RahiDroid Sep 4, 2022
4375805
Refact: Rename variable to a shorten name
RahiDroid Sep 4, 2022
86e4ecd
Chore: Remove unnecessary brackets
RahiDroid Sep 4, 2022
ecd6c3e
Refact: Remove duplicate style property
RahiDroid Sep 4, 2022
33e82a0
Feat: Add overlay when block is used inside query loop
RahiDroid Sep 4, 2022
b1e40ec
Fix: Missing attribute in unit test and doc
RahiDroid Sep 4, 2022
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ Display a post's featured image. ([Source](https://github.com/WordPress/gutenber
- **Name:** core/post-featured-image
- **Category:** theme
- **Supports:** align (center, full, left, right, wide), color (~~background~~, ~~text~~), spacing (margin, padding), ~~html~~
- **Attributes:** height, isLink, linkTarget, rel, scale, sizeSlug, width
- **Attributes:** customGradient, customOverlayColor, dimRatio, gradient, height, isLink, linkTarget, overlayColor, rel, scale, sizeSlug, width

## Post Navigation Link

Expand Down
16 changes: 16 additions & 0 deletions packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@
"linkTarget": {
"type": "string",
"default": "_self"
},
"overlayColor": {
"type": "string"
},
"customOverlayColor": {
"type": "string"
},
"dimRatio": {
"type": "number",
"default": 0
},
"gradient": {
"type": "string"
},
"customGradient": {
"type": "string"
}
},
"usesContext": [ "postId", "postType", "queryId" ],
Expand Down
24 changes: 22 additions & 2 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { store as noticesStore } from '@wordpress/notices';
* Internal dependencies
*/
import DimensionControls from './dimension-controls';
import Overlay from './overlay';

const ALLOWED_MEDIA_TYPES = [ 'image' ];

Expand Down Expand Up @@ -170,7 +171,14 @@ function PostFeaturedImageDisplay( {
return (
<>
{ controls }
<div { ...blockProps }>{ placeholder() }</div>
<div { ...blockProps }>
{ placeholder() }
<Overlay
attributes={ attributes }
setAttributes={ setAttributes }
clientId={ clientId }
/>
</div>
</>
);
}
Expand Down Expand Up @@ -247,7 +255,14 @@ function PostFeaturedImageDisplay( {
</MediaReplaceFlow>
</BlockControls>
) }
<figure { ...blockProps }>{ image }</figure>
<figure { ...blockProps }>
{ image }
<Overlay
attributes={ attributes }
setAttributes={ setAttributes }
clientId={ clientId }
/>
</figure>
</>
);
}
Expand All @@ -267,6 +282,11 @@ export default function PostFeaturedImageEdit( props ) {
withIllustration={ true }
style={ borderProps.style }
/>
<Overlay
attributes={ props.attributes }
setAttributes={ props.setAttributes }
clientId={ props.clientId }
/>
</div>
);
}
Expand Down
147 changes: 77 additions & 70 deletions packages/block-library/src/post-featured-image/editor.scss
Original file line number Diff line number Diff line change
@@ -1,85 +1,92 @@
// Provide special styling for the placeholder.
// @todo: this particular minimal style of placeholder could be componentized further.
.wp-block-post-featured-image.wp-block-post-featured-image {
// Style the placeholder.
.wp-block-post-featured-image__placeholder,
.components-placeholder {
justify-content: center;
align-items: center;
padding: 0;

// Hide the upload button, as it's also available in the media library.
.components-form-file-upload {
display: none;
}
.wp-block-post-featured-image {
.block-editor-media-placeholder {
z-index: 1; // Need to put it above the overlay so the upload button works.
backdrop-filter: none; // Removes background blur so the overlay's actual color is visible.
RahiDroid marked this conversation as resolved.
Show resolved Hide resolved
}

// Style the upload button.
.components-button.components-button {
padding: 0;
display: flex;
&.wp-block-post-featured-image {
// Style the placeholder.
.wp-block-post-featured-image__placeholder,
.components-placeholder {
justify-content: center;
align-items: center;
width: $grid-unit-60;
height: $grid-unit-60;
border-radius: 50%;
position: relative;
background: var(--wp-admin-theme-color);
border-color: var(--wp-admin-theme-color);
border-style: solid;
color: $white;
padding: 0;

> svg {
color: inherit;
// Hide the upload button, as it's also available in the media library.
.components-form-file-upload {
display: none;
}
}

// Show default placeholder height when not resized.
min-height: 200px;
// Style the upload button.
.components-button.components-button {
padding: 0;
display: flex;
justify-content: center;
align-items: center;
width: $grid-unit-60;
height: $grid-unit-60;
border-radius: 50%;
position: relative;
background: var(--wp-admin-theme-color);
border-color: var(--wp-admin-theme-color);
border-style: solid;
color: $white;

// The following override the default placeholder styles that remove
// its border so that a user selection for border color or width displays
// a visual border.
&:where(.has-border-color) {
border-style: solid;
}
&:where([style*="border-top-color"]) {
border-top-style: solid;
}
&:where([style*="border-right-color"]) {
border-right-style: solid;
}
&:where([style*="border-bottom-color"]) {
border-bottom-style: solid;
}
&:where([style*="border-left-color"]) {
border-left-style: solid;
}
> svg {
color: inherit;
}
}

&:where([style*="border-width"]) {
border-style: solid;
}
&:where([style*="border-top-width"]) {
border-top-style: solid;
}
&:where([style*="border-right-width"]) {
border-right-style: solid;
}
&:where([style*="border-bottom-width"]) {
border-bottom-style: solid;
}
&:where([style*="border-left-width"]) {
border-left-style: solid;
// Show default placeholder height when not resized.
min-height: 200px;

// The following override the default placeholder styles that remove
// its border so that a user selection for border color or width displays
// a visual border.
&:where(.has-border-color) {
border-style: solid;
}
&:where([style*="border-top-color"]) {
border-top-style: solid;
}
&:where([style*="border-right-color"]) {
border-right-style: solid;
}
&:where([style*="border-bottom-color"]) {
border-bottom-style: solid;
}
&:where([style*="border-left-color"]) {
border-left-style: solid;
}

&:where([style*="border-width"]) {
border-style: solid;
}
&:where([style*="border-top-width"]) {
border-top-style: solid;
}
&:where([style*="border-right-width"]) {
border-right-style: solid;
}
&:where([style*="border-bottom-width"]) {
border-bottom-style: solid;
}
&:where([style*="border-left-width"]) {
border-left-style: solid;
}
}
}

// Provide a minimum size for the placeholder when resized.
// Note, this should be as small as we can afford it, and exists only
// to ensure there's room for the upload button.
&[style*="height"] .components-placeholder {
min-height: $grid-unit-60;
min-width: $grid-unit-60;
height: 100%;
width: 100%;
// Provide a minimum size for the placeholder when resized.
// Note, this should be as small as we can afford it, and exists only
// to ensure there's room for the upload button.
&[style*="height"] .components-placeholder {
min-height: $grid-unit-60;
min-width: $grid-unit-60;
height: 100%;
width: 100%;
}
}
}

Expand Down
95 changes: 88 additions & 7 deletions packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
}
$post_ID = $block->context['postId'];

$is_link = isset( $attributes['isLink'] ) && $attributes['isLink'];
$size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
$post_title = trim( strip_tags( get_the_title( $post_ID ) ) );
$attr = get_block_core_post_featured_image_border_attributes( $attributes );
$is_link = isset( $attributes['isLink'] ) && $attributes['isLink'];
$size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
$post_title = trim( strip_tags( get_the_title( $post_ID ) ) );
$attr = get_block_core_post_featured_image_border_attributes( $attributes );
$overlay_markup = get_block_core_post_featured_image_overlay_element_markup( $attributes );

if ( $is_link ) {
$attr['alt'] = $post_title;
Expand All @@ -36,13 +37,22 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
if ( $is_link ) {
$link_target = $attributes['linkTarget'];
$rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';
$featured_image = sprintf( '<a href="%1$s" target="%2$s" %3$s>%4$s</a>', get_the_permalink( $post_ID ), esc_attr( $link_target ), $rel, $featured_image );
$featured_image = sprintf(
'<a href="%1$s" target="%2$s" %3$s>%4$s%5$s</a>',
get_the_permalink( $post_ID ),
esc_attr( $link_target ),
$rel,
$featured_image,
$overlay_markup
);
} else {
$featured_image = $featured_image . $overlay_markup;
}

$has_width = ! empty( $attributes['width'] );
$has_height = ! empty( $attributes['height'] );
if ( ! $has_height && ! $has_width ) {
return "<figure $wrapper_attributes>$featured_image</figure>";
return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
}

if ( $has_width ) {
Expand All @@ -57,7 +67,78 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
$featured_image = str_replace( 'src=', 'style="' . esc_attr( $image_styles ) . '" src=', $featured_image );
}

return "<figure $wrapper_attributes>$featured_image</figure>";
return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
}

/**
* Generate markup for the HTML element that will be used for the overlay.
*
* @param array $attributes Block attributes.
*
* @return string HTML markup in string format.
*/
function get_block_core_post_featured_image_overlay_element_markup( $attributes ) {
$has_dim_background = isset( $attributes['dimRatio'] ) && $attributes['dimRatio'];
$has_gradient = isset( $attributes['gradient'] ) && $attributes['gradient'];
$has_custom_gradient = isset( $attributes['customGradient'] ) && $attributes['customGradient'];
$has_solid_overlay = isset( $attributes['overlayColor'] ) && $attributes['overlayColor'];
$has_custom_overlay = isset( $attributes['customOverlayColor'] ) && $attributes['customOverlayColor'];
$class_names = array(
'wp-block-post-featured-image__overlay',
);
$styles_properties = array();

if ( ! $has_dim_background ) {
return '';
}

// Generate required classes for the element.
if ( $has_dim_background ) {
$class_names[] = 'has-background-dim';
$class_names[] = "has-background-dim-{$attributes['dimRatio']}";
}

if ( $has_solid_overlay ) {
$class_names[] = "has-{$attributes['overlayColor']}-background-color";
}

if ( $has_gradient || $has_custom_gradient ) {
$class_names[] = 'has-background-gradient';
}

if ( $has_gradient ) {
$class_names[] = "has-{$attributes['gradient']}-gradient-background";
}

// Generate required CSS properties and their values.
if ( ! empty( $attributes['style']['border']['radius'] ) ) {
$styles_properties['border-radius'] = $attributes['style']['border']['radius'];
}

if ( ! empty( $attributes['style']['border']['width'] ) ) {
$styles_properties['border-width'] = $attributes['style']['border']['width'];
}

if ( $has_custom_gradient ) {
$styles_properties['background-image'] = $attributes['customGradient'];
}

if ( $has_custom_overlay ) {
$styles_properties['background-color'] = $attributes['customOverlayColor'];
}

$styles = '';

foreach ( $styles_properties as $style_attribute => $style_attribute_value ) {
$styles .= "{$style_attribute}: $style_attribute_value; ";
}

return sprintf(
'<span class="%s" style="%s" aria-hidden="true"></span>',
esc_attr( implode( ' ', $class_names ) ),
esc_attr( trim( $styles ) )
);

}

/**
Expand Down
Loading