-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 Post Author block to use __experimentalColor and __experimentalLineHeight #23044
Changes from 5 commits
55c199b
16cb05f
319c117
ddd51f6
d17d371
b84f453
d4bcbfd
2a33335
fdbdaef
c3c40a5
ab224b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,23 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { forEach, groupBy } from 'lodash'; | ||
import { forEach } from 'lodash'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef, useMemo } from '@wordpress/element'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { | ||
AlignmentToolbar, | ||
BlockControls, | ||
InspectorControls, | ||
RichText, | ||
__experimentalUseColors, | ||
BlockColorsStyleSelector, | ||
} from '@wordpress/block-editor'; | ||
import { PanelBody, SelectControl, ToggleControl } from '@wordpress/components'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const DEFAULT_CONTRAST_CHECK_FONT_SIZE = 12; | ||
|
||
function PostAuthorEdit( { isSelected, context, attributes, setAttributes } ) { | ||
const { postType, postId } = context; | ||
|
||
|
@@ -47,53 +43,6 @@ function PostAuthorEdit( { isSelected, context, attributes, setAttributes } ) { | |
|
||
const { editEntityRecord } = useDispatch( 'core' ); | ||
|
||
// Need font size in number form for named presets to be used in contrastCheckers. | ||
const { fontSizes } = useSelect( ( select ) => | ||
select( 'core/block-editor' ).getSettings() | ||
); | ||
const fontSizeIndex = useMemo( () => groupBy( fontSizes, 'slug' ), [ | ||
fontSizes, | ||
] ); | ||
const contrastCheckFontSize = useMemo( | ||
() => | ||
// Custom size if set. | ||
attributes.style?.typography?.fontSize || | ||
// Size of preset/named value if set. | ||
fontSizeIndex[ attributes.fontSize ]?.[ 0 ].size || | ||
DEFAULT_CONTRAST_CHECK_FONT_SIZE, | ||
[ | ||
attributes.style?.typography?.fontSize, | ||
attributes.fontSize, | ||
fontSizeIndex, | ||
] | ||
); | ||
const ref = useRef(); | ||
const { | ||
TextColor, | ||
BackgroundColor, | ||
InspectorControlsColorPanel, | ||
ColorPanel, | ||
} = __experimentalUseColors( | ||
[ | ||
{ name: 'textColor', property: 'color' }, | ||
{ name: 'backgroundColor', className: 'background-color' }, | ||
], | ||
{ | ||
contrastCheckers: [ | ||
{ | ||
backgroundColor: true, | ||
textColor: true, | ||
fontSize: contrastCheckFontSize, | ||
}, | ||
], | ||
colorDetector: { targetRef: ref }, | ||
colorPanelProps: { | ||
initialOpen: true, | ||
}, | ||
}, | ||
[ contrastCheckFontSize ] | ||
); | ||
|
||
const { align, showAvatar, showBio, byline } = attributes; | ||
|
||
const avatarSizes = []; | ||
|
@@ -162,64 +111,51 @@ function PostAuthorEdit( { isSelected, context, attributes, setAttributes } ) { | |
</PanelBody> | ||
</InspectorControls> | ||
|
||
{ InspectorControlsColorPanel } | ||
|
||
<BlockControls> | ||
<AlignmentToolbar | ||
value={ align } | ||
onChange={ ( nextAlign ) => { | ||
setAttributes( { align: nextAlign } ); | ||
} } | ||
/> | ||
<BlockColorsStyleSelector | ||
TextColor={ TextColor } | ||
BackgroundColor={ BackgroundColor } | ||
> | ||
{ ColorPanel } | ||
</BlockColorsStyleSelector> | ||
</BlockControls> | ||
|
||
<TextColor> | ||
<BackgroundColor> | ||
<div ref={ ref } className={ classNames.block }> | ||
{ showAvatar && authorDetails && ( | ||
<div className="wp-block-post-author__avatar"> | ||
<img | ||
width={ attributes.avatarSize } | ||
src={ | ||
authorDetails.avatar_urls[ | ||
attributes.avatarSize | ||
] | ||
} | ||
alt={ authorDetails.name } | ||
/> | ||
</div> | ||
) } | ||
<div className="wp-block-post-author__content"> | ||
{ ( ! RichText.isEmpty( byline ) || | ||
isSelected ) && ( | ||
<RichText | ||
className="wp-block-post-author__byline" | ||
multiline={ false } | ||
placeholder={ __( 'Write byline …' ) } | ||
value={ byline } | ||
onChange={ ( value ) => | ||
setAttributes( { byline: value } ) | ||
} | ||
/> | ||
) } | ||
<p className="wp-block-post-author__name"> | ||
{ authorDetails?.name } | ||
</p> | ||
{ showBio && ( | ||
<p className="wp-block-post-author__bio"> | ||
{ authorDetails?.description } | ||
</p> | ||
) } | ||
</div> | ||
<div className={ classNames.block }> | ||
{ showAvatar && authorDetails && ( | ||
<div className="wp-block-post-author__avatar"> | ||
<img | ||
width={ attributes.avatarSize } | ||
src={ | ||
authorDetails.avatar_urls[ | ||
attributes.avatarSize | ||
] | ||
} | ||
alt={ authorDetails.name } | ||
/> | ||
</div> | ||
</BackgroundColor> | ||
</TextColor> | ||
) } | ||
<div className="wp-block-post-author__content"> | ||
{ ( ! RichText.isEmpty( byline ) || isSelected ) && ( | ||
<RichText | ||
className="wp-block-post-author__byline" | ||
multiline={ false } | ||
placeholder={ __( 'Write byline …' ) } | ||
value={ byline } | ||
onChange={ ( value ) => | ||
setAttributes( { byline: value } ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imagine this is unrelated to the PR, but it is really weird that the byline is not persisted across all instances of the same author! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah... it was set up as an attribute specific to the block for whatever reason. The bio on the other hand is per author. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting :p |
||
} | ||
/> | ||
) } | ||
<p className="wp-block-post-author__name"> | ||
{ authorDetails?.name } | ||
</p> | ||
{ showBio && ( | ||
<p className="wp-block-post-author__bio"> | ||
{ authorDetails?.description } | ||
</p> | ||
) } | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -24,7 +24,9 @@ function post_author_build_css_colors( $attributes ) { | |||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is also a good opportunity to implement this #22872 (comment) The idea being that you don't need to touch any backend or frontend code when applying the "useColors" flag, everything should just work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @youknowriad To clarify, you're suggesting to basically get There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think #22872 implements any of that server side support for these hooks, so I guess the suggestion is to implement the
That would be great! But currently we also have to touch the backend to inject the classNames and styles for But I agree, it would be really nice to implement them. Having to go through this manually in index.php is definitely a pain we would like to avoid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I meant #21420 rather than #22872, fixed my original comment now.)
The way I was reading Riad's comment was to have a PR to cover all those flags in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Addison-Stavlo I'm planning on working on this, do you want to team up to tackle the server side rendering? If we can get that done then we don't need to write any custom className and style code in any of these FSE blocks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was planning on using #23007 to add the code, so we have a block to test with at the same time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
One thing I'm unclear on is: should this function render classNames, inline block CSS, and global styles/css variables? I'm not clear on the relationship between your standard inline block CSS, and the new global styles using variables. /cc @youknowriad There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the classes/styles for this one have already been manually set-up, I wouldn't be opposed to merging this and circling back to update it after we have enabled the proper support and updated the other FSE blocks on the task list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference, started work on the render function here https://github.com/WordPress/gutenberg/pull/23007/files#diff-1334f2cb73e38f2f89d2aaf763f07ed9R307 |
||||||||||
// Text color. | ||||||||||
$has_named_text_color = array_key_exists( 'textColor', $attributes ); | ||||||||||
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); | ||||||||||
$has_custom_text_color = array_key_exists( 'style', $attributes ) | ||||||||||
&& array_key_exists( 'color', $attributes['style'] ) | ||||||||||
&& array_key_exists( 'text', $attributes['style']['color'] ); | ||||||||||
|
||||||||||
// If has text color. | ||||||||||
if ( $has_custom_text_color || $has_named_text_color ) { | ||||||||||
|
@@ -37,15 +39,40 @@ function post_author_build_css_colors( $attributes ) { | |||||||||
$text_colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); | ||||||||||
} elseif ( $has_custom_text_color ) { | ||||||||||
// Add the custom color inline style. | ||||||||||
$text_colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] ); | ||||||||||
$text_colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['style']['color']['text'] ); | ||||||||||
} | ||||||||||
|
||||||||||
// Link colors. | ||||||||||
$has_link_color = array_key_exists( 'style', $attributes ) | ||||||||||
&& array_key_exists( 'color', $attributes['style'] ) | ||||||||||
&& array_key_exists( 'link', $attributes['style']['color'] ); | ||||||||||
|
||||||||||
if ( $has_link_color ) { | ||||||||||
$text_colors['css_classes'][] = 'has-link-color'; | ||||||||||
// If link is a named color. | ||||||||||
if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { | ||||||||||
// Get the name from the string and add proper styles. | ||||||||||
$index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; | ||||||||||
$link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); | ||||||||||
$text_colors['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); | ||||||||||
} else { | ||||||||||
$text_colors['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); | ||||||||||
} | ||||||||||
} | ||||||||||
// Background color. | ||||||||||
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); | ||||||||||
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); | ||||||||||
$has_custom_background_color = array_key_exists( 'style', $attributes ) | ||||||||||
&& array_key_exists( 'color', $attributes['style'] ) | ||||||||||
&& array_key_exists( 'background', $attributes['style']['color'] ); | ||||||||||
|
||||||||||
// Gradient color. | ||||||||||
$has_named_gradient = array_key_exists( 'gradient', $attributes ); | ||||||||||
$has_custom_gradient = array_key_exists( 'style', $attributes ) | ||||||||||
&& array_key_exists( 'color', $attributes['style'] ) | ||||||||||
&& array_key_exists( 'gradient', $attributes['style']['color'] ); | ||||||||||
|
||||||||||
// If has background color. | ||||||||||
if ( $has_custom_background_color || $has_named_background_color ) { | ||||||||||
if ( $has_custom_background_color || $has_named_background_color || $has_named_gradient || $has_custom_gradient ) { | ||||||||||
// Add has-background-color class. | ||||||||||
$background_colors['css_classes'][] = 'has-background-color'; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im wondering if this should just be 'has-background'. I notice that gutenberg/packages/block-editor/src/hooks/color.js Lines 130 to 133 in c53d36b
Im assuming either this should be changed to 'has-background', or the hook should be using 'has-background-color' ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely change this to has-background we should match |
||||||||||
} | ||||||||||
|
@@ -55,7 +82,11 @@ function post_author_build_css_colors( $attributes ) { | |||||||||
$background_colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); | ||||||||||
} elseif ( $has_custom_background_color ) { | ||||||||||
// Add the custom background-color inline style. | ||||||||||
$background_colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] ); | ||||||||||
$background_colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); | ||||||||||
} elseif ( $has_named_gradient ) { | ||||||||||
$background_colors['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); | ||||||||||
} elseif ( $has_custom_gradient ) { | ||||||||||
$background_colors['inline_styles'] .= sprintf( 'background: %s;', $attributes['style']['color']['gradient'] ); | ||||||||||
} | ||||||||||
|
||||||||||
return array( | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we get the font size stuff out of the box with
__experimentalFontSize
? Yay! 🎉There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah! Actually, a lot of that goo you highlighted I had to add when I added the
__experimentalFontSize
but was still using the HOC for colors and needed them to still 'play nice'. Now that we are using the flag for this color hook as well things work much better out of the box and all this gunk is no longer seems required.