Web Components Custom Elements

Source: Internet
Author: User

What is Web Component?

Web components contain a variety of different technologies. You can think of Web Components as a generic term for reusable user interface parts created with a series of web technologies. WEB components gives developers the ability to extend browser tagging, allowing them to customize the component freely. But as of this time, WEB components remain a draft of the working Group and are formally incorporated into the standard, but that does not prevent us from learning it.

Web Components

What is a Web component? Web Components are not unfamiliar to web developers, and Web Components are a packaged set of HTML,CSS, as well as JavaScript, the most important feature of which is reusability. Basically on each site we can see a variety of components, such as drop-down menus, buttons, picture scrolling, calendar controls and so on. Wait, since we can already implement the encapsulation of Web Components, what is the meaning of this guy in Web Component? Web component replied: "You are not elegant enough to achieve the perfect, or look at my bar."

Because when we encapsulate a component with a variety of programming techniques, one cannot evade the fact that the internals of the component can be accessed and affected, for example, when we make changes to the stylesheet, we often worry about the style that affects the page component. And through the Web Component package, we can choose to let the interior of the component be hidden, that is to say, the inside of the component is isolated!

The composition of Web component
    • Custom Elements
    • HTML Templates
    • Shadow Dom
    • HTML Imports

In general, the Web Components contain the above four technologies, this article focuses on the custom Element, which is also the most important part of Web component.

Custom Elements

A custom element is an object that can be created by a creator to customize an interface. We need to register the custom element with document.registerelement () at the time of creation. The method returns the constructor of an element through which we can create an instance of our custom element.

var mybutton= document.registerelement (' My-button ');d ocument.body.appendChild (new MyButton ());

In fact Document.registerelement (Tag-name, prototype) contains two parameters:

tag-name: The label name of the custom element, the tag name must contain a hyphen '-', and the parent is the element used to differentiate between the custom element and the HTML specification

Prototype: This is an optional parameter that describes the prototype of the element in which you can define the interface of the custom element

var MyElement = document.registerelement (' my-element ', {   prototype:Object.create (htmlelement.prototype, {     Createdcallback: {       function() {         this. InnerHTML = "<p>i ' m a Custom Element</p> "; }}    );d ocument.body.appendChild (new myelement ()))

In the example above, we created an object that inherits from HtmlElement as a prototype of the custom object through the Object.create () method, and set the element default InnerHTML , if you are Object.create ( The second parameter of the method is unfamiliar, you'd better look it up first. In fact, the above example has the same effect as the one given below:

var Myelementproto =function() {    this. InnerHTML = "<p>i ' m a Custom Element </p> "}var myelement = document.registerelement (' my-element ', {Prototype:myelementproto }) Document.body.appendChild (new myelement ())

Next, on the page we can see that the following structure is rendered:

Customizing the life cycle of an element

In the example above we can see that the custom element has a Createdcallback property on its prototype, whose value is a callback function that is called when the custom element is created. In fact, a custom element may undergo the following changes in its life cycle:

    • Custom elements are created before they are registered
    • Custom element is registered
    • Instances of custom elements are created after the custom element is registered
    • The custom element is inserted into the document
    • To remove a custom element from the document
    • Properties of custom elements are created, removed, modified

Different callback functions are called when the custom element undergoes some of the above changes. These callback functions are stored in a set of key-value pairs called life cycle callbacks . The total number of callbacks we can implement is 4, where we can access the property name of the operation, the old property value, and the new property value through its parameters in the attributechangedcallback callback function.

Dom:

<DivID= "Modify">  <labelclass= "Cegreen"><inputtype= "Radio"name= "Ceclass"value= "Green">Green Box</label>  <labelclass= "Cered"><inputtype= "Radio"name= "Ceclass"value= "Red">Red box</label></Div>

Js:

varMyElement = document.registerelement (' my-element '), {prototype:Object.create (Htmlelement.prototype, {createdcallback: {value:function() {         This. InnerHTML = "<span>i ' m a Custom element</span>"}}, Attributechangedcallback: {value:function(property, OldValue, NewValue) { This. InnerHTML = "attribute" + Property + "are modified to" +NewValue}} })}) Document.body.appendChild (Newmyelement ())vartemp = Document.queryselector ("#modify")varMyElement = Document.queryselector ("My-element") Temp.addeventlistener (' Click ',function(e) {console.log (e.target.value) myelement.classname=E.target.value})

Also, adding a style to a custom element is the same as a normal element, which is the style added to the custom element in the example above:

{display: inline-block; margin-top: 20px; padding: 10px; font-size: 24px;}

This is the implementation of a most basic custom element. If we want our custom elements to be unaffected by external styles, we need to use the shadow DOM to encapsulate the internal DOM structure and style.

Web Components Custom Elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.