-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 Submenu: Fix color handling and inheritance #68351
base: trunk
Are you sure you want to change the base?
Changes from 7 commits
8cdd919
7d483df
0f0bb5e
c2e99d1
7ff2e55
faf394e
57fb66e
2b4e452
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,16 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) { | |
$open_on_hover_and_click = isset( $block->context['openSubmenusOnClick'] ) && ! $block->context['openSubmenusOnClick'] && | ||
$show_submenu_indicators; | ||
|
||
$block->block_type->supports['color'] = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repeated again on line 258, is there a reason we need to set twice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The repetition on line 258 could be omitted safely. Will be done in the next commit. Thanks for the suggestion. |
||
$colors_supports = wp_apply_colors_support( $block->block_type, $attributes ); | ||
if ( array_key_exists( 'class', $colors_supports ) ) { | ||
$css_classes .= ' ' . $colors_supports['class']; | ||
} | ||
|
||
if ( array_key_exists( 'style', $colors_supports ) ) { | ||
$style_attribute .= $colors_supports['style']; | ||
} | ||
|
||
$wrapper_attributes = get_block_wrapper_attributes( | ||
array( | ||
'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) . | ||
|
@@ -203,6 +213,16 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) { | |
} | ||
|
||
if ( $has_submenu ) { | ||
// These properties for submenus should only be applied from context. | ||
// Values directly stored inside attributes should be applied to the parent block only. | ||
unset( | ||
$attributes['textColor'], | ||
$attributes['backgroundColor'], | ||
$attributes['customTextColor'], | ||
$attributes['customBackgroundColor'], | ||
$attributes['style'] | ||
); | ||
|
||
// Copy some attributes from the parent block to this one. | ||
// Ideally this would happen in the client when the block is created. | ||
if ( array_key_exists( 'overlayTextColor', $block->context ) ) { | ||
|
@@ -218,6 +238,22 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) { | |
$attributes['style']['color']['background'] = $block->context['customOverlayBackgroundColor']; | ||
} | ||
|
||
// If there are no overlay colors provided, then inherit the colors from the parent block. | ||
if ( ! isset( $attributes['textColor'] ) && ! isset( $attributes['style']['color']['text'] ) ) { | ||
if ( array_key_exists( 'textColor', $block->context ) ) { | ||
$attributes['textColor'] = $block->context['textColor']; | ||
} elseif ( array_key_exists( 'customTextColor', $block->context ) ) { | ||
$attributes['style']['color']['text'] = $block->context['customTextColor']; | ||
} | ||
} | ||
if ( ! isset( $attributes['backgroundColor'] ) && ! isset( $attributes['style']['color']['background'] ) ) { | ||
if ( array_key_exists( 'backgroundColor', $block->context ) ) { | ||
$attributes['backgroundColor'] = $block->context['backgroundColor']; | ||
} elseif ( array_key_exists( 'customBackgroundColor', $block->context ) ) { | ||
$attributes['style']['color']['background'] = $block->context['customBackgroundColor']; | ||
} | ||
} | ||
|
||
// This allows us to be able to get a response from wp_apply_colors_support. | ||
$block->block_type->supports['color'] = true; | ||
$colors_supports = wp_apply_colors_support( $block->block_type, $attributes ); | ||
|
@@ -244,12 +280,14 @@ 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, | ||
) | ||
); | ||
$wrapper_attributes = array(); | ||
if ( ! empty( $style_attribute ) ) { | ||
$wrapper_attributes[] = sprintf( 'style="%s"', $style_attribute ); | ||
} | ||
if ( ! empty( $css_classes ) ) { | ||
$wrapper_attributes[] = sprintf( 'class="%s"', $css_classes ); | ||
} | ||
$wrapper_attributes = implode( ' ', $wrapper_attributes ); | ||
|
||
$html .= sprintf( | ||
'<ul %s>%s</ul>', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since its a string, I feel this is a little less readable on first glance.
Maybe adding a explicit check might be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
Double Bang
operator conveys the boolean nature and should provide sufficient context.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, but the original included two comparisons, whereas this approach reduces it to just one.