Skip to content

✨ InaboxResources: Observe intersections for some elements' viewportCallbacks#26942

Merged
alanorozco merged 7 commits intoampproject:masterfrom
alanorozco:viewport
Feb 26, 2020
Merged

✨ InaboxResources: Observe intersections for some elements' viewportCallbacks#26942
alanorozco merged 7 commits intoampproject:masterfrom
alanorozco:viewport

Conversation

@alanorozco
Copy link
Copy Markdown
Member

@alanorozco alanorozco commented Feb 24, 2020

viewportCallback is not triggered from InaboxResources. For browsers that support IntersectionObserver, use that instead to trigger them.

Copy link
Copy Markdown
Contributor

@lannka lannka left a comment

Choose a reason for hiding this comment

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

I like it. @dvoytenko I think we talked a bit about this. do you think we should do something like this for all AMP extensions? maybe an InOb service.

@dvoytenko
Copy link
Copy Markdown
Contributor

My one question: does carousel need a viewportCallback? Or is it only for its children?

Yeah, if we did something like this, we should look into how to do this higher up for other elements as well. But I do think the elements have to opt-in.

@alanorozco
Copy link
Copy Markdown
Member Author

@dvoytenko It's to start/pause autoplay. The actual signal here is visibility, so I'm unsure what a better model would be.

@dvoytenko
Copy link
Copy Markdown
Contributor

@dvoytenko It's to start/pause autoplay. The actual signal here is visibility, so I'm unsure what a better model would be.

Got it. We need do something like this. But it looks like for now it's only needed in inabox? Can we use some API separation for inabox to limit this for now?

@alanorozco
Copy link
Copy Markdown
Member Author

@dvoytenko RE: siloing to inabox, should we try to get rid of viewportCallback in general in favor of IntersectionObserver everywhere?

This would also remove the need for a scroll-bound event handler on video, and help with embedding video components and amp-timeago on Bento.

@dvoytenko
Copy link
Copy Markdown
Contributor

@dvoytenko RE: siloing to inabox, should we try to get rid of viewportCallback in general in favor of IntersectionObserver everywhere?

This would also remove the need for a scroll-bound event handler on video, and help with embedding video components and amp-timeago on Bento.

If we talk about the future: absolutely, I'd like to get rid of the viewportCallback and use InOb instead. However, a few things will have to change for this and that's why I recommend solving this for inabox first. Things that need to change before we can do this in the main AMP codebase are:

  1. We will need to switch from a generic viewportCallback to a more feature-specific InOb and it always has to be opt-in. In other words - viewportCallback should no longer be called on every single element. Instead loader system could use its own InOb and other features could use their own.
  2. We need to solve for InOb polyfill. Or, alternatively, we could define a behavior when it's absent per feature. For some features it could be the feature is disabled (e.g. loaders are disabled entirely on IE) vs some other features we assume an element is always in viewport (yeah, bad for performance, but...).

@alanorozco
Copy link
Copy Markdown
Member Author

We will need to switch from a generic viewportCallback to a more feature-specific InOb and it always has to be opt-in. In other words - viewportCallback should no longer be called on every single element. Instead loader system could use its own InOb and other features could use their own.

It sounds like we can make the opt-in API here similar to the InOb-specific API you mention, just removing knowledge of viewportCallbacks but not siloing it to inabox.

We need to solve for InOb polyfill. Or, alternatively, we could define a behavior when it's absent per feature. For some features it could be the feature is disabled (e.g. loaders are disabled entirely on IE) vs some other features we assume an element is always in viewport (yeah, bad for performance, but...).

Fair, I believe this PR still leaves the opportunity for that to be solved in the future. FWIW, IE is left out from this workaround.

Let me know if I'm missing something about making this specific to inabox at the moment.

Copy link
Copy Markdown
Contributor

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

I think just adding viewportCallback support in inabox is easier here, then we can migrate the entire API to InOb later.

@alanorozco alanorozco changed the title ✨ Use IntersectionObserver for amp-carousel 0.1 viewport callbacks ✨ InaboxResources: Observe intersections for some elements' viewportCallbacks Feb 25, 2020
@alanorozco
Copy link
Copy Markdown
Member Author

Discussed offline with @dvoytenko and @jridgewell. We're special-casing some component types on InaboxResources to keep viewportCallback API the same.

@lannka @dvoytenko @jridgewell

PTAL, ready for review.

@alanorozco alanorozco marked this pull request as ready for review February 25, 2020 20:20
Copy link
Copy Markdown
Contributor

@lannka lannka left a comment

Choose a reason for hiding this comment

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

Thanks for the fix

@alanorozco alanorozco merged commit 39b4ad5 into ampproject:master Feb 26, 2020
@alanorozco alanorozco deleted the viewport branch February 26, 2020 18:48
robinvanopstal added a commit to jungvonmatt/amphtml that referenced this pull request Feb 27, 2020
* master: (54 commits)
  inabox-resources: Minor test improvement (ampproject#26916)
  DocInfo: replace metaTags with viewport in API (ampproject#26687)
  🐛 SwG now uses AMP sendBeacon interface (ampproject#26970)
  🏗 Allow array destructuring on preact hooks (ampproject#26901)
  Gulp Dep Check: fail on unused entries (ampproject#26981)
  Update no-import lint rule to forbid sub-paths (ampproject#26531)
  🐛 amp-ad type blade - fix bladeOnLoad callback (ampproject#26627)
  📖 Clarify when max-age is required (ampproject#26956)
  ♻️ Consolidate players as .i-amphtml-media-component (ampproject#26967)
  Add Preact Enzyme tests (ampproject#26529)
  Fixes `update_tests` flag on `gulp validator` (ampproject#26965)
  📦 Update dependency google-closure-library to v20200224 (ampproject#26986)
  🏗 Transform aliased configured components (ampproject#26541)
  ✨ InaboxResources: Observe intersections for some elements' viewportCallbacks (ampproject#26942)
  variable substitutions: Support allowlist lookup in AmpDocShadow (ampproject#26731)
  cl/297197875 Revision bump for ampproject#26877 (ampproject#26982)
  Json fix (ampproject#26971)
  📦 Update dependency mocha to v7.1.0 (ampproject#26976)
  Add documentation for amp-access-scroll (ampproject#26782)
  make controls always shown in amp for email (ampproject#25714)
  ...
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.

7 participants