diff --git a/packages/block-library/src/latest-comments/block.json b/packages/block-library/src/latest-comments/block.json index 543512ddf3ce7..f935a0d20bef1 100644 --- a/packages/block-library/src/latest-comments/block.json +++ b/packages/block-library/src/latest-comments/block.json @@ -56,6 +56,7 @@ "fontSize": true } }, + "style": true, "interactivity": { "clientNavigation": true } diff --git a/packages/block-library/src/latest-comments/edit.js b/packages/block-library/src/latest-comments/edit.js index 432cd389efacc..dfeb14e6c2122 100644 --- a/packages/block-library/src/latest-comments/edit.js +++ b/packages/block-library/src/latest-comments/edit.js @@ -2,6 +2,7 @@ * WordPress dependencies */ import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; +import { useEffect } from '@wordpress/element'; import { Disabled, RangeControl, @@ -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
    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, @@ -45,7 +86,7 @@ export default function LatestComments( { attributes, setAttributes } ) { const dropdownMenuProps = useToolsPanelDropdownMenuProps(); return ( -
    +
    { + // 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 ); + } + } } />