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 excerpt block - add color, fontSize, lineHeight, and text alignment. #23945

Merged
14 changes: 12 additions & 2 deletions packages/block-library/src/post-excerpt/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "core/post-excerpt",
"category": "design",
"attributes": {
"align": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, this should most likely not be called align, but rather textAlign. I know align is how the Paragraph block does it, but this is confusing since there is also block alignment, which (if using the supports flag) uses align as its attribute name.

Copy link
Contributor Author

@Addison-Stavlo Addison-Stavlo Jul 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, textAlign or alignText would be preferable (I was following the paragraph block's lead at first here). I saw the align support hook is for block alignment as well and noticed the confusion. I wonder how many other blocks have this conflicted naming convention, but we should start updating them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or would it be better to similarly update the align hook to be blockAlign or something similar?

If this is more recent than some of the blocks that use align for textAlign, then less people should be effected by the change. That is, Im assuming if we updated the paragraph block's attr name then a user who updates their Gutenberg version will have whatever alignment settings they saved lost?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a compatibility layer for changing a block attribute name? I think there is something for updating a block to a newer version without loosing settings 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a compatibility layer for changing a block attribute name? I think there is something for updating a block to a newer version without loosing settings 🤔

That would be good, at the very least for now I will update the FSE blocks that are using align for text alignment to use textAlign

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is indeed a way to easily handle (what would otherwise be backward-incompatible) changes in block attribute schemas:

https://developer.wordpress.org/block-editor/developers/block-api/block-deprecation/

A lot of core blocks already make use this, storing their deprecated versions in a deprecated.js file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated for the FSE blocks specifically - #24077

"type": "string"
},
"wordCount": {
"type": "number",
"default": 55
Expand All @@ -15,9 +18,16 @@
}
},
"usesContext": [
"postId"
"postId",
"postType"
],
"supports": {
"html": false
"html": false,
"__experimentalFontSize": true,
"__experimentalColor": {
"gradients": true,
"linkColor": true
},
"__experimentalLineHeight": true
}
}
122 changes: 71 additions & 51 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { useEntityProp, useEntityId } from '@wordpress/core-data';
import { useEntityProp } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';
import { InspectorControls, RichText } from '@wordpress/block-editor';
import {
AlignmentToolbar,
BlockControls,
InspectorControls,
RichText,
} from '@wordpress/block-editor';
import { PanelBody, RangeControl, ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

function usePostContentExcerpt( wordCount ) {
function usePostContentExcerpt( wordCount, postId, postType ) {
const [ , , { raw: rawPostContent } ] = useEntityProp(
'postType',
'post',
'content'
postType,
'content',
postId
);
return useMemo( () => {
if ( ! rawPostContent ) {
Expand All @@ -25,19 +36,33 @@ function usePostContentExcerpt( wordCount ) {
}, [ rawPostContent, wordCount ] );
}

function PostExcerptEditor( {
attributes: { wordCount, moreText, showMoreOnNewLine },
export default function PostExcerptEdit( {
attributes: { align, wordCount, moreText, showMoreOnNewLine },
setAttributes,
isSelected,
context: { postId, postType },
} ) {
const [ excerpt, setExcerpt ] = useEntityProp(
'postType',
'post',
'excerpt'
postType,
'excerpt',
postId
);
const postContentExcerpt = usePostContentExcerpt(
wordCount,
postId,
postType
);
const postContentExcerpt = usePostContentExcerpt( wordCount );
return (
<>
<BlockControls>
<AlignmentToolbar
value={ align }
onChange={ ( newAlign ) =>
setAttributes( { align: newAlign } )
}
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Post Excerpt Settings' ) }>
{ ! excerpt && (
Expand All @@ -62,19 +87,40 @@ function PostExcerptEditor( {
/>
</PanelBody>
</InspectorControls>
<RichText
className={
! showMoreOnNewLine &&
'wp-block-post-excerpt__excerpt is-inline'
}
placeholder={ postContentExcerpt }
value={ excerpt || ( isSelected ? '' : postContentExcerpt ) }
onChange={ setExcerpt }
keepPlaceholderOnFocus
/>
{ ! showMoreOnNewLine && ' ' }
{ showMoreOnNewLine ? (
<p>
<div
className={ classnames( 'wp-block-post-excerpt', {
[ `has-text-align-${ align }` ]: align,
} ) }
>
<RichText
className={
! showMoreOnNewLine &&
'wp-block-post-excerpt__excerpt is-inline'
}
placeholder={ postContentExcerpt }
value={
excerpt ||
( isSelected
? ''
: postContentExcerpt ||
__( 'No post excerpt found' ) )
}
onChange={ setExcerpt }
keepPlaceholderOnFocus
/>
{ ! showMoreOnNewLine && ' ' }
{ showMoreOnNewLine ? (
<p className="wp-block-post-excerpt__more-text">
<RichText
tagName="a"
placeholder={ __( 'Read more…' ) }
value={ moreText }
onChange={ ( newMoreText ) =>
setAttributes( { moreText: newMoreText } )
}
/>
</p>
) : (
<RichText
tagName="a"
placeholder={ __( 'Read more…' ) }
Expand All @@ -83,34 +129,8 @@ function PostExcerptEditor( {
setAttributes( { moreText: newMoreText } )
}
/>
</p>
) : (
<RichText
tagName="a"
placeholder={ __( 'Read more…' ) }
value={ moreText }
onChange={ ( newMoreText ) =>
setAttributes( { moreText: newMoreText } )
}
/>
) }
) }
</div>
</>
);
}

export default function PostExcerptEdit( {
attributes,
setAttributes,
isSelected,
} ) {
if ( ! useEntityId( 'postType', 'post' ) ) {
return __( 'Post Excerpt' );
}
return (
<PostExcerptEditor
attributes={ attributes }
setAttributes={ setAttributes }
isSelected={ isSelected }
/>
);
}
13 changes: 11 additions & 2 deletions packages/block-library/src/post-excerpt/editor.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
.wp-block-post-excerpt__excerpt.is-inline {
display: inline-block;
.wp-block-post-excerpt {

.wp-block-post-excerpt__excerpt.is-inline {
display: inline-block;
}


}

.wp-block[data-type="core/post-excerpt"].has-link-color .wp-block-post-excerpt a {
color: var(--wp--style--color--link);
}
11 changes: 8 additions & 3 deletions packages/block-library/src/post-excerpt/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) {
$filter_excerpt_length
);

$output = '<p>' . get_the_excerpt( $block->context['postId'] );
$classes = 'wp-block-post-excerpt';
if ( isset( $attributes['align'] ) ) {
$classes .= ' has-text-align-' . $attributes['align'];
}

$output = sprintf( '<div class="%1$s">', esc_attr( $classes ) ) . '<p class="wp-block-post-excerpt__excerpt">' . get_the_excerpt( $block->context['postId'] );
if ( ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'] ) {
$output .= '</p>' . '<p>' . $more_text . '</p>';
$output .= '</p>' . '<p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>';
} else {
$output .= ' ' . $more_text . '</p>';
$output .= ' ' . $more_text . '</p>' . '</div>';
}

remove_filter(
Expand Down
10 changes: 10 additions & 0 deletions packages/block-library/src/post-excerpt/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.wp-block-post-excerpt {
.wp-block-post-excerpt__excerpt,
.wp-block-post-excerpt__more-text {
line-height: inherit;
}
}

.wp-block-post-excerpt.has-link-color a {
color: var(--wp--style--color--link) !important;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need !important ?

Copy link
Contributor Author

@Addison-Stavlo Addison-Stavlo Jul 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or more specificity. Since there are a handful of nested elements in this block theme supplied generic link style rules overwrite our link color selection when we also have a background color applied.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an example of rule with higher-specificity and where does it come from? (Just to help with the reasoning)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing (maybe there is a better way to deal with this), but currently I am testing the link colors with the Seedlet-Blocks theme (I can't seem to get the option to show up for 2020/2019-blocks). When a background is applied, the link color selection stops working because of this rule:

Screen Shot 2020-07-16 at 11 10 56 AM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, this is an area where the theme should adapt its styles. Ideally, with global styles (and the link color feature is part of global styles), the themes don't provide as much detailed CSS but it's more "managed".

Copy link
Contributor Author

@Addison-Stavlo Addison-Stavlo Jul 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about this as well - Theme styles being too restrictive vs. block styles not defining enough. Although, I was having trouble finding another theme where I could test link colors anymore (I remember previously testing them on one of the 2020/2019/blocks themes but they don't seem present anymore?).

Since link colors in simple blocks such as paragraphs work on Seedlet, I figured these more complex/nested element blocks should adapt their styles accordingly. After all has-background a does not seem like a very opinionated rule, and makes sense for a theme to supply a default style for links used in background areas. So perhaps blocks like this need some sort of rule to ensure that if a custom link color is applied at the block level, the nested elements are explicitly given a style rule to properly show that selection?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a Seedlet theme bug, though.
That :not( .has-background-background-color ) increases the specificity enough to override the block's link color.

If you try to remove that :not(), you'll see that the block's rule doesn't need the !important anymore.

All the same, you could leave the :not() on the theme, and add another class to the block's rule to make the color work without !important, e.g.

.wp-block-post-author.has-link-color a,
.wp-block-post-author.has-link-color.has-background a {
	color: var( --wp--style--color--link );
}

AFAIK, this happens because :not( .class ) works as if it added a .NOT-class to the selected element.
So you'd have .wp-block-post-author.has-link-color (2 "classes" of specificity) selected by the editor styles, and something like .has-background.NOT-has-background-background-color (again, 2 classes of specificity) selected by the theme styles.
Since the theme styles are loaded after the editor's, given the same specificity, they prevail.

I'm not entirely sure of the purpose of that :not( .has-background-background-color ) (defined here: editor, front end), by the way.
It basically forces a text color to all text blocks with a background, except those using the background palette color as background color.
I kinda suspect this selector could be revised in the theme, since there's already an override (editor, front end) for blocks with p as root element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very well put, thanks @Copons! Updated 😄

}
1 change: 1 addition & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@import "./table/style.scss";
@import "./text-columns/style.scss";
@import "./video/style.scss";
@import "./post-excerpt/style.scss";

// The following selectors have increased specificity (using the :root prefix)
// to assure colors take effect over another base class color, mainly to let
Expand Down