Skip to content

Commit

Permalink
Merge 3fb56b7 into 0e6e204
Browse files Browse the repository at this point in the history
  • Loading branch information
emeaguiar committed Jan 27, 2022
2 parents 0e6e204 + 3fb56b7 commit 71b98f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 44 deletions.
45 changes: 26 additions & 19 deletions theme/assets/src/block-editor/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,39 @@
*/
import { addFilter } from '@wordpress/hooks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useEffect } from '@wordpress/element';

/**
* External dependencies
* Trigger a class change when component is rendered.
*
* @param {*} BlockEdit Original component.
*
* @return {WPComponent} Original component.
*/
import classnames from 'classnames';

const withTitleClassNames = createHigherOrderComponent( BlockListBlock => {
const withAttributeChange = createHigherOrderComponent( BlockEdit => {
return props => {
if ( 'core/site-title' === props.name ) {
return (
<BlockListBlock
{ ...props }
className={ classnames(
const {
attributes: { className },
name,
} = props;

useEffect( () => {
if ( 'core/site-title' === name && ! className ) {
props.setAttributes( {
className:
'site-title mdc-typography mdc-typography--headline6',
props.className
) }
/>
);
}
} );
}
// Use empty array to make sure this runs only once.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );

return <BlockListBlock { ...props } />;
return <BlockEdit { ...props } />;
};
}, 'withTitleClassNames' );
}, 'withAttributeChange' );

addFilter(
'editor.BlockListBlock',
'material/title-class-name',
withTitleClassNames
'editor.BlockEdit',
'material/title-class-name/block-edit',
withAttributeChange
);
25 changes: 0 additions & 25 deletions theme/inc/blocks/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class Blocks {
*/
public function init() {
$this->action_register_blocks();
$this->add_filter_render_block();
}

/**
Expand Down Expand Up @@ -156,28 +155,4 @@ public static function render( $attributes, $content, $block ) {

return ob_get_clean();
}

/**
* Add filter to modify core dynamic blocks markup.
*/
public function add_filter_render_block() {
add_filter( 'render_block', [ $this, 'filter_render_block' ], 10, 2 );
}

/**
* Filter render_block to add specific classes to site title.
*
* @param string $block_content Block content.
* @param array $block Block object.
*
* @return string
*/
public function filter_render_block( $block_content, $block ) {
if ( 'core/site-title' === $block['blockName'] ) {
// Add additional classes to markup.
$block_content = str_replace( '<h1 class="', '<h1 class="site-title mdc-typography mdc-typography--headline6 ', $block_content );
}

return $block_content;
}
}

0 comments on commit 71b98f1

Please sign in to comment.