Web Components Named Slots
- Web Components Named Slots Machines
- Web Components Named Slots Games
- Web Components Named Slots Real Money
Using slots means that I would be doing just passing HTML and I don't think there is an easy way to iterate through HTML list items. Component I can keep on using components but now I have to convert my old list to an object and add some conditionals to manage to prepare the component for handling HTML formating. Slot elements with a name attribute are called named slots, but this attribute is not required. It is merely used to render content in a specific place. When one or more slots do not have a name attribute, content will just be rendered inside them in the order in which it is provided by the user. A web developer provides a tutorial on how to perform content projection in an Angular element using the latest version of the framework, Angular 7.0.
Now that we went over some of the basic concepts of Web Components and Custom Elements, let’s push our exploration a little bit further and discuss attributes and properties.
This post discusses the Custom Elements V1 spec.
We’ll start with a few important concepts about attributes and properties in the DOM:
Properties vs Attributes
The difference between properties and attributes can be confusing. Properties are available on a DOM node when being manipulated by JavaScript:
And attributes are provided in the HTML itself. Here alt, width and height are all attributes:
Attributes should only be used for scalar values like strings, numbers and boolean values. Properties, on the other hand, are perfectly suited to also hold values that are objects or arrays.
Reflecting Properties to Attributes
Most properties reflect their values as attributes, meaning that if the property is changed using JavaScript, the corresponding attribute is also changed at the same time to reflect the new value. This is useful for accessibility and to allow CSS selectors to work as intended.
You can try it out yourself for a concrete example. Just select, say, an image element in your browser’s developer tools, and then change one of its properties:
Notice how the with attribute in the DOM representation is automatically changed to the new value. The same is true if you change the value for the attribute manually in the DOM inspector, you’ll see that the property will now hold the new value.
Reflecting properties to attributes in Custom Elements
Your own Custom Elements should also follow this practice of reflecting properties to attributes. Luckily, it’s quite easy to do using getters and setters.
For example, if you have a custom element that has a value property that should be reflected as an attribute, here’s how you would use a getter and a setter to get the value of the attribute when doing property access and setting the new value for the attribute when the property is changed:
Or, if you have a boolean property, like, say hidden:
Listening for Changed Attributes
With Custom Elements, you can listen for attribute changes using the attributeChangedCallback method. This makes it easy to trigger actions when attributes are changed. To help with performance, only attributes defined with an observedAttributes getter that returns an array of observed attribute names will be observed.
The attributeChangedCallback is defined with three parameters, the name of the attribute, the old value and the new value. In this example, we observe the value and max attributes:
Web Components Named Slots Machines
Notice also that the observedAttributes getter is a static method on the class. Static methods are often used as utility methods for the class itself because they are unavailable on class instances.
Web Components Named Slots Games
Putting it All Together
Let’s put all these concepts together by building a simple counter element, similar to the one that we build using Stencil.
Our component can be used like this:
Or it can be used with the following attributes:
Here’s the full code for our custom element, with a few interesting parts highlighted:
my-counter.js
Most of the code is pretty straight-forward and uses concepts that we discussed in this article. The highlighted parts may be new however, so here are some quick explanations:
- In the constructor we bind the this for our increment and decrement methods to the this of the class itself. Otherwise, since these two methods are used as callbacks for event handlers on button elements, the this would be the clicked button instead of the Custom Element.
- In the connectedCallback method we set the value attribute to an initial value of 1 if value hasn’t been set by the user of the element.
- We remove our event listeners in the class’ disconnectedCallback method.