Skip to content

Image block: lightbox trigger button is mispositioned when the img is wrapped #82283

Closed

Description

@sanketio

Description

setButtonStyles measures ref.parentElement (view.js#L621-L629), assuming the img is a direct child of the figure. When a plugin wraps the img — for example Modern Image Formats' <picture style="display: contents"> — that wrapper generates no box, so clientWidth/clientHeight read 0, the button gets right: -offsetWidth + 16, and the page scrolls horizontally. figure.querySelector( 'figcaption' ) on the next line also returns null, so the caption height is never subtracted.

Proposed fix: use ref.closest( '.wp-lightbox-container' ). That is the element the button is positioned against (.wp-lightbox-container is position: relative, the button position: absolute), it is guaranteed present since block_core_image_render_lightbox() adds the class, and it resolves to the same node as parentElement for unwrapped images.

#57089 made the trigger button ref resilient to the same wrapping.

Step-by-step reproduction instructions

  1. Install Modern Image Formats; enable "Generate JPEG and WebP" and the picture element setting under Settings → Media.
  2. Upload a JPEG, add it to an Image block, enable "Expand on click".
  3. View the post at a ~500px viewport.

The trigger button renders outside the figure and the page scrolls horizontally.

Environment

WordPress 7.1, Twenty Twenty-Five.

Reported downstream: WordPress/performance#1918

Metadata

Metadata

Labels

[Block] ImageAffects the Image Block[Status] In ProgressTracking issues with work in progress[Type] BugAn existing feature does not function as intended[Type] Plugin InteroperabilityIncompatibilities between a specific plugin and the block editor. Close with workaround notes.

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions