Skip to content

Export imperative API from carousel#30046

Merged
dvoytenko merged 2 commits into
ampproject:masterfrom
dvoytenko:bento/carousel-imperative-api
Aug 31, 2020
Merged

Export imperative API from carousel#30046
dvoytenko merged 2 commits into
ampproject:masterfrom
dvoytenko:bento/carousel-imperative-api

Conversation

@dvoytenko
Copy link
Copy Markdown
Contributor

@dvoytenko dvoytenko commented Aug 28, 2020

This pull request uses Preact's useImperativeHandle to export the component's imperative API to the AMP layer. The most common use for this is support of AMP actions. This pull request implements next, prev, and goToSlide actions. The imperative API itself is declared and described in the type.js file.

Partial for: #28284
Related to: ampproject/wg-bento#36

Comment thread extensions/amp-base-carousel/1.0/test/test-amp-base-carousel.js Outdated
Comment thread extensions/amp-base-carousel/1.0/base-carousel.js
Copy link
Copy Markdown
Member

@samouri samouri left a comment

Choose a reason for hiding this comment

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

I'm new to useImperativeHandle, and this seems like a clean pattern for exposing APIs from a component! One request: can you add small description for this PR?

Left some non-blocking comments. Only major item is the .only Caroline found.

Comment thread extensions/amp-base-carousel/1.0/test/test-amp-base-carousel.js
* @param {../action-constants.ActionTrust} minTrust
* @protected
*/
registerApiAction(alias, handler, minTrust) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

would we make this a no-op in the bento mode envelope?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. We still need to allow action execution. E.g. a caller should be able to call carousel.goToSlide(0) no matter what mode we are in.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this mean we would want a Bento version of amp-state? I don't recall having discussions about this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No need for Bento amp-state. But the on-actions are not part of amp-state - that's just normal imperative API for an element. Just like video.pause() - there has to be a way to pause a video programmatically. Otherwise the usability of an element is significantly reduced. This is also usually not a big deal to support - it'd most often be a simple passthrough element.goToSlide -> api(component).goToSlide.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To clarify, amp-state might use element on-actions, but these actions exist independent of amp-state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

And one more clarification. I also don't expect Bento mode to use on="tap:carousel.goToSlide" type of thing. But somehow the element needs to export an API to make it possible still or the element's API surface will be incomplete. Currently it's possible with element.enqueAction('goToSlide') - a really complicated way to do it, but sufficient for testing. We will be looking into how we can make these API calls more ergonomic in Bento world.

Copy link
Copy Markdown
Member

@samouri samouri Aug 31, 2020

Choose a reason for hiding this comment

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

API calls more ergonomic in Bento world

This PR sets the stage for it! One potential option would be to forward all keys within the .getApi() object to the element, so the caller could write element.goToSlide

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for clarifying, this all makes sense and I think the imported independently was groggy Monday brain. Feel free to disregard.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think forwarding is exactly what we should do. But Not yet 100% certain it'd be that simple. Another option is to simply export api property on the element. E.g. users would call element.api.goToSlide(). That has two benefits:

  1. We could give .api more clear async semantics. It's not available until the element is upgraded.
  2. We don't ever have to worry about method collision with any DOM APIs - it's basically functions as a namespace.

But it has one big downside: per Preact semantics the imperative handle can easily change, which is not something a Bento user would always expect. element.api@time1 !== element.api@time2.

Thus, I think, some sort of proxying is a more likely solution for us. This is something to fully design and TODO, but not subject for this PR specifically since it's only taking care of the element->preact bridge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I created ampproject/wg-bento#36 to track the element API question.

Comment thread src/preact/base-element.js
Comment thread extensions/amp-base-carousel/1.0/base-carousel.js
Comment thread extensions/amp-base-carousel/1.0/base-carousel.js
@dvoytenko dvoytenko merged commit 3aa7b8c into ampproject:master Aug 31, 2020
@dvoytenko dvoytenko deleted the bento/carousel-imperative-api branch August 31, 2020 18:30
ed-bird pushed a commit to ed-bird/amphtml that referenced this pull request Dec 10, 2020
* Export imperative API from carousel

* review fixes
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.

4 participants