-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
100 lines (81 loc) · 2.32 KB
/
Copy pathbase.js
File metadata and controls
100 lines (81 loc) · 2.32 KB
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
import { reset } from '@aegisjsproject/styles/reset.js';
import { layers } from '@aegisjsproject/styles/layers.js';
import { customButton } from '@aegisjsproject/styles/custom-button.js';
const sheet = new CSSStyleSheet();
sheet.replace(`@layer components {
:host([hidden]) {
display: none;
}
}`);
const styles = [layers, reset, customButton, sheet];
export class CustomButton extends HTMLElement {
#shadow = this.attachShadow({ mode: 'open' });
#internals = this.attachInternals();
#oldTabIndex = NaN;
#controller;
constructor() {
super();
this.#shadow.adoptedStyleSheets = styles;
this.#internals.role = 'button';
}
connectedCallback() {
this.#controller = new AbortController();
if (this.#shadow.childElementCount === 0) {
this.#shadow.append(document.createElement('slot'));
}
if (! (this.hasAttribute('tabindex') || this.disabled)) {
this.tabIndex = 0;
}
this.addEventListener('keydown', event => {
if (event.key === 'Enter' && ! this.disabled) {
event.preventDefault();
event.currentTarget.click();
} else if (event.key === ' ' && ! this.disabled) {
event.preventDefault();
}
}, { signal: this.#controller.signal });
this.addEventListener('keyup', event => {
if (event.key === ' ' && ! this.disabled) {
event.preventDefault();
event.currentTarget.click();
}
}, { signal: this.#controller.signal });
}
attributeChangedCallback(name, oldVal, newVal) {
switch(name) {
case 'disabled':
if (typeof newVal === 'string') {
this.#oldTabIndex = this.hasAttribute('tabindex') ? this.tabIndex : 0;
this.tabIndex = -1;
this.#internals.states.add('disabled');
this.#internals.ariaDisabled = 'true';
if (this.isSameNode(this.ownerDocument.activeElement)) {
this.blur();
}
} else {
this.#internals.states.delete('disabled');
this.#internals.ariaDisabled = null;
this.tabIndex = Number.isNaN(this.#oldTabIndex) ? 0 : this.#oldTabIndex;
}
break;
}
}
disconnectedCallback() {
this.#controller.abort();
}
get disabled() {
return this.hasAttribute('disabled');
}
set disabled(val) {
this.toggleAttribute('disabled', val);
}
get signal() {
return this.#controller.signal;
}
static get observedAttributes() {
return ['disabled'];
}
static register(tag) {
customElements.define(tag, this);
}
}