Skip to content

Commit

Permalink
Move logic to hide ToC heading if content is empty from JS to render
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Jan 30, 2024
1 parent ddc5de1 commit 75ef37e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
6 changes: 0 additions & 6 deletions mu-plugins/blocks/sidebar-container/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ function init() {
// Avoids footer collisions (ex, when linked to #reply-title).
onResize();
window.addEventListener( 'resize', onResize );

// If there is no table of contents, hide the heading.
if ( ! document.querySelector( '.wp-block-wporg-table-of-contents' ) ) {
const heading = document.querySelector( '.wp-block-wporg-sidebar-container h2' );
heading?.style.setProperty( 'display', 'none' );
}
}

window.addEventListener( 'load', init );
13 changes: 8 additions & 5 deletions mu-plugins/blocks/table-of-contents/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ function render( $attributes, $content, $block ) {
$title = apply_filters( 'wporg_table_of_contents_heading', __( 'In this article', 'wporg' ), $post->ID );

$content = '<div class="wporg-table-of-contents__header">';
$content .= do_blocks(
'<!-- wp:heading {"style":{"typography":{"fontStyle":"normal","fontWeight":"400"},"spacing":{"margin":{"top":"0","bottom":"0"}}},"fontSize":"normal","fontFamily":"inter"} -->
<h2 class="wp-block-heading has-inter-font-family has-normal-font-size" style="margin-top:0;margin-bottom:0;font-style:normal;font-weight:400">' . esc_html( $title ) . '</h2>
<!-- /wp:heading -->'
);
// If there is no content, don't render the heading.
$content .= empty( trim( wp_strip_all_tags( $post_content ) ) )
? ''
: do_blocks(
'<!-- wp:heading {"style":{"typography":{"fontStyle":"normal","fontWeight":"400"},"spacing":{"margin":{"top":"0","bottom":"0"}}},"fontSize":"normal","fontFamily":"inter"} -->
<h2 class="wp-block-heading has-inter-font-family has-normal-font-size" style="margin-top:0;margin-bottom:0;font-style:normal;font-weight:400">' . esc_html( $title ) . '</h2>
<!-- /wp:heading -->'
);
$content .= '<button type="button" class="wporg-table-of-contents__toggle" aria-expanded="false">';
$content .= '<span class="screen-reader-text">' . esc_html__( 'Table of Contents', 'wporg' ) . '</span>';
$content .= '</button>';
Expand Down

0 comments on commit 75ef37e

Please sign in to comment.