Skip to content
Merged
5 changes: 5 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

- Columns: Remove the column count slider from the block settings ([#83262](https://github.com/WordPress/gutenberg/pull/83262)).

### Bug Fixes

- Image: Fix the lightbox being impossible to close when its overlay is not a direct child of `<body>` ([#83480](https://github.com/WordPress/gutenberg/pull/83480)).
- Image: Stop the lightbox from removing `inert` that a theme set on elements outside the overlay ([#83480](https://github.com/WordPress/gutenberg/pull/83480)).

## 11.1.0 (2026-09-23)

### Enhancements
Expand Down
38 changes: 29 additions & 9 deletions packages/block-library/src/image/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ const touchStartEvent = {
startTime: 0,
};

/**
* Elements made inert while the lightbox is open, so only those are restored
* when it closes.
*
* @type {Element[]}
*/
let inertElements = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Restoring only what we added is a nice touch, but it isn't part of the regression.

Trunk removed inert from every direct child of <body> on close (and on first load), including inert the theme had set.

Was that a deliberate choice in #62906, or just missed?

If we agree it's worth keeping, could we mention it in the CHANGELOG entry too, since it changes behaviour for themes that set inert on those elements?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The unintentional removal of the inert attribute is an issue and an oversight in #62906.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the confirmation, Aki! Hit, could you please add this change to the CHANGELOG?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks Aki, Eshaan.

I have updated the PR to include e2e tests. Can you please have final review on that and we can make it forward.


const focusableSelectors = [
'.wp-lightbox-close-button',
'.wp-lightbox-navigation-button',
Expand Down Expand Up @@ -690,16 +698,28 @@ const { state, actions, callbacks } = store(
}
},
setInertElements() {
// Makes all children of the document inert exempt .wp-lightbox-overlay.
document
.querySelectorAll( 'body > :not(.wp-lightbox-overlay)' )
.forEach( ( el ) => {
if ( state.overlayEnabled ) {
el.setAttribute( 'inert', '' );
} else {
el.removeAttribute( 'inert' );
if ( ! state.overlayEnabled ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we add an e2e test for this?

Today nothing closes the lightbox in a test: the existing lightbox test only opens it, which is how this slipped through.

The mu-plugin from issue could become a test plugin, and the test would open the lightbox and check it can be closed.

Happy for this to be a follow-up if you'd rather keep this PR focused on the fix.

inertElements.forEach( ( el ) =>
el.removeAttribute( 'inert' )
);
inertElements = [];
return;
}
// Inerts the overlay's siblings at each level, not its ancestors.
const { ref } = getElement();
let node = ref;
while ( node && node !== document.body && node.parentElement ) {
for ( const sibling of node.parentElement.children ) {
if (
sibling !== node &&
! sibling.hasAttribute( 'inert' )
) {
sibling.setAttribute( 'inert', '' );
inertElements.push( sibling );
}
} );
}
node = node.parentElement;
}
},
initTriggerButton() {
const { imageId } = getContext();
Expand Down
25 changes: 25 additions & 0 deletions packages/e2e-tests/plugins/lightbox-overlay-wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Plugin Name: Gutenberg Test Lightbox Overlay Wrapper
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-lightbox-overlay-wrapper
*/

/**
* Opens a wrapper around the page, so the lightbox overlay printed on
* `wp_footer` is not a direct child of `<body>`.
*/
function gutenberg_test_lightbox_overlay_wrapper_open() {
echo '<div id="site-wrap">';
}
add_action( 'wp_body_open', 'gutenberg_test_lightbox_overlay_wrapper_open' );

/**
* Closes the wrapper after the lightbox overlay has been printed.
*/
function gutenberg_test_lightbox_overlay_wrapper_close() {
echo '</div>';
}
add_action( 'wp_footer', 'gutenberg_test_lightbox_overlay_wrapper_close', 100 );
56 changes: 56 additions & 0 deletions test/e2e/specs/editor/blocks/image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,62 @@ test.describe( 'Image - lightbox', () => {
} );
expect( margin ).toBe( '0px' );
} );

test.describe( 'Overlay not a direct child of body', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activatePlugin(
'gutenberg-test-lightbox-overlay-wrapper'
);
} );

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.deactivatePlugin(
'gutenberg-test-lightbox-overlay-wrapper'
);
} );

test( 'should make only the rest of the page inert and restore it on close', async ( {
editor,
page,
} ) => {
await editor.setContent( `<!-- wp:image {"id":${ uploadedMedia.id },"sizeSlug":"full","linkDestination":"none","lightbox":{"enabled":true}} -->
<figure class="wp-block-image size-full"><img src="${ uploadedMedia.source_url }" alt="" class="wp-image-${ uploadedMedia.id }"/></figure>
<!-- /wp:image --> ` );

const postId = await editor.publishPost();
await page.goto( `/?p=${ postId }` );

const wrapper = page.locator( '#site-wrap' );
const pageContent = page.locator(
'#site-wrap > :has(.wp-lightbox-container)'
);
const overlay = page.locator(
'#site-wrap > .wp-lightbox-overlay'
);
await expect( overlay ).toBeAttached();

// A direct child of <body> the theme made inert must stay inert
// after closing.
await page.evaluate( () => {
const themeInert = document.createElement( 'div' );
themeInert.id = 'theme-inert';
themeInert.inert = true;
document.body.prepend( themeInert );
} );

await page.locator( '.wp-lightbox-container img' ).click();
await expect( overlay ).toHaveClass( /active/ );
await expect( wrapper ).not.toHaveAttribute( 'inert' );
await expect( pageContent ).toHaveAttribute( 'inert' );

await overlay.getByRole( 'button', { name: 'Close' } ).click();
await expect( overlay ).not.toHaveClass( /active/ );
await expect( pageContent ).not.toHaveAttribute( 'inert' );
await expect( page.locator( '#theme-inert' ) ).toHaveAttribute(
'inert'
);
} );
} );
} );
} );

Expand Down
Loading