Web components

The Web Component API allows us to create custom elements that have defined behaviors utilizing only the browser APIs. This is different to a framework such as Bootstrap or even Vue as we are able to utilize all of the technologies that are present in our browser.

Chrome and Firefox have all of these APIs supported. Safari has most of them, and if this is a browser that we want to support, we will only be able to utilize some of the APIs. Edge does not have support for the Web Component APIs, but with it moving to a chromium base, we will see another browser able to utilize this technology.

Let's create a basic tooltip element. First, we need to extend the base HTMLElement in our class. We will then need to attach some properties to allow us to place the element and to give us the text that we need to use. Finally, we will need to register this component with our system to make sure it recognizes our custom element. The following code creates this custom element (modified from https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements):

class Tooltip extends HTMLElement {
constructor() {
super();
this.text = this.getAttribute('text');
this.type = this.getAttribute('type');
this.typeMap = new Map(Object.entries({
'success' : "&#x2714",
'error' : "&#x2716",
'info' : "&#x2755",
'default' : "&#x2709"
}));

this.shadow = this.attachShadow({mode : 'open'});
const container = document.createElement('span');
container.classList.add('wrapper');
container.classList.add('hidden');
const type = document.createElement('span');
type.id = 'icon';
const el = document.createElement('span');
el.id = 'main';
const style = document.createElement('style');
el.textContent = this.text;
type.innerHTML = this.getType(this.type);

style.innerText = `<left out>`
this.shadow.append(style);
this.shadow.append(container);
container.append(type);
contianer.append(el);
}
update() {
const x = this.getAttribute('x');
const y = this.getAttribute('y');
const type = this.getAttribute('type');
const text = this.getAttribute('text');
const show = this.getAttribute('show');
const wrapper = this.shadow.querySelector('.wrapper');
if( show === "true" ) {
wrapper.classList.remove('hidden');
} else {
wrapper.classList.add('hidden');
}
this.shadow.querySelector('#icon').innerHTML = this.getType(type);
this.shadow.querySelector('#main').innerText = text;
wrapper.style.left = `${x}px`;
wrapper.style.top = `${y}px`;
}
getType(type) {
return type ?
this.typeMap.has(type) ?
this.typeMap.get(type) :
this.typeMap.get('default') :
this.typeMap.get('default');
}
connectCallback() {
this.update(this);
}
attributeChangedCallback(name, oldValue, newValue) {
this.update(this);
}
static get observedAttributes() {
return ['x', 'y', 'type', 'text', 'show'];
}
}

customElements.define('our-tooltip', Tooltip);

First, we have a list of attributes that we are going to use to style and position our tooltip. They are called x, y, type, text, and show, respectively. Next, we create a map for some emoji-based text so that we can utilize icons without bringing in a full-fledged library for this. We then set up our reusable object inside a shadow container. We also put the shadow root on the object so we have easy access to it. The update method will fire on the first creation of our element and on any subsequent changes to our attributes. We can see this in the last three functions. connectedCallback will fire once we have been attached to the DOM. attributeChangedCallback will alert us to any attribute changes that have occurred. This is very similar to the Proxy API. The last piece lets our object know which attributes we specifically care about, in this case, x, y, type, text, and show. Finally, we register our custom component with the customElements.define method, giving it a name and the class that we want to run when one of these objects is created.

Now, if we create our tooltip, we can utilize these different properties to make a reusable system for tooltip or even alerts. The following code demonstrates this:

<our-tooltip show="true" x="100" y="100" icon="success" text="here is our tooltip"></our-tooltip>

We should see a floating box with a checkmark and the text Here is our tooltip. We can make this tooltip a little bit easier to read by utilizing the templating system that also comes with the Web Component API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.207.129