Skip to main content
View on YouTube to comment and like!

Syntax Video

Invokers Explained - The new HTML API that to trigger events without JavaScript

Chapter 1 00:00

Transcript

do you hate having to use JavaScript for simple actions like opening a dialogue or a modal well this new HTML API is going to blow your mind because coming very soon is a new API called invokers and if you haven't heard of invokers there are a couple of attributes that you can throw on HTML you have the command for attribute and the command attribute the command is what command you are running in this case it is show modal and the command four is the ID of the item of what you're trying to run that action on now invokers are actually coming very soon to the web platform in fact they just kind of showed up under a flag in every single major browser that's right these are behind a flag in both the technical preview of safari Firefox as well as Chrome meaning that these things are definitely coming so what can you do with invokers Beyond just opening up a dialogue well in its

current current state you'll be able to do things like show modal or close which will affect a dialogue element show popover hide popover for using the popover API without JavaScript there's even a toggle popover if you want to toggle it so you don't have to implement your own toggle by showing and hiding you just can use the the toggle command for you can even use custom values which I'll show you in just a second and while these are the initial invokers to arrive in the browser there is a hope that that will'll see things like show picker play pause for audio and video elements um doing things like toggling opening and closing a details element as well all the types of basic things that you'd think you'd want to be able to have more control over within HTML itself you'll be able to do with command and command 4 and hopefully many of these things eventually as well so if we want to see this in action I basically just have a button right here where I have again my

command 4 is attached to the ID of this particular dialogue element and then the command is simply show modal when I click this you can see without JavaScript it pops open and this functions just like a dialogue would if you've opened it with JavaScript I still get that keyboard capture I still get that Escape key to close it and you can see I've also added this step up and step down now you see mine is actually working here uh it says that I have a command for step down on the input type of number where the ID of number where the command 4 is numb so I'm attaching a button with a command of either step up or step down and you can see without JavaScript I'm able to increment this number now why does this work well it works because I have my invokers turned on behind a flag in Chrome and apparently between the last time I used this and this time the invokers now have the ability to use some of these additional ones like step up and step down if you want to give these things a try today at least for the basic ones

without a flag turned on there is is a really really good polyfill available here I'll make sure that Link's available in the comments below but you can drop this polyfill in and at least take advantage of the dialogue and popover based command that you see here and maybe you'll have to wait on some of these or if these plan to get implemented maybe they'll drop that functionality into the polyfill as well now we've talked about all of the basic invokers that are going to be coming in the version one of this with shipping initially but let's actually talk about creating our own custom invoker because you can do that too but you will have to reach for JavaScript you want to write your own custom invokers it's incredible it's really easy to do you just have a command as you can see I have hyphen hyen shrink grow reset see how I have that double hyphen there and then the command 4 is all targeting the exact same thing which was the image itself you can see the command 4 again is the

my image of the ID my image now to write the custom voer all you have to do is add an event listener to the image now typically what you'd approach in this situation is you would add an event listener to the buttons right but in this particular case I'm adding an event listener to the image and I'm listening for a command now when that command happens and event fires off I can check the event command and check to see what it's equal to if it's one of my custom commands for shrink I'm just adjusting a number value here which is size I'm decreasing it and then I'm going to set the CSS property of scale to that size so you can see when I click shrink here it gets smaller by half same with grow doubles or go grows up by half a value and then reset will take us back to that one initially as we go so this is all pretty sick in fact this is amazing because you're dropping this in and you're essentially commanding all of this with HTML now I do think that the

coolest part about this API is not having to write JavaScript so I'm much more excited for all of the built-in invokers especially the ones to to toggle and control dialogues popovers media elements those types of things but being able to add your own custom ones is I'm sure it's going to invent a whole lot of interesting new patterns as well being able to control things from HTML with these two sick new attributes now if you want to learn about invokers there's a couple of awesome places to go open UI has a great blog post on these things it's well worth your read as well as mdn of course has the amazing documentation and voker command API it talks about all of the different things you can do it talks about uh the JavaScript interfaces and extensions how you can actually use this thing if you'd like to use it with JavaScript beyond the default ones but this is well worth your time and since these things are already behind a flag in either the preview versions or the actual versions of all of the three major browsers that indicates to me that this is something you're going to want to pick up sooner

rather than later or at least be aware of that this technique is coming to HTML and granted we have a polyfill for these basic ones and in my experience that polyfill works pretty darn well so if you want to give this a try give it a try head on over to my website I'll have a link to the two demos that I did here if you just want to click around and give this a try and I'll also link to that polyfill so you can learn more either way let me know what you think about invokers are you stoked for these things I'm I'm very excited uh less JavaScript I have to write is the best JavaScript I have to write so as always I'll see you in the next one

Quote it with credit and a link. Quoting Syntax