Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation: Don't save the level of the link in an attribute #48219

Merged
merged 12 commits into from
Mar 3, 2023
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 ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this was duplicated. I'd rather use the same function in both places. If we need to keep two copies of it, I'm not sure what we should do with this one - it never uses the $is_sub_menu branch, so we could just remove all that, but then it diverges from block_core_navigation_submenu_build_css_colors.

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 );
getdave marked this conversation as resolved.
Show resolved Hide resolved
$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,
getdave marked this conversation as resolved.
Show resolved Hide resolved
)
);

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

}

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