-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegates-focus.html
129 lines (111 loc) · 3.48 KB
/
delegates-focus.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: delegatesFocus</title>
<meta name="author" title="Eugene Kashida" href="mailto:[email protected]">
</head>
<body>
<script>
class DelegatesFalse extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({
delegatesFocus: false,
mode: 'open',
});
this._shadowRoot.innerHTML = `
<div>
<h3>no tabindex</h3>
<input placeholder="delegatesFocus=false">
</div>
`;
}
connectedCallback() {
var tabindex = this.getAttribute('tabindex');
if (tabindex) {
this._shadowRoot
.querySelector('h3')
.textContent = `tabindex=${tabindex}`;
}
this.addEventListener('focus', function() {
console.log('focus host element');
});
this._shadowRoot.querySelector('input').addEventListener('focus', function() {
console.log('focus internal input');
});
}
}
class DelegatesTrue extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({
delegatesFocus: true,
mode: 'open',
});
this._shadowRoot.innerHTML = `
<div>
<h3>no tabindex</h3>
<input placeholder="delegatesFocus=true">
</div>
`;
}
connectedCallback() {
var tabindex = this.getAttribute('tabindex');
if (tabindex) {
this._shadowRoot
.querySelector('h3')
.textContent = `tabindex=${tabindex}`;
}
this.addEventListener('focus', function() {
console.log('focus host element');
});
this._shadowRoot.querySelector('input').addEventListener('focus', function() {
console.log('focus internal input');
});
}
}
customElements.define('delegates-true', DelegatesTrue);
customElements.define('delegates-false', DelegatesFalse);
var div = document.createElement('div');
div.innerHTML = `
<input placeholder="spacer">
<delegates-false class="notabindex"></delegates-false>
<input placeholder="spacer">
<delegates-false tabindex="0"></delegates-false>
<input placeholder="spacer">
<delegates-false tabindex="-1"></delegates-false>
<input placeholder="spacer">
<delegates-true class="notabindex"></delegates-true>
<input placeholder="spacer">
<delegates-true tabindex="0"></delegates-true>
<input placeholder="spacer">
<delegates-true tabindex="-1"></delegates-true>
<input placeholder="spacer">
`;
document.body.appendChild(div);
[
'delegates-false.notabindex',
'delegates-false[tabindex="0"]',
'delegates-false[tabindex="-1"]',
'delegates-true.notabindex',
'delegates-true[tabindex="0"]',
'delegates-true[tabindex="-1"]',
].forEach((selector) => {
var button = document.createElement('button');
button.textContent = selector;
button.style = 'margin-top: 1em; display: block;';
button.addEventListener('click', function () {
document.querySelector(selector).focus();
});
div.appendChild(button);
});
setInterval(function () {
var activeElement = document.activeElement;
while (activeElement.shadowRoot && activeElement.shadowRoot.activeElement) {
activeElement = activeElement.shadowRoot.activeElement;
};
console.log(activeElement);
}, 3000);
</script>
</body>
</html>