diff --git a/packages/block-library/CHANGELOG.md b/packages/block-library/CHANGELOG.md
index 29df5a0b68d014..60e68802b93525 100644
--- a/packages/block-library/CHANGELOG.md
+++ b/packages/block-library/CHANGELOG.md
@@ -6,6 +6,7 @@
- Playlist, Playlist Track: Allow tracks without an attachment ID to render on the front end if they have a source URL ([#83880](https://github.com/WordPress/gutenberg/pull/83880)).
- Math: Let keyboard users move focus out of the LaTeX field when the formula has an error, instead of sending focus back to the field ([#86027](https://github.com/WordPress/gutenberg/pull/86027)).
+- Image: Position the lightbox trigger button against the `.wp-lightbox-container` figure instead of the image's parent element, so the button stays over the image when a plugin wraps the `img` in a `picture` element ([#82312](https://github.com/WordPress/gutenberg/pull/82312)).
- Page List: Keep page items visible in the Navigation sidebar during content-only editing ([#85357](https://github.com/WordPress/gutenberg/pull/85357)).
- Template Part: Show the "Default based on area" HTML element as selected instead of the `Select` placeholder ([#85172](https://github.com/WordPress/gutenberg/pull/85172)).
- Navigation: Apply a block's Additional CSS only to its outer wrapper, so borders and padding do not appear twice on its inner list ([#84881](https://github.com/WordPress/gutenberg/pull/84881)).
diff --git a/packages/block-library/src/image/view.js b/packages/block-library/src/image/view.js
index b0aaf53326e0ee..95b60e502f0d30 100644
--- a/packages/block-library/src/image/view.js
+++ b/packages/block-library/src/image/view.js
@@ -922,15 +922,21 @@ const { state, actions, callbacks } = store(
return;
}
- const figure = ref.parentElement;
- const figureWidth = ref.parentElement.clientWidth;
+ // The image is not necessarily a direct child of the figure, as
+ // plugins can wrap it, so it uses the container the button is
+ // positioned against rather than the image's parent element.
+ const container = ref.closest( '.wp-lightbox-container' );
+ if ( ! container ) {
+ return;
+ }
+ const containerWidth = container.clientWidth;
// It needs special handling for the height because a caption will cause
- // the figure to be taller than the image, which means it needs to
+ // the container to be taller than the image, which means it needs to
// account for that when calculating the placement of the button in the
// top right corner of the image.
- let figureHeight = ref.parentElement.clientHeight;
- const caption = figure.querySelector( 'figcaption' );
+ let containerHeight = container.clientHeight;
+ const caption = container.querySelector( 'figcaption' );
if ( caption ) {
const captionComputedStyle =
window.getComputedStyle( caption );
@@ -939,16 +945,16 @@ const { state, actions, callbacks } = store(
captionComputedStyle.position
)
) {
- figureHeight =
- figureHeight -
+ containerHeight =
+ containerHeight -
caption.offsetHeight -
parseFloat( captionComputedStyle.marginTop ) -
parseFloat( captionComputedStyle.marginBottom );
}
}
- const buttonOffsetTop = figureHeight - offsetHeight;
- const buttonOffsetRight = figureWidth - offsetWidth;
+ const buttonOffsetTop = containerHeight - offsetHeight;
+ const buttonOffsetRight = containerWidth - offsetWidth;
let buttonTop = buttonOffsetTop + 16;
let buttonRight = buttonOffsetRight + 16;
diff --git a/packages/e2e-tests/plugins/image-picture-wrapper.php b/packages/e2e-tests/plugins/image-picture-wrapper.php
new file mode 100644
index 00000000000000..b916251d2a3f2b
--- /dev/null
+++ b/packages/e2e-tests/plugins/image-picture-wrapper.php
@@ -0,0 +1,30 @@
+]+>/',
+ '$0',
+ $block_content,
+ 1
+ );
+}
+add_filter( 'render_block', 'gutenberg_test_wrap_image_in_picture', 10, 2 );
diff --git a/test/e2e/specs/editor/blocks/image.spec.js b/test/e2e/specs/editor/blocks/image.spec.js
index 61a0d1c2a5a861..40c835b313d5e1 100644
--- a/test/e2e/specs/editor/blocks/image.spec.js
+++ b/test/e2e/specs/editor/blocks/image.spec.js
@@ -1077,6 +1077,74 @@ test.describe( 'Image - lightbox', () => {
expect( margin ).toBe( '0px' );
} );
+ // Regression test for https://github.com/WordPress/gutenberg/issues/82283.
+ test.describe( 'when a plugin wraps the image', () => {
+ let largeMedia;
+
+ test.beforeAll( async ( { requestUtils } ) => {
+ largeMedia = await requestUtils.uploadMedia(
+ './assets/1024x768_e2e_test_image_size.jpeg'
+ );
+ await requestUtils.activatePlugin(
+ 'gutenberg-test-image-picture-wrapper'
+ );
+ } );
+
+ test.afterAll( async ( { requestUtils } ) => {
+ await requestUtils.deactivatePlugin(
+ 'gutenberg-test-image-picture-wrapper'
+ );
+ } );
+
+ test( 'Trigger button should still be placed inside the figure', async ( {
+ editor,
+ page,
+ } ) => {
+ await editor.setContent( `
+
A caption.
+ ` );
+
+ const postId = await editor.publishPost();
+ await page.setViewportSize( { width: 500, height: 800 } );
+ await page.goto( `/?p=${ postId }` );
+
+ const image = page.locator( '.wp-lightbox-container img' );
+ await expect( image ).toBeVisible();
+ // The button is only positioned once the image has loaded.
+ await expect
+ .poll( () =>
+ page.evaluate(
+ () =>
+ document.querySelector(
+ 'button.lightbox-trigger'
+ ).style.top
+ )
+ )
+ .not.toBe( '' );
+
+ const figureBox = await page
+ .locator( '.wp-lightbox-container' )
+ .boundingBox();
+ const buttonBox = await page
+ .locator( 'button.lightbox-trigger' )
+ .boundingBox();
+
+ expect( buttonBox.y ).toBeGreaterThanOrEqual( figureBox.y );
+ expect( buttonBox.x + buttonBox.width ).toBeLessThanOrEqual(
+ figureBox.x + figureBox.width
+ );
+
+ // A mispositioned button pushes the page sideways.
+ const { scrollWidth, clientWidth } = await page.evaluate(
+ () => ( {
+ scrollWidth: document.documentElement.scrollWidth,
+ clientWidth: document.documentElement.clientWidth,
+ } )
+ );
+ expect( scrollWidth ).toBe( clientWidth );
+ } );
+ } );
+
test.describe( 'Overlay not a direct child of body', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activatePlugin(