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

Prevent loading auto lightbox when disabled using data-amp-auto-lightbox-disable attribute #37854

Merged
merged 9 commits into from
Apr 14, 2022

Conversation

deepaklalwani97
Copy link
Contributor

Fixes #26452

  • This PR prevents loading the amp-autoligthbox JS from loading if the data-amp-auto-lightbox-disable attribute is added to the body tag

@deepaklalwani97 deepaklalwani97 marked this pull request as ready for review March 11, 2022 05:32
@amp-owners-bot amp-owners-bot bot requested review from caroqliu and samouri March 11, 2022 05:32
Comment on lines 67 to 69
// Explicitly opted out.
'[data-amp-auto-lightbox-disable]',

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we want this documentation here still? How else will people know?

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 removed this line from here because it won't be executed as we are excluding the amp-auto-lightbox.js script if the data-amp-auto-lightbox-disable attribute is present in the body. Can you suggest any .md file where we can document this attribute for people to know?

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be left, since it allows individual opt outs without disabling all auto-lightbox.

@deepaklalwani97
Copy link
Contributor Author

Hey @dethstrobe just following up on this PR. Please let me know if I can help unblock this PR in any way.

Comment on lines 67 to 69
// Explicitly opted out.
'[data-amp-auto-lightbox-disable]',

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be left, since it allows individual opt outs without disabling all auto-lightbox.

if (
!isAmphtml(win.document) ||
!ampdoc.isSingleDoc() ||
win.document.body.hasAttribute('data-amp-auto-lightbox-disable')
Copy link
Contributor

Choose a reason for hiding this comment

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

When this is called, we can't guarantee that the <body> (or all of its attributes) have bene parsed. It would be safer to put this on the <html> element instead.

Copy link
Contributor

@AnuragVasanwala AnuragVasanwala Apr 14, 2022

Choose a reason for hiding this comment

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

@jridgewell Should it be something like this:

<html  lang="en" data-amp-auto-lightbox-disable>
...
</html>

Please correct me if there is misinterpretation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's correct.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. Let me revise implementation 👍🏻

Copy link
Contributor

Choose a reason for hiding this comment

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

@jridgewell Done. I have revised implementation and updated markdown (refer 02ff8d1)

Let me know if there is anything that needs to be taken care of.

Marking `data-amp-auto-lightbox-disable` as `<html ⚡ lang="en" data-amp-auto-lightbox-disable>` will disable `amp-auto-lightbox` from auto-load on page load.
…y` to `html`

This will check if HTML Tag has `data-amp-auto-lightbox-disable` attribute like:
`<html ⚡ lang="en" data-amp-auto-lightbox-disable>`. If it has, it will disable auto-loading of `amp-auto-lightbox` on page load.
@@ -241,10 +241,6 @@ describes.realWin(
accepts: 'elements inside non-clickable anchor',
wrapWith: () => html` <a id="my-anchor"></a> `,
},
{
rejects: 'explicitly opted-out subnodes',
mutate: (el) => el.setAttribute('data-amp-auto-lightbox-disable', ''),
Copy link
Contributor

Choose a reason for hiding this comment

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

Undo this removal.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done 👍🏻

@jridgewell jridgewell merged commit 4069d8b into ampproject:main Apr 14, 2022
@westonruter
Copy link
Member

So adding data-amp-auto-lightbox-disable to the body prevents the auto-lightbox behavior, but it doesn't prevent the script from being loaded? To prevent the script from being loaded and to prevent the behavior, you have to add it to the body and the html element?

@westonruter
Copy link
Member

I ask because the AMP plugin now is adding data-amp-auto-lightbox-disable to the body by default (ampproject/amp-wp#6936).

@jridgewell
Copy link
Contributor

I believe you only need to add it to the <html> element to prevent both. Adding to the <body> acts only to prevent individual DOM trees (in this case, everything on the page) from being presented in the lightbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: allow doc level opt-out of amp-auto-lightbox
6 participants