MCPcopy
hub / github.com/mdn/web-components-examples / constructor

Method constructor

editable-list/main.js:5–63  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3(function() {
4 class EditableList extends HTMLElement {
5 constructor() {
6 // establish prototype chain
7 super();
8
9 // attaches shadow tree and returns shadow root reference
10 // https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow
11 const shadow = this.attachShadow({ mode: 'open' });
12
13 // creating a container for the editable-list component
14 const editableListContainer = document.createElement('div');
15
16 // get attribute values from getters
17 const title = this.title;
18 const addItemText = this.addItemText;
19 const listItems = this.items;
20
21 // adding a class to our container for the sake of clarity
22 editableListContainer.classList.add('editable-list');
23
24 // creating the inner HTML of the editable list element
25 editableListContainer.innerHTML = `
26 <style>
27 li, div > div {
28 display: flex;
29 align-items: center;
30 justify-content: space-between;
31 }
32
33 .icon {
34 background-color: #fff;
35 border: none;
36 cursor: pointer;
37 float: right;
38 font-size: 1.8rem;
39 }
40 </style>
41 <h3>${title}</h3>
42 <ul class="item-list">
43 ${listItems.map(item => `
44 <li>${item}
45 <button class="editable-list-remove-item icon">&ominus;</button>
46 </li>
47 `).join('')}
48 </ul>
49 <div>
50 <label>${addItemText}</label>
51 <input class="add-new-list-item-input" type="text">
52 <button class="editable-list-add-item icon">&oplus;</button>
53 </div>
54 `;
55
56 // binding methods
57 this.addListItem = this.addListItem.bind(this);
58 this.handleRemoveItemListeners = this.handleRemoveItemListeners.bind(this);
59 this.removeListItem = this.removeListItem.bind(this);
60
61 // appending the container to the shadow DOM
62 shadow.appendChild(editableListContainer);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected