Skip to content

Commit

Permalink
Fix the navigation issue inside cover blocks (WordPress#66093)
Browse files Browse the repository at this point in the history
Co-authored-by: renatho <renathoc@git.wordpress.org>
Co-authored-by: kevin940726 <kevin940726@git.wordpress.org>
Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
  • Loading branch information
6 people authored and karthick-murugan committed Nov 13, 2024
1 parent 4abf154 commit 80381d4
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/block-library/src/cover/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
color: inherit;
// Reset the fixed LTR direction at the root of the block in RTL languages.
/*rtl:raw: direction: rtl; */

// Reset the z-index to auto when the body has a modal open. So when the
// modal is inside the cover, it doesn't create a stacking context.
@at-root .has-modal-open & {
z-index: auto;
}
}

// Position: Top
Expand Down
33 changes: 30 additions & 3 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ import AccessibleDescription from './accessible-description';
import AccessibleMenuDescription from './accessible-menu-description';
import { unlock } from '../../lock-unlock';

function useResponsiveMenu( navRef ) {
const [ isResponsiveMenuOpen, setResponsiveMenuVisibility ] =
useState( false );

useEffect( () => {
if ( ! navRef.current ) {
return;
}

const htmlElement = navRef.current.ownerDocument.documentElement;

// Add a `has-modal-open` class to the <html> when the responsive
// menu is open. This reproduces the same behavior of the frontend.
if ( isResponsiveMenuOpen ) {
htmlElement.classList.add( 'has-modal-open' );
} else {
htmlElement.classList.remove( 'has-modal-open' );
}

return () => {
htmlElement?.classList.remove( 'has-modal-open' );
};
}, [ navRef, isResponsiveMenuOpen ] );

return [ isResponsiveMenuOpen, setResponsiveMenuVisibility ];
}

function ColorTools( {
textColor,
setTextColor,
Expand Down Expand Up @@ -284,8 +311,10 @@ function Navigation( {
__unstableMarkNextChangeAsNotPersistent,
} = useDispatch( blockEditorStore );

const navRef = useRef();

const [ isResponsiveMenuOpen, setResponsiveMenuVisibility ] =
useState( false );
useResponsiveMenu( navRef );

const [ overlayMenuPreview, setOverlayMenuPreview ] = useState( false );

Expand Down Expand Up @@ -367,8 +396,6 @@ function Navigation( {
__unstableMarkNextChangeAsNotPersistent,
] );

const navRef = useRef();

// The standard HTML5 tag for the block wrapper.
const TagName = 'nav';

Expand Down
71 changes: 71 additions & 0 deletions test/e2e/specs/editor/blocks/cover.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,77 @@ test.describe( 'Cover', () => {
await expect( overlay ).toHaveCSS( 'background-color', 'rgb(0, 0, 0)' );
await expect( overlay ).toHaveCSS( 'opacity', '0.5' );
} );

test( 'other cover blocks are not over the navigation block when the menu is open', async ( {
editor,
page,
} ) => {
// Insert a Cover block
await editor.insertBlock( { name: 'core/cover' } );
const coverBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Cover',
} );

// Choose a color swatch to transform the placeholder block into
// a functioning block.
await coverBlock
.getByRole( 'option', {
name: 'Color: Black',
} )
.click();

// Insert a Navigation block inside the Cover block
await editor.selectBlocks( coverBlock );
await coverBlock.getByRole( 'button', { name: 'Add block' } ).click();
await page.keyboard.type( 'Navigation' );
const blockResults = page.getByRole( 'listbox', {
name: 'Blocks',
} );
const blockResultOptions = blockResults.getByRole( 'option' );
await blockResultOptions.nth( 0 ).click();

// Insert a second Cover block.
await editor.insertBlock( { name: 'core/cover' } );
const secondCoverBlock = editor.canvas
.getByRole( 'document', {
name: 'Block: Cover',
} )
.last();

// Choose a color swatch to transform the placeholder block into
// a functioning block.
await secondCoverBlock
.getByRole( 'option', {
name: 'Color: Black',
} )
.click();

// Set the viewport to a small screen and open menu.
await page.setViewportSize( { width: 375, height: 1000 } );
const navigationBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Navigation',
} );
await editor.selectBlocks( navigationBlock );
await editor.canvas
.getByRole( 'button', { name: 'Open menu' } )
.click();

// Check if inner container of the second cover is clickable.
const secondInnerContainer = secondCoverBlock.locator(
'.wp-block-cover__inner-container'
);
let isClickable;
try {
isClickable = await secondInnerContainer.click( {
trial: true,
timeout: 1000, // This test will always take 1 second to run.
} );
} catch ( error ) {
isClickable = false;
}

expect( isClickable ).toBe( false );
} );
} );

class CoverBlockUtils {
Expand Down

0 comments on commit 80381d4

Please sign in to comment.