block_core_navigation_add_directives_to_submenu

函数
block_core_navigation_add_directives_to_submenu ( $w, $block_attributes )
参数
  • (string) $w Markup of the navigation block.
    Required:
  • (array) $block_attributes Block attributes.
    Required:
返回值
  • (string) Submenu markup with the directives injected.
定义位置
相关方法
block_core_navigation_render_submenu_iconblock_core_navigation_link_render_submenu_iconblock_core_navigation_submenu_render_submenu_iconblock_core_navigation_parse_blocks_from_menu_itemsblock_core_navigation_build_css_font_sizes
引入
-
弃用
-

使用标签处理器在导航子菜单和页面列表区块标记中添加交互性 API 指令。

function block_core_navigation_add_directives_to_submenu( $w, $block_attributes ) {
	while ( $w->next_tag(
		array(
			'tag_name'   => 'LI',
			'class_name' => 'has-child',
		)
	) ) {
		// Add directives to the parent `<li>`.
		$w->set_attribute( 'data-wp-interactive', true );
		$w->set_attribute( 'data-wp-context', '{ "core": { "navigation": { "submenuOpenedBy": {}, "type": "submenu" } } }' );
		$w->set_attribute( 'data-wp-effect', 'effects.core.navigation.initMenu' );
		$w->set_attribute( 'data-wp-on--focusout', 'actions.core.navigation.handleMenuFocusout' );
		$w->set_attribute( 'data-wp-on--keydown', 'actions.core.navigation.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.
		$w->set_attribute( 'tabindex', '-1' );

		if ( ! isset( $block_attributes['openSubmenusOnClick'] ) || false === $block_attributes['openSubmenusOnClick'] ) {
			$w->set_attribute( 'data-wp-on--mouseenter', 'actions.core.navigation.openMenuOnHover' );
			$w->set_attribute( 'data-wp-on--mouseleave', 'actions.core.navigation.closeMenuOnHover' );
		}

		// Add directives to the toggle submenu button.
		if ( $w->next_tag(
			array(
				'tag_name'   => 'BUTTON',
				'class_name' => 'wp-block-navigation-submenu__toggle',
			)
		) ) {
			$w->set_attribute( 'data-wp-on--click', 'actions.core.navigation.toggleMenuOnClick' );
			$w->set_attribute( 'data-wp-bind--aria-expanded', 'selectors.core.navigation.isMenuOpen' );
			// The `aria-expanded` attribute for SSR is already added in the submenu block.
		}
		// Add directives to the submenu.
		if ( $w->next_tag(
			array(
				'tag_name'   => 'UL',
				'class_name' => 'wp-block-navigation__submenu-container',
			)
		) ) {
			$w->set_attribute( 'data-wp-on--focus', 'actions.core.navigation.openMenuOnFocus' );
		}

		// Iterate through subitems if exist.
		block_core_navigation_add_directives_to_submenu( $w, $block_attributes );
	}
	return $w->get_updated_html();
}

常见问题

FAQs
查看更多 >