Skip to content

Custom block stylesheet for the core/navigation-submenu block is not enqueued #66786

Closed

Description

@germanfrelo

Description

It's not possible to use a custom block stylesheet for the core/navigation-submenu block. The styles are not loaded on the front-end of the website. Instead, I have to put the submenu styles in the custom block stylesheet for the core/navigation block (for which the block stylesheet does works).

Step-by-step reproduction instructions

  1. Using a block theme, in the function.php file, add the code snippet below.
  2. Create a file named core-navigation-submenu.css in /assets/css/blocks/ (example) and add some styles like the example below.

Screenshots, screen recording, code snippet

In functions.php:

function themeslug_block_stylesheets() {
	// Add the block name (with namespace) for each style
	$blocks = array(
		// 'core/navigation',
		'core/navigation-submenu',
	);

	// Loop through each block and enqueue its styles
	foreach ( $blocks as $block ) {
		// Replace slash with hyphen for filename
		$slug = str_replace( '/', '-', $block );

		wp_enqueue_block_style( $block, array(
			'handle' => "themeslug-block-{$slug}",
			'src'    => get_theme_file_uri( "assets/css/blocks/{$slug}.css" ),
			'path'   => get_theme_file_path( "assets/css/blocks/{$slug}.css" )
		) );
	}
}

add_action( 'init', 'themeslug_block_stylesheets' );

In assets/css/blocks/core-navigation-submenu.css:

/**
 * Submenu Container (example)
 */

.wp-block-navigation .has-child .wp-block-navigation-submenu__toggle[aria-expanded="true"] ~ .wp-block-navigation__submenu-container,
.wp-block-navigation .has-child:not(.open-on-click):hover > .wp-block-navigation__submenu-container,
.wp-block-navigation .has-child:not(.open-on-click):not(.open-on-hover-click):focus-within > .wp-block-navigation__submenu-container {
	min-width: max-content;
}

Environment info

  • Wordpress version: 6.6.2
  • Gutenberg version: I don't know, the one that comes with WP 6.6.2? 18.5?
  • Active theme: a custom block child theme of a custom block parent theme from scratch
  • Browser: Google Chrome 130.0.6723.92 (arm64)
  • macMini M2 with macOS Sonoma 14.7.1

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Activity

  1. added
    [Type] BugAn existing feature does not function as intended
    on Nov 6, 2024
  2. changed the title [-]Custom block stylesheet for the navigation-submenu block is not recognized[/-] [+]Custom block stylesheet for the `core/navigation-submenu` block is not enqueued[/+] on Nov 6, 2024
  3. patil-vipul commented on Dec 27, 2024

    @patil-vipul

    While reviewing the core functionality of enqueuing custom block stylesheets, I noticed that the stylesheets for core/navigation-submenu are successfully enqueued on the frontend. Here's the same code and CSS used for reference.

    function themeslug_block_stylesheets() {
    	// Add the block name (with namespace) for each style
    	$blocks = array(
    		//				'core/navigation',
    		'core/navigation-submenu',
    	);
    
    	// Loop through each block and enqueue its styles
    	foreach ( $blocks as $block ) {
    		// Replace slash with hyphen for filename
    		$slug = str_replace( '/', '-', $block );
    
    		wp_enqueue_block_style( $block, array(
    			'handle' => "test-block-core-navigation-submenu",
    			'src'    => get_theme_file_uri( "assets/css/blocks/core-navigation-submenu.css" ),
    			'path'   => get_theme_file_path( "assets/css/blocks/core-navigation-submenu.css" ),
    		) );
    	}
    }
    
    add_action( 'init', 'themeslug_block_stylesheets' );
    .wp-block-navigation{
    	background: #808080FF;
    }
    
    .wp-block-navigation:not(.has-background) .wp-block-navigation__submenu-container{
    	background: red;
    }

    Upon inspecting the page, the styles are present in the DOM after being enqueued for core/navigation-submenu. However, the changes are not visible on the frontend, as shown below.

    Image

    Image

    To apply the changes, I used !important, as demonstrated in the code below. In the preview we can see that the styles are now applied correctly.

    .wp-block-navigation{
    	background: #808080FF;
    }
    
    .wp-block-navigation:not(.has-background) .wp-block-navigation__submenu-container{
    	background: red!important;
    }

    Image

    Reason for the Issue

    The issue arises due to CSS specificity and how WordPress loads styles:

    When Enqueuing for core/navigation:

    -The styles are loaded at the navigation block level.
    -They naturally cascade to submenu elements, maintaining specificity through the parent-child relationship.

    When Enqueuing for core/navigation-submenu:

    • WordPress core loads its default submenu styles with a certain specificity.
    • Custom styles must compete with these default styles, which often have equal or greater specificity.
    • The cascade from the parent navigation block is lost, making custom styles less effective.

    Behavior Breakdown

    Core Navigation Enqueuing:

    wp_enqueue_block_style( 'core/navigation', ... );
    • Styles apply at the navigation block level.
    • They cascade naturally to submenu elements.
    • Specificity is maintained through the parent-child hierarchy.

    Submenu Enqueuing:

    wp_enqueue_block_style( 'core/navigation-submenu', ... );
    • Styles are scoped specifically to the submenu component.
    • They directly compete with WordPress's default submenu styles, which often have equal or higher specificity.
    • The cascade from the parent navigation block is lost.

    In case where not to rely on !important, we can increase the selector specificity of your custom styles.

    cc: @germanfrelo

  4. patil-vipul commented on Dec 27, 2024

    @patil-vipul

    @germanfrelo Let me know your thoughts on the above. As I think we should continue with the default behaviour here.

  5. germanfrelo commented on Jan 9, 2025

    @germanfrelo
    Author

    Sorry for the delay. I will test it again in a few days.

  6. germanfrelo commented on Feb 8, 2025

    @germanfrelo
    Author

    @patil-vipul I confirm that it works. I don't know why it didn't work for me before.
    I'm closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Type] BugAn existing feature does not function as intended

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions