When should there be a declarative version of a JavaScript API?

I feel like it’s high time I revived some interest in my proposal for button type=”share”. Last I left it, I was gathering use cases and they seem to suggest that the most common use case for the Web Share API is sharing the URL of the current page.


This content originally appeared on Adactio: Journal and was authored by Adactio: Journal

I feel like it’s high time I revived some interest in my proposal for button type="share". Last I left it, I was gathering use cases and they seem to suggest that the most common use case for the Web Share API is sharing the URL of the current page.

If you want to catch up on the history of this proposal, here’s what I’ve previously written:

Remember, my proposal isn’t to replace the JavaScript API, it’s to complement it with a declarative option. The declarative option doesn’t need to be as fully featured as the JavaScript API, but it should be able to cover the majority use case. I think this should hold true of most APIs.

A good example is the Constraint Validation API. For the most common use cases, the required attribute and input types like “email”, “url”, and “number” have you covered. If you need more power, reach for the JavaScript API.

A bad example is the Geolocation API. The most common use case is getting the user’s current location. But there’s no input type="geolocation" (or button type="geolocation"). Your only choice is to use JavaScript. It feels heavy-handed.

I recently got an email from Taylor Hunt who has come up with a good litmus test for JavaScript APIs that should have a complementary declarative option:

I’ve been thinking about how a lot of recently-proposed APIs end up having to deal with what Chrome devrel’s been calling the “user gesture/activation budget”, and wondering if that’s a good indicator of when something should have been HTML in the first place.

I think he’s onto something here!

Think about any API that requires a user gesture. Often the documentation or demo literally shows you how to generate a button in JavaScript in order to add an event handler to it in order to use the API. Surely that’s an indication that a new button type could be minted?

The Web Share API is a classic example. You can’t invoke the API after an event like the page loading. You have to invoke the API after a user-initiated event like, oh, I don’t know …clicking on a button!

The Fullscreen API has the same restriction. You can’t make the browser go fullscreen unless you’re responding to user gesture, like a click. So why not have button type="fullscreen" in HTML to encapsulate that? And again, the fallback in non-supporting browsers is predictable—it behaves like a regular button—so this is trivial to polyfill. I should probably whip up a polyfill to demonstrate this.

I can’t find a list of all the JavaScript APIs that require a user gesture, but I know there’s more that I’m just not thinking of. I’d love to see if they’d all fit this pattern of being candidates for a new button type value.

The only potential flaw in this thinking is that some APIs that require a user gesture might also require a secure context (either being served over HTTPS or localhost). But as far as I know, HTML has never had the concept of features being restricted by context. An element is either supported or it isn’t.

That said, there is some prior art here. If you use input type="password" in a non-secure context—like a page being served over HTTP—the browser updates the interface to provide scary warnings. Perhaps browsers could do something similar for any new button types that complement secure-context JavaScript APIs.


This content originally appeared on Adactio: Journal and was authored by Adactio: Journal


Print Share Comment Cite Upload Translate Updates
APA

Adactio: Journal | Sciencx (2022-03-09T17:42:33+00:00) When should there be a declarative version of a JavaScript API?. Retrieved from https://www.scien.cx/2022/03/09/when-should-there-be-a-declarative-version-of-a-javascript-api/

MLA
" » When should there be a declarative version of a JavaScript API?." Adactio: Journal | Sciencx - Wednesday March 9, 2022, https://www.scien.cx/2022/03/09/when-should-there-be-a-declarative-version-of-a-javascript-api/
HARVARD
Adactio: Journal | Sciencx Wednesday March 9, 2022 » When should there be a declarative version of a JavaScript API?., viewed ,<https://www.scien.cx/2022/03/09/when-should-there-be-a-declarative-version-of-a-javascript-api/>
VANCOUVER
Adactio: Journal | Sciencx - » When should there be a declarative version of a JavaScript API?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/09/when-should-there-be-a-declarative-version-of-a-javascript-api/
CHICAGO
" » When should there be a declarative version of a JavaScript API?." Adactio: Journal | Sciencx - Accessed . https://www.scien.cx/2022/03/09/when-should-there-be-a-declarative-version-of-a-javascript-api/
IEEE
" » When should there be a declarative version of a JavaScript API?." Adactio: Journal | Sciencx [Online]. Available: https://www.scien.cx/2022/03/09/when-should-there-be-a-declarative-version-of-a-javascript-api/. [Accessed: ]
rf:citation
» When should there be a declarative version of a JavaScript API? | Adactio: Journal | Sciencx | https://www.scien.cx/2022/03/09/when-should-there-be-a-declarative-version-of-a-javascript-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.