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

Support display observer in the lightbox #32701

Merged
merged 9 commits into from
Feb 22, 2021

Conversation

dvoytenko
Copy link
Contributor

@dvoytenko dvoytenko commented Feb 17, 2021

Partial for #31540.

See explainer here.

@dvoytenko dvoytenko marked this pull request as ready for review February 17, 2021 22:34
src/utils/display-observer.js Show resolved Hide resolved
src/utils/display-observer.js Outdated Show resolved Hide resolved
src/utils/display-observer.js Outdated Show resolved Hide resolved
src/utils/display-observer.js Outdated Show resolved Hide resolved
src/utils/display-observer.js Show resolved Hide resolved
src/utils/display-observer.js Outdated Show resolved Hide resolved
src/utils/display-observer.js Show resolved Hide resolved
src/utils/display-observer.js Show resolved Hide resolved
@dvoytenko
Copy link
Contributor Author

@jridgewell I refactored the code per your recommendation to combine container and observer into one structure. It does look cleaner that way. PTAL.

src/utils/display-observer.js Show resolved Hide resolved
src/utils/display-observer.js Show resolved Hide resolved
src/utils/display-observer.js Show resolved Hide resolved
src/utils/display-observer.js Show resolved Hide resolved
* @param {!Element} container
* @private
*/
containerObserved_(isDisplayed, container) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure this optimization is necessary? I imagine destroying/creating the InObs is actually more expensive than having an initialized one that's not observing anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, not sure about this, tbh. Recreating InOb would definitely have its cost. The issue I'm having: the intersection observer on the container may very well see the intersections - it'd do so unless the container or the observed child are truly set display:none. However, from the point of view of the document, the container itself as a whole might be not-displayed. So, I'd then need to override the container observations with false somehow. So, what I do here instead is disconnect the observer and instead overwrite all values to false, knowing that the observer cannot flip them back to true until it's recreated. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

Does the container's InOb not alert all the targets that they are not visible anymore? I had assumed we could make this function just a noop, but if more is required then it makes sense to disconnect as display changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not always. It's possible to have a situation where the container's InOb considers a target displayable, but the container itself is not. This is because InOb doesn't have a concept of "displayable" - it can only answer the question of whether a target "intersects" the container.

src/utils/display-observer.js Show resolved Hide resolved
src/utils/display-observer.js Show resolved Hide resolved
@dvoytenko
Copy link
Contributor Author

@jridgewell Responded. PTAL.

*/
setObservation_(target, index, value, callbacks) {
let observations = this.targetObservations_.get(target);
if (!observations) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this only happens during observe(target, cb), right? If so, we could just move that there and simplify setObservation_.

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 think so. But this still feels a bit safer since the map.get() by type returns a X|undefined value.

* @param {!Element} container
* @private
*/
containerObserved_(isDisplayed, container) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the container's InOb not alert all the targets that they are not visible anymore? I had assumed we could make this function just a noop, but if more is required then it makes sense to disconnect as display changes.

@dvoytenko dvoytenko merged commit 6549bb8 into ampproject:master Feb 22, 2021
@dvoytenko dvoytenko deleted the run3/nested-display branch February 22, 2021 17:49
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.

3 participants