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

resolve latest comment block font size issue in editorside issue #68877

Closed
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
1 change: 1 addition & 0 deletions packages/block-library/src/latest-comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"fontSize": true
}
},
"style": true,
"interactivity": {
"clientNavigation": true
}
Expand Down
56 changes: 53 additions & 3 deletions packages/block-library/src/latest-comments/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import {
Disabled,
RangeControl,
Expand Down Expand Up @@ -31,8 +32,48 @@ const MIN_COMMENTS = 1;
const MAX_COMMENTS = 100;

export default function LatestComments( { attributes, setAttributes } ) {
const { commentsToShow, displayAvatar, displayDate, displayExcerpt } =
attributes;
const {
commentsToShow,
displayAvatar,
displayDate,
displayExcerpt,
style,
} = attributes;

const typographyStyles = {
fontSize: style?.typography?.fontSize || undefined,
lineHeight: style?.typography?.lineHeight || undefined,
fontFamily: style?.typography?.__experimentalFontFamily || undefined,
fontWeight: style?.typography?.__experimentalFontWeight || undefined,
fontStyle: style?.typography?.__experimentalFontStyle || undefined,
textTransform:
style?.typography?.__experimentalTextTransform || undefined,
textDecoration:
style?.typography?.__experimentalTextDecoration || undefined,
letterSpacing:
style?.typography?.__experimentalLetterSpacing || undefined,
};

const blockProps = useBlockProps( {
style: typographyStyles, // Apply styles to the parent block
} );

useEffect( () => {
// Apply typography styles to the <ol> element after the content is rendered
const applyTypographyStyles = () => {
const olElement = document.querySelector(
'.wp-block-latest-comments ol'
);
if ( olElement ) {
Object.assign( olElement.style, typographyStyles );
}
};

// Apply styles initially
applyTypographyStyles();

// The server-side render will trigger the re-apply after content is rendered
}, [ typographyStyles ] );

const serverSideAttributes = {
...attributes,
Expand All @@ -45,7 +86,7 @@ export default function LatestComments( { attributes, setAttributes } ) {
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

return (
<div { ...useBlockProps() }>
<div { ...blockProps }>
<InspectorControls>
<ToolsPanel
label={ __( 'Settings' ) }
Expand Down Expand Up @@ -148,6 +189,15 @@ export default function LatestComments( { attributes, setAttributes } ) {
// the block appears on the frontend. Setting the locale
// explicitly prevents any middleware from setting it to 'user'.
urlQueryArgs={ { _locale: 'site' } }
onRender={ () => {
// This function is called whenever the ServerSideRender completes rendering
const olElement = document.querySelector(
'.wp-block-latest-comments ol'
);
if ( olElement ) {
Object.assign( olElement.style, typographyStyles );
}
} }
/>
</Disabled>
</div>
Expand Down
Loading