Webcomponents Cheatsheet 2021
Webcomponents Cheatsheet 2021
WEB COMPONENTS
Created by @Manz ( https://twitter.com/Manz ) https://lenguajejs.com/
<template> inert tag for html content ES <slot> external html to inside component
<template shadowroot="open"> <slot>��slot>
#DOCUMENT-FRAGMENT declarative
shadow dom C Custom Elements HTML Components NAMED SLOT
<div>...��div> <slot name="text"> multiple html to inside
HTML STRUCTURE
��template>
<prefix-component> custom html tag <slot name="title">��slot>
API TEMPLATE HTMLTemplateElement
SR .shadowRoot shadow dom root node <div>content��div> SHADOW DOM ::slotted(selector) style to slotted tags
<div>content��div> LIGHT DOM EVENTS HTMLTemplateElement
D DOM HTML Access DOM manipulation ��base-name> slotchange detect slot-element changes
API HTML CLASS API JS PROPERTIES & METHODS <base-name>
s .innerHTML get/replace HTML markup o constructor() initial method super() <h2 slot="title">Default��h2>
s .outerHTML idem (includes HTML tag) public() public properties/methods <p slot="description">Text��p>
s .textContent get/replace text content #private() private properties/methods ��base-name>
MULTIPLE SLOT SIMPLE SLOT
element.innerHTML = `<div>text��div>`; class BaseName extends HTMLElement {
CREATE HTML / ADD ELEMENT ... C CSS in WebComponents Styles (CSS)
e document .createElement(tag) create node } CSS WITHOUT SHADOW DOM GLOBAL CSS
customElements.define("base-name",BaseName) connectedCallback() {
document.createElement("div"); COMPONENT LIFECYCLE HOOKS WHEN? this.innerHTML = `
connectedCallback() added to DOM <style>p { color: red; }��style>
o .appendChild(child) add inside element `;
disconnectedCallback() remove from DOM
element.appendChild(node); adoptedCallback() move to new doc. }
CSS WITH SHADOW DOM LOCAL CSS
INSERT HTML / ELEMENT class BaseName extends HTMLElement {
constructor() { connectedCallback() {
.insertAdjacentHTML(pos, html) add super(); this.shadowRoot.innerHTML = `
el.insertAdjacentHTML("afterend", ... don't forget <style>p { color: red; }��style>
} super() on `;
`<div><p>Content��p>��div>`); constructor
connectedCallback() { ... } }
disconnectedCallback() { ... }
.insertAdjacentElement(pos, node) CSS CONTAINER
adoptedCallback() { ... }
el.insertAdjacentElement("afterend", node); } :host style custom element (container)
customElements.define("base-name", BaseName) :host(selector) idem, if match container
beforebegin afterend ATTRIBUTE OBSERVATION :host-context(selector) idem, ancestor
html tag
<div> text ��div> position to a get observedAttributes() notify changes CSS PARTS
insert
attributeChangedCallback(attr, old, now) <span part="name"> define part
afterbegin beforeend
class BaseName extends HTMLElement {
<span part="name">��span>
F Find HTML Elements DOM search static get observedAttributes() {
return ["name1", "name2"]; ::part(selector) style surface parts
TRADITIONAL DOM SEARCH API
}
o document .getElementById(id)find by #id attributeChangedCallback(name, old, now){ S Shadow DOM .shadowRoot (DOM isolate)
a .getElementsByName(name) name attr ... fire this callback when a WEBCOMPONENT WITHOUT SHADOW DOM
} observed attribute changes
a .getElementsByClassName(class) .class } class BaseName extends HTMLElement {
a .getElementsByTagName(tag) html tag customElements.define("base-name", BaseName) constructor() {
document.getElementById("name"); CUSTOM ELEMENTS REGISTRY GLOBAL REGISTRY super();
this.innerHTML = `<div>��div>`;
MODERN DOM SEARCH API customElements .define(name, class) reg
}
o customElements .get(name) register elem
o .querySelector(selector) return first elem }
customElements .upgrade(node) update el.
a .querySelectorAll(selector) ret all elems WEBCOMPONENT WITH SHADOW DOM
p customElements .whenDefined(name) fire.
o .closest(selector) return closest ancestor s .attachShadow(options) add shadow dom
b .matches(selector) matches with elem? C Custom Events Send/Receive events SHADOW DOM OPTIONS
document.querySelector(".menu > p"); CUSTOM EVENTS API
s mode encapsulation mode open closed
HTML ATTRIBUTE API b .dispatchEvent(event) send event b delegatesFocus shadow get focus false
b .hasAttributes() element w/attributes? CUSTOM EVENT OBJECT
class BaseName extends HTMLElement {
s .getAttributeNames() return attrs array o .detail data object with information constructor() {
b .hasAttribute(name) check attribute b .bubbles bubbles up through the DOM super();
s .getAttribute(name) return value attr b .composed send across shadow DOM this.attachShadow({ mode: "open" });
.removeAttribute(name) delete attribute this.shadowRoot.innerHTML = `<p>��p>`;
const event = new CustomEvent("message", {
}
.setAttribute(name, value) modify attr detail: { ... },
}
b .toggleAttribute(name, force) add/del bubbles: true,
composed: true
element.setAttribute("name", "value"); });
CHEAT SHEET
LIT-ELEMENT/LIT-HTML lit-html
Created by @Manz ( https://twitter.com/Manz ) https://lenguajejs.com/
T Templates Next-gen lit-html templates C Component Lit-Element web component P Properties Properties != Attributes
SETUP & CONFIGURATION SETUP & CONFIGURATION PROPERTIES DECLARATION SYNTAX
https://unpkg.com/lit-element?module https://cdn.skypack.dev/lit-element
https://unpkg.com/lit-html/directives/class-map.js?module https://cdn.skypack.dev/lit-html/directives/class-map.js