| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en-au"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<meta name="source" content="https://mdn.github.io/web-components-examples/life-cycle-callbacks/"> |
| 6 |
<title>Life cycle callbacks test</title> |
| 7 |
<style> |
| 8 |
custom-square { |
| 9 |
margin: 20px; |
| 10 |
} |
| 11 |
</style> |
| 12 |
</head> |
| 13 |
<body> |
| 14 |
<h1>Elements</h1> |
| 15 |
|
| 16 |
<span> |
| 17 |
<button class="add">Add custom-square to DOM</button> |
| 18 |
<button class="update">Update attributes</button> |
| 19 |
<button class="remove">Remove custom-square from DOM</button> |
| 20 |
</span> |
| 21 |
|
| 22 |
<iframe src="iframe.html" height="200" width="300" name="iframe"></iframe> |
| 23 |
<iframe src="iframe.html" height="200" width="300" name="iframe2"></iframe> |
| 24 |
<script> |
| 25 |
<!-- |
| 26 |
// Create a class for the element |
| 27 |
|
| 28 |
class Square extends HTMLElement { |
| 29 |
// Specify observed attributes so that |
| 30 |
// attributeChangedCallback will work |
| 31 |
static get observedAttributes() { |
| 32 |
return ['c', 'l']; |
| 33 |
} |
| 34 |
|
| 35 |
constructor() { |
| 36 |
// Always call super first in constructor |
| 37 |
super(); |
| 38 |
|
| 39 |
const shadow = this.attachShadow({mode: 'open'}); |
| 40 |
|
| 41 |
const div = document.createElement('div'); |
| 42 |
div.setAttribute('id', 'outer-div'); |
| 43 |
div.setAttribute('title', 'In the Shadow Realms'); |
| 44 |
div.setAttribute('class', 'outer-div-class'); |
| 45 |
const style = document.createElement('style'); |
| 46 |
shadow.appendChild(style); |
| 47 |
shadow.appendChild(div); |
| 48 |
const meta = document.createElement('meta'); |
| 49 |
meta.setAttribute('name', 'meta-name'); |
| 50 |
meta.setAttribute('title', 'Very META'); |
| 51 |
shadow.appendChild(meta); |
| 52 |
const span1 = document.createElement('span'); |
| 53 |
shadow.appendChild(span1); |
| 54 |
const span2 = document.createElement('span'); |
| 55 |
shadow.appendChild(span2); |
| 56 |
const link = document.createElement('a'); |
| 57 |
link.setAttribute('href', 'https://metacpan.org'); |
| 58 |
const innerText = document.createTextNode('MetaCPAN'); |
| 59 |
link.appendChild(innerText); |
| 60 |
shadow.appendChild(link); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
connectedCallback() { |
| 65 |
console.log('Custom square element added to page.'); |
| 66 |
updateStyle(this); |
| 67 |
} |
| 68 |
|
| 69 |
disconnectedCallback() { |
| 70 |
console.log('Custom square element removed from page.'); |
| 71 |
} |
| 72 |
|
| 73 |
adoptedCallback() { |
| 74 |
console.log('Custom square element moved to new page.'); |
| 75 |
} |
| 76 |
|
| 77 |
attributeChangedCallback(name, oldValue, newValue) { |
| 78 |
console.log('Custom square element attributes changed.'); |
| 79 |
updateStyle(this); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
customElements.define('custom-square', Square); |
| 84 |
|
| 85 |
function updateStyle(elem) { |
| 86 |
const shadow = elem.shadowRoot; |
| 87 |
shadow.querySelector('style').textContent = ` |
| 88 |
div { |
| 89 |
width: ${elem.getAttribute('l')}px; |
| 90 |
height: ${elem.getAttribute('l')}px; |
| 91 |
background-color: ${elem.getAttribute('c')}; |
| 92 |
} |
| 93 |
`; |
| 94 |
} |
| 95 |
|
| 96 |
const add = document.querySelector('.add'); |
| 97 |
const update = document.querySelector('.update'); |
| 98 |
const remove = document.querySelector('.remove'); |
| 99 |
let square; |
| 100 |
|
| 101 |
update.disabled = true; |
| 102 |
remove.disabled = true; |
| 103 |
|
| 104 |
function random(min, max) { |
| 105 |
return Math.floor(Math.random() * (max - min + 1) + min); |
| 106 |
} |
| 107 |
|
| 108 |
add.onclick = function() { |
| 109 |
// Create a custom square element |
| 110 |
square = document.createElement('custom-square'); |
| 111 |
square.setAttribute('l', '100'); |
| 112 |
square.setAttribute('c', 'red'); |
| 113 |
document.body.appendChild(square); |
| 114 |
|
| 115 |
update.disabled = false; |
| 116 |
remove.disabled = false; |
| 117 |
add.disabled = true; |
| 118 |
}; |
| 119 |
|
| 120 |
update.onclick = function() { |
| 121 |
// Randomly update square's attributes |
| 122 |
square.setAttribute('l', random(50, 200)); |
| 123 |
square.setAttribute('c', `rgb(${random(0, 255)}, ${random(0, 255)}, ${random(0, 255)})`); |
| 124 |
}; |
| 125 |
|
| 126 |
remove.onclick = function() { |
| 127 |
// Remove the square |
| 128 |
document.body.removeChild(square); |
| 129 |
|
| 130 |
update.disabled = true; |
| 131 |
remove.disabled = true; |
| 132 |
add.disabled = false; |
| 133 |
}; |
| 134 |
// --> |
| 135 |
</script> |
| 136 |
</body> |
| 137 |
</html> |