-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode.html
77 lines (60 loc) · 1.59 KB
/
mode.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<html>
<head>
<title>Custom Element simples</title>
</head>
<body>
<h3>Custom element simples</h3>
<custom-element data-mode="open">
<h3>custom element 01 - aberto</h3>
</custom-element>
<custom-element data-mode="closed">
<h3>custom element 02 - fechado</h3>
</custom-element>
<custom-element data-mode="open">
<h3>custom element 03 - aberto</h3>
</custom-element>
<custom-element data-mode="closed">
<h3>custom element 04 - fechado</h3>
</custom-element>
<template id="myFirstWebcomponent">
<style>
h3{
color: #ff9100;
font-size: 2rem;
}
</style>
<h3>Hello world!</h3>
<slot></slot>
</template>
<script>
class CustomElementClass extends HTMLElement
{
constructor()
{
super();
const template = document.getElementById('myFirstWebcomponent');
const dataMode = this.dataset.mode;
let shadowRoot = this.attachShadow({mode: dataMode});
shadowRoot.appendChild(template.content.cloneNode(true));
}
}
if(!'customElements' in window) {
console.error( new Error('Seu browser não suporta a customElements') );
}
customElements.define('custom-element', CustomElementClass);
testMode();
function testMode(){
let elements = document.querySelectorAll('custom-element');
let shadowroot = elements.shadowRoot;
elements.forEach(function(el)
{
if (!el.shadowRoot) {
console.warn(`Não é possível acessar o shadow DOM do elemento, portando suas propriedades internas: mode, host e innerHTML não estão disponíveis`);
return false;
}
console.log(el.shadowRoot, '\n', el.shadowRoot.mode, '\n', el.shadowRoot.host, '\n', el.shadowRoot.innerHTML);
});
}
</script>
</body>
</html>