Cookbook Home ~ alanwsmith.com ~ links ~ podcast ~ twitter

Web Components - Hello World

Deatils

Example

HTML

<hello-component></hello-component>

JavaScript

class HelloComponent extends HTMLElement {
    constructor() {
        super()

        this.attachShadow({ mode: 'open' })
        const wrapper = document.createElement('span')
        wrapper.innerText = 'Hello, Component!'

        this.shadowRoot.append(wrapper)
    }
}

customElements.define('hello-component', HelloComponent)

Notes

References