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

Refactor Nav Block Overlay CSS #56037

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function ResponsiveWrapper( {

const responsiveContainerClasses = classnames(
'wp-block-navigation__responsive-container',
'wp-overlay-component',
{
'has-text-color':
!! overlayTextColor.color || !! overlayTextColor?.class,
Expand All @@ -47,6 +48,7 @@ export default function ResponsiveWrapper( {
overlayBackgroundColor?.slug
) ]: !! overlayBackgroundColor?.slug,
'is-menu-open': isOpen,
'is-overlay-open': isOpen, // alias for new overlay specific style
'hidden-by-default': isHiddenByDefault,
}
);
Expand All @@ -61,13 +63,16 @@ export default function ResponsiveWrapper( {

const openButtonClasses = classnames(
'wp-block-navigation__responsive-container-open',
'wp-overlay-component__open',
'wp-overlay-component__toggle',
{ 'always-shown': isHiddenByDefault }
);

const modalId = `${ id }-modal`;

const dialogProps = {
className: 'wp-block-navigation__responsive-dialog',
className:
'wp-block-navigation__responsive-dialog wp-overlay-component__dialog',
...( isOpen && {
role: 'dialog',
'aria-modal': true,
Expand Down Expand Up @@ -95,20 +100,25 @@ export default function ResponsiveWrapper( {
id={ modalId }
>
<div
className="wp-block-navigation__responsive-close"
className="wp-block-navigation__responsive-close wp-overlay-component__maybe-redundant"
tabIndex="-1"
>
<div { ...dialogProps }>
<Button
className="wp-block-navigation__responsive-container-close"
className={ classnames(
'wp-block-navigation__responsive-container-close',
'wp-overlay-component__close',
'wp-overlay-component__toggle',
{ 'always-shown': isHiddenByDefault }
) }
aria-label={ hasIcon && __( 'Close menu' ) }
onClick={ () => onToggle( false ) }
>
{ hasIcon && <Icon icon={ close } /> }
{ ! hasIcon && __( 'Close' ) }
</Button>
<div
className="wp-block-navigation__responsive-container-content"
className="wp-block-navigation__responsive-container-content wp-overlay-component__content"
id={ `${ modalId }-content` }
>
{ children }
Expand Down
223 changes: 223 additions & 0 deletions packages/block-library/src/navigation/overlay.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// ********************************************************
// VARIABLES & MIXINS
// ********************************************************

// Abstraction for Navigation breakpoint.
@mixin nav-breakpoint() {
@include break-small {
@content;
}
}


// ********************************************************
// ANIMATION
// ********************************************************
@keyframes overlay-menu__fade-in-animation {
from {
opacity: 0;
transform: translateY(0.5em);
}

to {
opacity: 1;
transform: translateY(0);
}
}


// ********************************************************
// ROOT
// Alias: wp-block-navigation__responsive-container
// ********************************************************
.wp-overlay-component {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;

@include nav-breakpoint {
&:not(.hidden-by-default):not(.is-overlay-open) {
display: block;
width: 100%;
position: relative;
z-index: auto;
background-color: inherit;
}
}
Comment on lines +41 to +49
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 assume this code exists so that if the overlay is open and the screen size changes then the overlay becomes hidden?

I'm wondering. If in the future we want to allow the Nav breakpoint to change dynamically based on measuring the width of the Navigation block, then we might like to look at moving all the responsive behaviour to JavaScript with something like matchMedia similar to what I did in #45274.

The only issue is then we'd need to find a way to do a similar thing with directives on the front of the site.

I believe taking such an approach would make this code a lot simpler.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, I was thinking on using a class that will be added or removed once we reach the correct viewport width


// STATE: overlay is open.
// ********************************************************
&.is-overlay-open {
display: flex; // Needs to be set to override "none".
flex-direction: column;
background-color: inherit;

// Try to inherit any root paddings set, so the X can align to a top-right aligned menu.
padding-top: clamp(1rem, var(--wp--style--root--padding-top), 20rem);
padding-right: clamp(1rem, var(--wp--style--root--padding-right), 20rem);
padding-bottom: clamp(1rem, var(--wp--style--root--padding-bottom), 20rem);
padding-left: clamp(1rem, var(--wp--style--root--padding-left), 20em);

// Animation.
animation: overlay-menu__fade-in-animation 0.1s ease-out;
animation-fill-mode: forwards;
@include reduce-motion("animation");

// Allow modal to scroll.
overflow: auto;

// Give it a z-index just higher than the adminbar.
z-index: 100000;
}
}


// ********************************************************
// DIALOG
// Alias: wp-block-navigation__responsive-dialog
// ********************************************************
.wp-overlay-component__dialog {
position: relative;

// STATE: overlay is open.
// ********************************************************
.is-overlay-open & {
// Quesiton: why is this conditionally applied?
box-sizing: border-box;
}
}


// ********************************************************
// CONTENT
// Alias: wp-block-navigation__responsive-container-content
// ********************************************************
.wp-overlay-component__content {

// STATE: overlay is open.
// ********************************************************
.is-overlay-open & {

// Always align the contents of the menu to the top.
justify-content: flex-start;

// Add padding above to accommodate close button.
padding-top: calc(2rem + #{ $navigation-icon-size });

// Don't crop the focus style.
overflow: visible;

// Override the container flex layout settings
// because we want overlay menu to always display
// as a column.
display: flex;
flex-direction: column;
flex-wrap: nowrap;

// Quesiton: why is this conditionally applied?
box-sizing: border-box;
}
}


// ********************************************************
// TOGGLE BUTTONS
// Alias:
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the agnostic classes. We could use @extend to keep backwards compatibility with the old classes

// - .wp-block-navigation__responsive-container-open,
// - .wp-block-navigation__responsive-container-close
// ********************************************************

.wp-overlay-component__toggle {
vertical-align: middle;
cursor: pointer;
color: currentColor;
background: transparent;
border: none;
margin: 0;
padding: 0;
text-transform: inherit;

svg {
fill: currentColor;
pointer-events: none;
display: block;
width: $navigation-icon-size;
height: $navigation-icon-size;
}
}


// ********************************************************
// OPEN BUTTON
// Alias: wp-block-navigation__responsive-container-open
// ********************************************************

.wp-overlay-component__open {
display: flex;

// Hide the button on smaller viewports except
// when the overlay is set to always show.

&:not(.always-shown) {
@include break-small {
display: none;
}
}
}

// ********************************************************
// CLOSE BUTTON
// Alias: wp-block-navigation__responsive-container-close
// ********************************************************

.wp-overlay-component__close {
position: absolute;
top: 0;
right: 0;
z-index: 2; // Needs to be above the modal z index itself.
display: none; // hide by default for all users.

// STATE: overlay is open.
// ********************************************************
.is-overlay-open & {
box-sizing: border-box;

// Reveal when overlay is open.
display: inline-flex; // as per <Button> component.
}

// STATE: overlay is set to always show.
// ********************************************************
&.always-shown {
display: inline-flex; // as per <Button> component.
}
}


// ********************************************************
// MAYBE REDUNDANT
// Alias: wp-block-navigation__responsive-close
// ********************************************************
.wp-overlay-component__maybe-redundant {
width: 100%;

// STATE: overlay is open.
// ********************************************************
.has-modal-open & {
// Try to inherit wide-width when defined, so the X can align to a top-right aligned menu.
max-width: var(--wp--style--global--wide-size, 100%);
margin-left: auto;
margin-right: auto;
}

// This element is not keyboard accessible, and is focusable only using the mouse.
// It is part of the MicroModal library that adds a scrim outside of a modal dialog that is not fullscreen,
// where clicking that scrim closes the overlay just like the close button.
// It should not have a visible focus rectangle.
&:focus {
outline: none;
}
}
Loading
Loading