Repository navigation
Fix: Image lightbox: overlay becomes unusable (can't close) when it isn't a direct child of <body>
#83480
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: Image lightbox: overlay becomes unusable (can't close) when it isn't a direct child of <body>
#83480
Changes from all commits
97fd7b5
17e8263
c3cb2d3
ae0021b
9e9312c
52159d1
883c28f
898c454
58c88f7
ed296aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = []; | ||
|
|
||
| const focusableSelectors = [ | ||
| '.wp-lightbox-close-button', | ||
| '.wp-lightbox-navigation-button', | ||
|
|
@@ -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 ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
|
||
| 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 ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.