Skip to content
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

Add lightbox support to Core Image blocks with links #3460

Merged
merged 9 commits into from
Oct 21, 2019

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Oct 8, 2019

Summary

As reported in #3450, there's an issue where images that are wrapped in <a> didn't support the lightbox feature. Clicking on them simply caused the browser to go to that link.

So this recognizes when an image is wrapped in <a>, and runs the lighbox logic for it.

Steps to reproduce:

  1. Create a post
  2. Add an Image block
  3. Add any image to it
  4. Click the 'link' icon, and click 'Media File' or 'Attachment Page':

image-add-link

  1. Click 'Add lightbox effect'
  2. Click 'Update,' and go to the front-end
  3. Expected: Clicking that image causes the lightbox to display
  4. Actual: it goes to the URL of the image:

image-attachment-url

  1. Repeat all of the steps above, and after step 4, set the alignment of the image to 'Align Right':

setting-alignment

The expected outcome is the same: clicking the image on the front-end causes the lightbox to display.

Fixes #3450

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

There was an issue where images that are wrapped in
<a> didn't support the lightbox feature,
as clicking on them simply caused the browser
to go to that link.
So this recognizes when an image is wrapped in <a>,
and it strips that.
@googlebot googlebot added the cla: yes Signed the Google CLA label Oct 8, 2019
@kienstra
Copy link
Contributor Author

Request To Test

Hi @rsmith4321,
Hope you've had a great week.

When you have a chance, could you please test this PR?

It should fix your Issue #3450, and shouldn't require the workaround of removing the Image block's link.

Thanks, Ryan!

