-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Image Block][Lightbox] console error in frontend when closing lightbox when img tag is added inside picture tag #56541
Comments
Adding more context on how we are trying to fix this issue now: Here image block is adding a new attribute to the image tag data-wp-init to call the init code that we mentioned above. based on this document we can define multiple wp-init directives but with the name changed, If we managed to call our init code after theirs, we will be able to check if this mentioned variable inside context is null so we can define it whatever we need. So we managed using filters to change Any idea? |
I've tested the issue and unfortunately, it doesn't seem like this plugin is compatible with the lightbox. I didn't manage to reproduce the problem but when the plugin is activated, I can see other issues:
Screen.Recording.2023-12-07.at.19.51.03.mov |
I wrote code to reproduce this problem. This is the code to simply wrap the img element with a picture element. function render_block_image_with_picture( $block_content ) {
preg_match( '/<img.*?\/>/', $block_content, $matches );
if ( ! isset( $matches[0] ) ) {
return $block_content;
}
$image_tag = $matches[0];
$new_image_tag = sprintf(
'<picture>%s</picture>',
$image_tag
);
$block_content = str_replace( $image_tag, $new_image_tag, $block_content );
return $block_content;
}
add_filter( 'render_block_core/image', 'render_block_image_with_picture', 20 ); If you open a Lightbox by clicking on an image on the front end and close the Lightbox, the following error will be logged in the browser console.
This hook has a priority of 20, while the Lightbox HTML is added via a hook with a higher priority of 15. In other words, the following processing is performed:
As a result, HTML similar to the following will be generated. <figure class="wp-block-image">
<picture>
<img class="wp-image-1">
</picture>
<button class="lightbox-trigger"><svg><path></path></svg></button>
</figure> The code below causes an error because the parent of the img element is no longer a figure element (i.e. a block wrapper). gutenberg/packages/block-library/src/image/view.js Lines 229 to 234 in 6989045
This instability is also mentioned in #55407. I think we need to find an approach that allows Lightbox to work correctly without errors, no matter what priority the image block rendering is hooked to. |
Exactly @t-hamano I was about to send you a way to test that, but you got it by yourself, many thanks. Add to that, you can replicate the issue with the filter I believe this needs to be fixed from the JS side as I mentioned in the issue description: This can be easily fixed by changing this line
To
so this will get the closest container class and move from there, what do u think? Also I have a question to make workaround for it:
|
I thought of an approach using the |
I like this new approach, hope it's approved and released soon. |
Description
When using image optimizer plugin that adds the webp conversion functionality with image block lightbox feature (expand on click), it shows console error when closing the lightbox modal.
We shouldn't see any console error in this case.
Step-by-step reproduction instructions
Screenshots, screen recording, code snippet
When using image optimizer plugin that adds the webp conversion functionality, it converts every img tag (here this img tag is generated from image block with lightbox enabled) in the page to a picture tag with sources and img tag inside like below:
image block adds a button with class lightbox-trigger and then in js they use
as in here
ref
here is the img tag and its parent in our case is the picture tag not the main container tag and this picture tag doesn't contain this button socontext.core.image.lightboxTriggerRef
equals null in this case, and when closing the lightbox it fails here:https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/image/view.js#L155
This can be easily fixed by changing this line
To
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
No
The text was updated successfully, but these errors were encountered: