Skip to content

Commit

Permalink
Navigation: Add support for submenu items in the hardcoded menus (#570)
Browse files Browse the repository at this point in the history
* Navigation: Add support for `submenu` items in the hardcoded menus

* Local Navigation: Style submenus in small screen menus
  • Loading branch information
ryelle authored Feb 8, 2024
1 parent fb25fca commit c10e4d2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
8 changes: 8 additions & 0 deletions mu-plugins/blocks/local-navigation-bar/postcss/style.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
/* Navigation. */

/* Remove padding from menu items with background color, which is used to color the modal background. */
& .wp-block-navigation ul.has-background,
& .wp-block-navigation:where(.has-background) .wp-block-navigation-item a:not(.wp-element-button),
& .wp-block-navigation:where(.has-background) .wp-block-navigation-submenu a:not(.wp-element-button) {
padding: 0;
Expand All @@ -130,6 +131,13 @@
padding-left: var(--wp--preset--spacing--edge-space) !important;
padding-top: 21px !important;
padding-bottom: 18px !important;

& .wp-block-navigation__submenu-container {
padding: 0 !important;
padding-inline-start: var(--wp--preset--spacing--20, 20px) !important;
margin-top: var(--wp--preset--spacing--20, 20px) !important;
gap: var(--wp--preset--spacing--20, 20px) !important;
}
}
}

Expand Down
32 changes: 30 additions & 2 deletions mu-plugins/blocks/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ function get_menu_content( $menu_slug ) {

$menu_content = '';
foreach ( $menu_items as $item ) {
$menu_content .= render_menu_item( $item );
}

return $menu_content;
}

/**
* Render an individual navigation item, to support recursively building submenus.
*
* @param array $item
*
* @return string Menu item in block syntax.
*/
function render_menu_item( $item ) {
$output = '';

if ( isset( $item['submenu'] ) ) {
$output = sprintf(
'<!-- wp:navigation-submenu {"label":"%1$s","url":"#","kind":"custom"} -->',
$item['label']
);

foreach ( $item['submenu'] as $submenu_item ) {
$output .= render_menu_item( $submenu_item, false );
}

$output .= '<!-- /wp:navigation-submenu -->';
} else {
$block_code = '<!-- wp:navigation-link {"label":"%1$s","url":"%2$s","kind":"custom"} /-->';

// If this is a relative link, convert it to absolute and try to find
Expand All @@ -108,15 +136,15 @@ function get_menu_content( $menu_slug ) {
}
}

$menu_content .= sprintf(
$output .= sprintf(
$block_code,
esc_html( $item['label'] ),
esc_url( $item['url'], ),
isset( $item['id'] ) ? intval( $item['id'] ) : ''
);
}

return $menu_content;
return $output;
}

/**
Expand Down

0 comments on commit c10e4d2

Please sign in to comment.