@@ -393,13 +398,18 @@ private function maybe_add_lightbox_attributes( $attributes, $node ) {
$attributes['tabindex'] = 0;

$this->maybe_add_amp_image_lightbox_node();

if ( $is_node_wrapped_in_anchor ) {
// Remove the <a> that the image is wrapped in, as it can prevent the lightbox from working.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check if this issue has been reported in AMP? Perhaps it is a bug in AMP itself that should be fixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a good point. It is a little strange that the plugin has to remove the <a>.

Copy link
Contributor Author

@kienstra kienstra Oct 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find an issue in http://github.com/ampproject/amphtml for this, but I'll keep looking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also might open an issue in amphtml for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened an issue in amphtml: ampproject/amphtml#25021

@schlessera
Copy link
Collaborator

Code looks good to me. However, we should wait for feedback on ampproject/amphtml#25021 before proceeding.

@kienstra
Copy link
Contributor Author

Code looks good to me. However, we should wait for feedback on ampproject/amphtml#25021 before proceeding.

Nice, thanks!

It sounds like this is the recommended component
for lightboxes,
not the previous amp-image-ligthbox.
if ( $is_node_wrapped_in_anchor ) {
// Remove the <a> that the image is wrapped in, as it can prevent the lightbox from working.
$node->parentNode->parentNode->replaceChild( $node, $node->parentNode );
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If amphtml later supports wrapping <amp-img lightbox> in <a>, this can be removed.

@kienstra
Copy link
Contributor Author

kienstra commented Oct 15, 2019

This PR is now changing the Core Image block from using the <amp-image-lightbox> to the recommended <amp-lightbox-gallery>. It looks like <amp-image-lightbox> will eventually be deprecated.

It looks like this works well so far. The only change I noticed is that all <amp-img lightbox> elements will be visible in the lightbox:

amp-gallery-lightbox

If the amphtml feature is added,
the code can probably be removed.
@westonruter westonruter added this to the v1.3.1 milestone Oct 15, 2019
@westonruter
Copy link
Member

@rsmith4321 Would you test this please? Here's a build for testing: amp.zip

@rsmith4321
Copy link

rsmith4321 commented Oct 16, 2019 via email

@westonruter
Copy link
Member

It still has the same issue of the lightbox not opening if it's linked to the media file surrounded by an tag.

I'm confused. @kienstra I thought this is what the $is_node_wrapped_in_anchor was for?

@kienstra
Copy link
Contributor Author

@westonruter, right, this PR should allow the image to be wrapped in a <a>. Let me look at this more.

@westonruter
Copy link
Member

If the link is for the media file itself, then I think it is correct to remove the hyperlink in favor of the lightbox alone, since clicking the link would just cause the image to be loaded on a separate page; here a lightbox is a better experience since you don't leave the page context.

If, however, the link is for the attachment page, then in this case I think it is better to retain the link.

@kienstra
Copy link
Contributor Author

If the link is for the media file itself, then I think it is correct to remove the hyperlink in favor of the lightbox alone, since clicking the link would just cause the image to be loaded on a separate page; here a lightbox is a better experience since you don't leave the page context.

If, however, the link is for the attachment page, then in this case I think it is better to retain the link.

Sounds good, I'll work on applying that.

@kienstra
Copy link
Contributor Author

kienstra commented Oct 16, 2019

Hi @rsmith4321,

It still has the same issue of the lightbox not opening if it's linked to the media file surrounded by an tag.

Thanks for testing this!

Is that on a public site, by chance, and could you share a link to it?

That might help to reproduce it.

Locally, when linking to the Media File and selecting the lightbox, the lightbox works:

ligthbox-media

Thanks again, Ryan! You've been really helpful in testing everything.

@rsmith4321
Copy link

rsmith4321 commented Oct 16, 2019 via email

…page

As Weston mentioned, the lightbox is a better
experience than simply going to the URL
of the media file.

But if it's for the attachment page,
don't add the lightbox attribute,
and don't strip the <a>.
@kienstra
Copy link
Contributor Author

Thanks, I also see the issue with the lightbox not working.

In the link provided, it looks like the lightbox doesn't work when it's for an Image block that has a link and alignment:

has-alignment-and-a

Image blocks with alignment are wrapped in an extra <div>. The logic accounts for that extra div, or an extra <a> from the link, but not both.

I'll work on this.

@swissspidy swissspidy modified the milestones: v1.3.1, v1.4 Oct 17, 2019
Before, there was handling for links or alignment,
but not both.
So add a way to get the great-grandparent of the <img>.
This doesn't pass without
passing the object to it.
It looks like it won't be implemented
for backwards-compatibility reasons.
This caused an error in
PHP 5.4 and 5.5.
@kienstra
Copy link
Contributor Author

kienstra commented Oct 18, 2019

Request To Test

Hi @rsmith4321,
Thanks for your patience. Could you please test this amp.zip, which should fix the issue with lightboxes on the URL you provided?

This shouldn't be an issue on that URL you shared, but the lightbox will only work if the link is to the 'Media File,' not to the 'Attachment Page':
link-to-attachment-page

...though on that URL you provided, it looks like the URLs are to the 'Media File,' so they should be fine.

Thanks, Ryan! You've been really helpful in reporting and testing these issues. Have a great weekend.

$classes = preg_split( '/\s+/', $grand_parent->getAttribute( 'class' ) );
if ( in_array( 'wp-block-image', $classes, true ) ) {
$parent_node = $grand_parent;
}
Copy link
Contributor Author

@kienstra kienstra Oct 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic above is mainly copied into does_node_have_block_class()

@rsmith4321
Copy link

rsmith4321 commented Oct 18, 2019 via email

@kienstra
Copy link
Contributor Author

Thanks, Ryan! Right, it does look like the issue still exists on that URL. I'll look at that.

@kienstra
Copy link
Contributor Author

Maybe attachment_url_to_postid isn't returning the ID, as expected. Though I couldn't reproduce this locally.

&&
( 'figure' === $parent_node->tagName || 'figure' === $parent_node->parentNode->tagName )
&&
attachment_url_to_postid( $parent_node->getAttribute( 'href' ) )
Copy link
Member

@westonruter westonruter Oct 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I recall, attachment_url_to_postid() is not super reliable. I think it would be better to look at the URL to see if it looks like a file instead. For example:

$is_file_url = preg_match( '/\.\w+$/', wp_parse_url( $parent_node->getAttribute( 'href' ), PHP_URL_PATH ) );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's applied in 783a8d8

Use the line that Weston suggested,
instead of attachment_url_to_postid().
@kienstra
Copy link
Contributor Author

Hi @rsmith4321,
Sorry to keep sending you plugin builds to test.

But here's another amp.zip when you have a chance.

Thanks a lot for your help.

@rsmith4321
Copy link

rsmith4321 commented Oct 21, 2019 via email

@rsmith4321
Copy link

rsmith4321 commented Oct 21, 2019 via email

@swissspidy
Copy link
Collaborator

Glad to hear it's working!


Personally I don't think the AMP plugin should change the cursor as it's the theme's responsibility.

You should be able to use the following CSS in your theme to change the cursor:

[data-amp-lightbox="true"] { cursor: pointer; }

@kienstra
Copy link
Contributor Author

Hi @rsmith4321,
Thanks a lot for testing this so many times. It's good to hear it's working.

The only thing I notice on a mouse hover the cursor is not changed to a
pointer because it's not a link anymore.

Sorry, it would have been ideal if it could have still been wrapped in a link. We ended up having to strip.

The styling above should work, let me know if there's an issue.

@kienstra
Copy link
Contributor Author

Request For Final Review

Hi @westonruter,
Happy Monday!

When you have a chance, could you please review this PR again?

In addition to the testing steps above, this should also work when adding alignment to the image, like alignright, after testing step 4.

Thanks, Weston!

@westonruter westonruter merged commit 468b349 into develop Oct 21, 2019
@westonruter westonruter deleted the update/3450-lightbox-anchor branch October 21, 2019 16:14
@rsmith4321
Copy link

Glad to hear it's working!

Personally I don't think the AMP plugin should change the cursor as it's the theme's responsibility.

You should be able to use the following CSS in your theme to change the cursor:

[data-amp-lightbox="true"] { cursor: pointer; }

Thanks I'll use that. I would agree except you are doing something unusual by stripping the link. Normally a lightbox would open from a link to the image so the cursor would change. So I don't think any theme is going to ever know to add that css.

@kienstra
Copy link
Contributor Author

Thanks I'll use that. I would agree except you are doing something unusual by stripping the link.

Yeah, that's a good point. We tried to get around stripping the link, but the amphtml project couldn't change the behavior, due to backwards-compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add lightbox effect doesn't work for image blocks
6 participants