WordPress Version: 6.5
/**
* Add Interactivity API directives to the navigation-submenu and page-list
* blocks markup using the Tag Processor.
*
* @param WP_HTML_Tag_Processor $tags Markup of the navigation block.
* @param array $block_attributes Block attributes.
*
* @return string Submenu markup with the directives injected.
*/
function block_core_navigation_add_directives_to_submenu($tags, $block_attributes)
{
while ($tags->next_tag(array('tag_name' => 'LI', 'class_name' => 'has-child'))) {
// Add directives to the parent `<li>`.
$tags->set_attribute('data-wp-interactive', 'core/navigation');
$tags->set_attribute('data-wp-context', '{ "submenuOpenedBy": { "click": false, "hover": false, "focus": false }, "type": "submenu" }');
$tags->set_attribute('data-wp-watch', 'callbacks.initMenu');
$tags->set_attribute('data-wp-on--focusout', 'actions.handleMenuFocusout');
$tags->set_attribute('data-wp-on--keydown', 'actions.handleMenuKeydown');
// This is a fix for Safari. Without it, Safari doesn't change the active
// element when the user clicks on a button. It can be removed once we add
// an overlay to capture the clicks, instead of relying on the focusout
// event.
$tags->set_attribute('tabindex', '-1');
if (!isset($block_attributes['openSubmenusOnClick']) || false === $block_attributes['openSubmenusOnClick']) {
$tags->set_attribute('data-wp-on--mouseenter', 'actions.openMenuOnHover');
$tags->set_attribute('data-wp-on--mouseleave', 'actions.closeMenuOnHover');
}
// Add directives to the toggle submenu button.
if ($tags->next_tag(array('tag_name' => 'BUTTON', 'class_name' => 'wp-block-navigation-submenu__toggle'))) {
$tags->set_attribute('data-wp-on--click', 'actions.toggleMenuOnClick');
$tags->set_attribute('data-wp-bind--aria-expanded', 'state.isMenuOpen');
// The `aria-expanded` attribute for SSR is already added in the submenu block.
}
// Add directives to the submenu.
if ($tags->next_tag(array('tag_name' => 'UL', 'class_name' => 'wp-block-navigation__submenu-container'))) {
$tags->set_attribute('data-wp-on--focus', 'actions.openMenuOnFocus');
}
// Iterate through subitems if exist.
block_core_navigation_add_directives_to_submenu($tags, $block_attributes);
}
return $tags->get_updated_html();
}