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

Fix colour rendering in Navigation overlay #36479

Merged
merged 4 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,29 @@ function Navigation( {
},
} );

const overlayClassnames = classnames( {
'has-text-color':
!! overlayTextColor.color || !! overlayTextColor?.class,
[ getColorClassName(
'color',
overlayTextColor?.slug
) ]: !! overlayTextColor?.slug,
'has-background':
!! overlayBackgroundColor.color || overlayBackgroundColor?.class,
[ getColorClassName(
'background-color',
overlayBackgroundColor?.slug
) ]: !! overlayBackgroundColor?.slug,
} );

const overlayStyles = {
color: ! overlayTextColor?.slug && overlayTextColor?.color,
backgroundColor:
! overlayBackgroundColor?.slug &&
overlayBackgroundColor?.color &&
overlayBackgroundColor.color,
};

// Turn on contrast checker for web only since it's not supported on mobile yet.
const enableContrastChecking = Platform.OS === 'web';

Expand Down Expand Up @@ -494,6 +517,8 @@ function Navigation( {
isOpen={ isResponsiveMenuOpen }
isResponsive={ 'never' !== overlayMenu }
isHiddenByDefault={ 'always' === overlayMenu }
classNames={ overlayClassnames }
styles={ overlayStyles }
>
{ isEntityAvailable && (
<NavigationInnerBlocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ export default function ResponsiveWrapper( {
isResponsive,
onToggle,
isHiddenByDefault,
classNames,
styles,
} ) {
if ( ! isResponsive ) {
return children;
}
const responsiveContainerClasses = classnames(
'wp-block-navigation__responsive-container',
classNames,
{
'is-menu-open': isOpen,
'hidden-by-default': isHiddenByDefault,
Expand Down Expand Up @@ -61,7 +64,11 @@ export default function ResponsiveWrapper( {
</Button>
) }

<div className={ responsiveContainerClasses } id={ modalId }>
<div
className={ responsiveContainerClasses }
style={ styles }
id={ modalId }
>
<div
className="wp-block-navigation__responsive-close"
tabIndex="-1"
Expand Down
48 changes: 44 additions & 4 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/
function block_core_navigation_build_css_colors( $attributes ) {
$colors = array(
'css_classes' => array(),
'inline_styles' => '',
'css_classes' => array(),
'inline_styles' => '',
'overlay_css_classes' => array(),
'overlay_inline_styles' => '',
);

// Text color.
Expand Down Expand Up @@ -54,6 +56,42 @@ function block_core_navigation_build_css_colors( $attributes ) {
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
}

// Overlay text color.
$has_named_overlay_text_color = array_key_exists( 'overlayTextColor', $attributes );
$has_custom_overlay_text_color = array_key_exists( 'customOverlayTextColor', $attributes );

// If has overlay text color.
if ( $has_custom_overlay_text_color || $has_named_overlay_text_color ) {
// Add has-text-color class.
$colors['overlay_css_classes'][] = 'has-text-color';
}

if ( $has_named_overlay_text_color ) {
// Add the overlay color class.
$colors['overlay_css_classes'][] = sprintf( 'has-%s-color', $attributes['overlayTextColor'] );
} elseif ( $has_custom_overlay_text_color ) {
// Add the custom overlay color inline style.
$colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $attributes['customOverlayTextColor'] );
}

// Overlay background color.
$has_named_overlay_background_color = array_key_exists( 'overlayBackgroundColor', $attributes );
$has_custom_overlay_background_color = array_key_exists( 'customOverlayBackgroundColor', $attributes );

// If has overlay background color.
if ( $has_custom_overlay_background_color || $has_named_overlay_background_color ) {
// Add has-background class.
$colors['overlay_css_classes'][] = 'has-background';
}

if ( $has_named_overlay_background_color ) {
// Add the overlay background-color class.
$colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', $attributes['overlayBackgroundColor'] );
} elseif ( $has_custom_overlay_background_color ) {
// Add the custom overlay background-color inline style.
$colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customOverlayBackgroundColor'] );
}

return $colors;
}

Expand Down Expand Up @@ -247,6 +285,7 @@ function( $block ) {
$responsive_container_classes = array(
'wp-block-navigation__responsive-container',
$is_hidden_by_default ? 'hidden-by-default' : '',
implode( ' ', $colors['overlay_css_classes'] ),
);
$open_button_classes = array(
'wp-block-navigation__responsive-container-open',
Expand All @@ -255,7 +294,7 @@ function( $block ) {

$responsive_container_markup = sprintf(
'<button aria-expanded="false" aria-haspopup="true" aria-label="%3$s" class="%6$s" data-micromodal-trigger="modal-%1$s"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg></button>
<div class="%5$s" id="modal-%1$s">
<div class="%5$s" style="%7$s" id="modal-%1$s">
<div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close>
<div class="wp-block-navigation__responsive-dialog" role="dialog" aria-modal="true" aria-labelledby="modal-%1$s-title" >
<button aria-label="%4$s" data-micromodal-close class="wp-block-navigation__responsive-container-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" role="img" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg></button>
Expand All @@ -270,7 +309,8 @@ function( $block ) {
__( 'Open menu' ), // Open button label.
__( 'Close menu' ), // Close button label.
implode( ' ', $responsive_container_classes ),
implode( ' ', $open_button_classes )
implode( ' ', $open_button_classes ),
$colors['overlay_inline_styles']
);

return sprintf(
Expand Down
11 changes: 9 additions & 2 deletions packages/block-library/src/navigation/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@
align-items: var(--layout-align, initial);
}

// If the responsive wrapper is present but overlay is not open,
// overlay styles shouldn't apply.
&:not(.is-menu-open.is-menu-open) {
color: inherit !important;
Copy link
Contributor

Choose a reason for hiding this comment

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

The !important's feel appropriate enough for the way the feature works at the moment. It's possible in the future we could explore editing the contents of the modal in isolation (like template parts), at which point these would be revisited anyway.

background-color: inherit !important;
}

// Overlay menu.
// Provide an opinionated default style for menu items inside.
// Inherit as much as we can regarding colors, fonts, sizes,
Expand Down Expand Up @@ -432,9 +439,9 @@

// Remove background colors for items inside the overlay menu.
// Has to be !important to override global styles.
// @todo: We should revisit this so that the overlay colors can be applied, instead of the background.
.wp-block-navigation-item .wp-block-navigation__submenu-container,
.wp-block-navigation-item {
.wp-block-navigation-item,
.wp-block-page-list {
color: inherit !important;
background: transparent !important;
}
Expand Down