Skip to content

Commit

Permalink
Navigation: Don't save the level of the link in an attribute (#48219)
Browse files Browse the repository at this point in the history
* Don't save the level of the link in an attribute

* reuse code

* remove duplicate code

* add extra escaping

* Remove refactoring

* update the doc block

* fix linter

* fix linter

* remove double escape

* Use existing truthy var for clarity in function call

* Navigation Link blocks don't need submenu colors as this is set in the submenu block

* fix linter

---------

Co-authored-by: Dave Smith <getdavemail@gmail.com>
  • Loading branch information
2 people authored and ntsekouras committed Mar 7, 2023
1 parent 3760efb commit e749760
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
9 changes: 0 additions & 9 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,6 @@ export default function NavigationLinkEdit( {
[ clientId ]
);

useEffect( () => {
// This side-effect should not create an undo level as those should
// only be created via user interactions. Mark this change as
// not persistent to avoid undo level creation.
// See https://github.com/WordPress/gutenberg/issues/34564.
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { isTopLevelLink } );
}, [ isTopLevelLink ] );

/**
* Transform to submenu block.
*/
Expand Down
9 changes: 4 additions & 5 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
* Build an array with CSS classes and inline styles defining the colors
* which will be applied to the navigation markup in the front-end.
*
* @param array $context Navigation block context.
* @param array $attributes Block attributes.
* @param array $context Navigation block context.
* @param array $attributes Block attributes.
* @param bool $is_sub_menu Whether the link is part of a sub-menu.
* @return array Colors CSS classes and inline styles.
*/
function block_core_navigation_link_build_css_colors( $context, $attributes ) {
function block_core_navigation_link_build_css_colors( $context, $attributes, $is_sub_menu = false ) {
$colors = array(
'css_classes' => array(),
'inline_styles' => '',
);

$is_sub_menu = isset( $attributes['isTopLevelLink'] ) ? ( ! $attributes['isTopLevelLink'] ) : false;

// Text color.
$named_text_color = null;
$custom_text_color = null;
Expand Down
10 changes: 0 additions & 10 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,6 @@ export default function NavigationSubmenuEdit( {
}
}, [] );

// Store the colors from context as attributes for rendering.
useEffect( () => {
// This side-effect should not create an undo level as those should
// only be created via user interactions. Mark this change as
// not persistent to avoid undo level creation.
// See https://github.com/WordPress/gutenberg/issues/34564.
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { isTopLevelItem } );
}, [ isTopLevelItem ] );

/**
* The hook shouldn't be necessary but due to a focus loss happening
* when selecting a suggestion in the link popover, we force close on block unselection.
Expand Down
29 changes: 23 additions & 6 deletions packages/block-library/src/navigation-submenu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
* Build an array with CSS classes and inline styles defining the colors
* which will be applied to the navigation markup in the front-end.
*
* @param array $context Navigation block context.
* @param array $attributes Block attributes.
* @param array $context Navigation block context.
* @param array $attributes Block attributes.
* @param bool $is_sub_menu Whether the block is a sub-menu.
* @return array Colors CSS classes and inline styles.
*/
function block_core_navigation_submenu_build_css_colors( $context, $attributes ) {
function block_core_navigation_submenu_build_css_colors( $context, $attributes, $is_sub_menu = false ) {
$colors = array(
'css_classes' => array(),
'inline_styles' => '',
);

$is_sub_menu = isset( $attributes['isTopLevelItem'] ) ? ( ! $attributes['isTopLevelItem'] ) : false;

// Text color.
$named_text_color = null;
$custom_text_color = null;
Expand Down Expand Up @@ -250,6 +249,15 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
}

if ( $has_submenu ) {
$colors = block_core_navigation_submenu_build_css_colors( $block->context, $attributes, $has_submenu );
$classes = array_merge(
array( 'wp-block-navigation__submenu-container' ),
$colors['css_classes']
);
$css_classes = trim( implode( ' ', $classes ) );

$style_attribute = $colors['inline_styles'];

$inner_blocks_html = '';
foreach ( $block->inner_blocks as $inner_block ) {
$inner_blocks_html .= $inner_block->render();
Expand All @@ -263,10 +271,19 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
$html = $tag_processor->get_updated_html();
}

$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $css_classes,
'style' => $style_attribute,
)
);

$html .= sprintf(
'<ul class="wp-block-navigation__submenu-container">%s</ul>',
'<ul %s>%s</ul>',
$wrapper_attributes,
$inner_blocks_html
);

}

$html .= '</li>';
Expand Down

0 comments on commit e749760

Please sign in to comment.