Skip to content

Image lightbox: overlay becomes unusable (can't close) when it isn't a direct child of <body> #83479

Closed

Description

@hbhalodia

Description

Taking out from here to discuss first - https://core.trac.wordpress.org/ticket/66084
Site that is mentioned in the ticket - https://integracontrols.com/ddc-controls-in-hvac/

Note: The mentioned site's structure needs to be change though to fix the issue on the site, as the wrapper which opens up in site is not being closed correctly. (If that closed after the footer then the bug needs to be fixed upstream here, as that is now a valid HTML).

In WordPress 7.0, when the image lightbox opens, it marks the rest of the page as inert here:

// packages/block-library/src/image/view.js — callbacks.setInertElements()
document.querySelectorAll( 'body > :not(.wp-lightbox-overlay)' )
	.forEach( ( el ) => { /* set or remove inert */ } );

This assumes the overlay is a direct child of <body>. Core doesn't guarantee that, though. The overlay is printed on wp_footer (block_core_image_print_lightbox_overlay()), so it ends up inside whatever element is still open at that point.
 
If wp_footer() runs inside a wrapper element, that wrapper gets inert, and the overlay inherits it. The lightbox opens but can't be used:

  • the close button doesn't respond
  • the prev/next buttons don't respond
  • clicking the backdrop does nothing
  • Esc does nothing, because focus can't move into an inert subtree

The user can't close the lightbox without reloading the page.
This is a regression. Up to 6.9 the lightbox didn't set inert, so these sites worked with an unwrapped div as well.

Step-by-step reproduction instructions

Step-by-step reproduction

  1. Start a fresh WordPress 7.0 site with a default theme (e.g. Twenty Twenty-Five).
  2. Add this mu-plugin. It wraps the page in a <div> that closes after the overlay is printed, which is valid HTML:
   <?php
   add_action( 'wp_body_open', function () { echo '<div id="site-wrap">'; } );
   add_action( 'wp_footer', function () { echo '</div>'; }, 100 );
  1. Create a post with an Image block and turn on Expand on click.
  2. Open the post on the front end and click the image.
  3. Try to close the lightbox with the close button, the backdrop or Esc.

Expected: the lightbox closes.
Actual: nothing responds. document.getElementById('site-wrap').inert is true, and the overlay sits inside it.

Suggested fix

Instead of only inerting body > *, walk up from the overlay and inert the siblings at each level. That way no ancestor of the overlay is ever made inert:

setInertElements() {
	const { ref } = getElement();
	let node = ref;
	while ( node && node !== document.body && node.parentElement ) {
		for ( const sibling of node.parentElement.children ) {
			if ( sibling === node ) continue;
			if ( state.overlayEnabled ) {
				sibling.setAttribute( 'inert', '' );
			} else {
				sibling.removeAttribute( 'inert' );
			}
		}
		node = node.parentElement;
	}
},

An alternative is to move the overlay to document.body when the store initializes. The overlay already declares data-wp-router-region='{ "id": "core/image-overlay", "attachTo": "body" }', so body-level placement seems to be the intent.

Screenshots, screen recording, code snippet

The issue on site mentioned

Screen.Recording.2026-09-24.at.12.46.51.PM.mov

Using mu plugint to wrap a valid div before header and after footer

Screen.Recording.2026-09-24.at.12.55.42.PM.mov

Environment info

  • WordPress 7.0
  • GB trunk
  • Also reproducible with the minimal mu-plugin above on a default theme

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Activity

  1. added
    [Type] Bug
    [Package] Block library
    on Sep 24, 2026
  2. added theissue type on Sep 24, 2026
  3. added
    [Block] Image
    on Sep 24, 2026
  4. self-assigned this
    on Sep 24, 2026
  5. changed the title [-]Image lightbox: overlay becomes unusable (can't close) when it isn't a direct child of `<body>[/-] [+]Image lightbox: overlay becomes unusable (can't close) when it isn't a direct child of `<body>`[/+] on Sep 24, 2026
  6. added
    [Type] Regression
    on Sep 24, 2026
  7. added
    [Status] In Progress
    on Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[Block] Image[Package] Block library[Status] In Progress[Type] Bug[Type] Regression

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions