Skip to content

Commit

Permalink
rebased and moved changes to WP_Navigation_Block_Renderer new location
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieCabrera committed Jan 29, 2024
1 parent 9e5b861 commit 67e700f
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,26 @@ private static function get_classes( $attributes ) {
return implode( ' ', $classes );
}

/**
* Returns whether or not the navigation is always overlay.
*
* @param array $attributes The block attributes.
* @return bool Returns whether or not the navigation is always overlay.
*/
private static function is_always_overlay( $attributes ) {
return isset( $attributes['overlayMenu'] ) && 'always' === $attributes['overlayMenu'];
}

/**
* Returns whether or not the navigation is collapsable.
*
* @param array $attributes The block attributes.
* @return bool Returns whether or not the navigation is collapsable.
*/
private static function is_collapsable( $attributes ) {
return isset( $attributes['overlayMenu'] ) && in_array( $attributes['overlayMenu'], array( 'mobile', 'auto' ), true );
}

/**
* Get styles for the navigation block.
*
Expand Down Expand Up @@ -530,13 +546,20 @@ private static function get_nav_element_directives( $is_interactive, $attributes
if ( ! $is_interactive ) {
return '';
}

$gutenberg_experiments = get_option( 'gutenberg-experiments' );
$is_experiment = ( $gutenberg_experiments && array_key_exists( 'gutenberg-navigation-overlay-auto', $gutenberg_experiments ) ) ? true : false;

$overlay_menu = $is_experiment ? 'auto' : $attributes['overlayMenu'];

// When adding to this array be mindful of security concerns.
$nav_element_context = wp_json_encode(
array(
'overlayOpenedBy' => array(),
'type' => 'overlay',
'roleAttribute' => '',
'ariaLabel' => __( 'Menu' ),
'overlayMenu' => $overlay_menu,
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP
);
Expand All @@ -549,12 +572,20 @@ private static function get_nav_element_directives( $is_interactive, $attributes
* When the navigation's 'overlayMenu' attribute is set to 'always', JavaScript
* is not needed for collapsing the menu because the class is set manually.
*/
if ( ! static::is_always_overlay( $attributes ) ) {
$nav_element_directives .= 'data-wp-init="callbacks.initNav"';
if ( static::is_collapsable( $attributes ) ) {
$nav_element_directives .= ' '; // space separator
$nav_element_directives .= 'data-wp-class--is-collapsed="context.isCollapsed"';
}

if ( isset( $overlay_menu ) && 'mobile' === $overlay_menu ) {
$nav_element_directives .= ' '; // space separator
$nav_element_directives .= 'data-wp-init="callbacks.initMobileNav"';
}
if ( isset( $overlay_menu ) && 'auto' === $overlay_menu ) {
$nav_element_directives .= ' '; // space separator
$nav_element_directives .= 'data-wp-init="callbacks.initAutoNav"';
}

return $nav_element_directives;
}

Expand Down

0 comments on commit 67e700f

Please sign in to comment.