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 <amp-story-embed> AMP extension #25006

Closed
wants to merge 3 commits into from

Conversation

newmuis
Copy link
Contributor

@newmuis newmuis commented Oct 10, 2019

This adds an AMP component that has the same API/behavior as the non-AMP component, for embedding AMP stories.

As a side note, visual diff tests don't work for anything in shadow DOM, so they're disabled for now.

This also puts the AMP and non-AMP variants behind an experiment.

constructor(element) {
super(element);

this.embedManager_ = new AmpStoryEmbedManager(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is safer to have components do 0 work in the constructor. Is there any downside to moving this to buildCallback?
If for some reason we need to instantiate the AmpStoryEmbedManager before the buildCallback, maybe we could use firstAttachedCallback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, I will change that. Just wondering: why do you think this is safer?

Copy link
Contributor

Choose a reason for hiding this comment

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

Doing some work in the constructor of an element will run it as soon as the element is attached to the DOM, very likely during rendering.
Because the work is "hidden" in the other constructor of the EmbedManager, it would be easy to introduce latency or privacy issues by mistake, just because it is hard to realize when the code actually runs if you don't have context on the whole thing.

@@ -683,6 +683,26 @@
".i-amphtml-story-loaded",
],
},
{
"flaky": true, // Skip this until shadow DOM the embeds support shadow DOM
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I understand why the visual diff tests aren't working since we already support components in the shadow DOM like the bookend?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the core stories framework, we're actually not testing the shadow DOM code. When we create new shadow roots, we use a utility; that utility simply does not create a shadow root in test code.

export function createShadowRootWithStyle(container, element, css) {
const style = self.document.createElement('style');
style./*OK*/ textContent = css;
const containerToUse = getMode().test
? container
: createShadowRoot(container);
containerToUse.appendChild(style);
containerToUse.appendChild(element);
}

Since this code doesn't live in the amp-story extension, we don't use that util. We can use the same high-level approach, but to do so, we have to rewrite a bunch of things (all the styles break), so I figured it would be better to do as a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it, thanks!

@newmuis
Copy link
Contributor Author

newmuis commented Oct 18, 2019

Ping @jridgewell for OWNERS approval

/** @private {!Array<!HTMLAnchorElement>} */
this.stories_ = Array.prototype.slice.call(element.querySelectorAll('a'));
/** @private {?Array<!HTMLAnchorElement>} */
this.stories_;
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to set these to null. As is, these don't actually create the field on the object, which causes polymorphism.

return;
}

this.stories_ = Array.prototype.slice.call(
Copy link
Contributor

Choose a reason for hiding this comment

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

We have a helper for this in src/array.js, I think.

const embedImpl = new AmpStoryEmbed(doc, embed);
embedImpl.build();
if (!isAMP) {
const doc = self.document;
Copy link
Contributor

Choose a reason for hiding this comment

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

This gets run very early in the page lifecycle, there's no guarantee that you'll find the amp-story-embed element.

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

Successfully merging this pull request may close these issues.

None yet

5 participants