#CeasefireNow#FreePalestine

Skip to content Skip to footer
/ Mainasara's Blog

Web Components, for when VueJS is too much

How to make reactive/stateful components without a framework or library

These days when you want to make a website you just can't avoid the words "VueJS" or "ReactJS" and for very good reasons, these libraries make developing a website much easier thanks to their component-based architecture and how they handle data/properties and update the relevant parts of your site accordingly it's like magic!! ✨. But for times when I need a simple component or the element I want does not have dynamic data, I ask myself "Do I really need React/Vue for this? 🤔", well that is where web components come in. Web components are features (not the elements themselves) that help you do a lot of things, one of which is to create a custom element that can be used just like , and the rest. Let's start!. Step 1: Define our component One way to do this is by creating a class that implements the interface and give it a tag name by using the function. According to MDN. >The HTMLElement interface represents any HTML element. Some elements directly implement this interface, while others implement it via an interface that inherits it. _Notice that the component name is hyphenated, this is because we are not allowed to make a component called something like , the name needs to resemble or _ Now when we include in our HTML file we can use the component we've just created. And if we check the console we will see the message , That means our component is working fine. Step 2: Element lifecycle Just like in Vue there are lifecycle callbacks namely : this is called just after our element has been rendered. : this is called when our element is about to be removed. We now add a button to index.html which removes our element so we can test all the lifecycle callbacks. Now when we press the button our component is removed and we see the message in the console. Step 3: Let's make something Now that we have the basic knowledge on how to make a custom element let's use it!. A good example of this is a clock element. Warning!!!!, CODE BOMB INCOMING!!!!! 💣💣💣 Let me explain what is going on here, We have renamed the element to and registered it as There is now an interval id used to identify and eventually clear the interval declared in The method is used to add a 0 to numbers that are single digit, this makes the time look like when it would look like The method returns the appropriate suffix for the time based on the hour The method looks complicated but it actually is not, we just get the current hour, minute and second and convert it into a nice string showing the time in 12-hour format In the , we start a timer that sets the innerHTML of our element to the current time every 1000ms (1 second) In the we clear the timer. Now that we understand that code, let's add the element to our website. Step 4: Attributes Our clock looks good so far but it can be better, we will now make it display either 24-hour or 12-hour format based on an attribute of our choice. I personally like this syntax : > so we will aim at using the existence of the attribute as a boolean. If you pay attention to the new code added in you will see a very strange statement , this is there because when we set the attribute like this: we get the value of the attribute as which in javascript is equivalent to false, so will return false even if the attribute exists Now we can now choose to display either in 12-hour or 24-hour format by adding an attribute !! Step 5: Reactive state Our element currently does not change state in runtime even if our attribute has, that looks like it can be improved upon. So we will now make the element reactive to attribute changes. To do this we use a , this helps us watch for any changes to our element. A good place to put this is in the element constructor. The constructor returns a MutationObserver that invokes a specified callback whenever there are changes to our element. We assign the observer to instead of because we need to clean up the listener in our . When the attribute changes we need to display the accurate time format, and for that, we also need to change to so we can access the variable from the MutationObserver. We are done 🎉🎉🎉🎉🎉🎉🎉🎉 Not only have we just made our custom element but we made it react dynamically to changes. This only scratches the surface of what web components can do and I can't wait to see the great things you guys will use it for. Once again this is not a replacement for VueJS (Or it's counterparts), it is only an alternative for when Vue is overkill Thanks For Reading!!