Navigating here in SOpt found the following question: How to add customElements support for Opera 12 ? , and would like to know how useful this property is.
Note that by doing a simple search here, to date there is only this question quoted.
Navigating here in SOpt found the following question: How to add customElements support for Opera 12 ? , and would like to know how useful this property is.
Note that by doing a simple search here, to date there is only this question quoted.
Simply put, customElements
means custom elements . This means that you can create your own element, just as you can use <a> <br> <div>
elements.
In order to use this property, the browser needs to implement the Custom Elements v1 specification. It also specifies the rules that developers need to follow to implement their custom elements.
To start understanding your usage, I suggest using the Google tutorials , much easier to understand than the specification.
For example:
class WmSouza extends HTMLElement {
connectedCallback() {
this.appendChild(document.createTextNode(" especial para wmsouza
<file-reader>
and <skype-login>
are valid names for custom elements, while <skype_login>
and <skypelogin>
are not valid names. This is required to allow the HTML parser to differentiate a custom element and an embedded HTML element. DOMException
error will be thrown if you do this. <skype-login />
. It should always be written like this: <skype-login></skype-login>
. A custom element can be created using the customElements.define()
method of the browser API and a class that extends HTMLElement
in JavaScript, like this:
class ErickPrates extends HTMLElement {
// Definir comportamento aqui
}
window.customElements.define('Erick-Prates', ErickPrates);
Another option is to use an anonymous class like this:
window.customElements.define('Erick-Prates', class extends HTMLElement {
// Definir comportamento aqui
});
With this already defined, you can now use the custom element on a web page, like this:
<Erick-Prates></Erick-Prates>
You can set properties in a CustomElement. For example, we'll add an attribute called open
and our <Erick-Prates>
element. This can be achieved like this:
class ErickPrates extends HTMLElement {
// Defina a propriedade "open"
set open(option) {
this.setAttribute("open", option);
}
// Obter a propriedade "open"
get open() {
return this.hasAttribute("open");
}
}
This refers to the DOM element itself. So in this example, this refers to
<Erick-Prates>
.
Once you have done this, you can now use the custom element in your browser like this:
<Erick-Prates open="true"></Erick-Prates>
You can also define a constructor in the class, but you should call the super()
method before adding any other code.
There are lifecycle hooks that custom elements can define during their lifetime. These hooks are:
You can reference the custom element specification for more information.