This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathte-checkmark.js
89 lines (79 loc) · 2.02 KB
/
te-checkmark.js
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
class TeCheckmark extends HTMLElement {
constructor() {
super();
const shadowDom = this.attachShadow({mode: 'open'});
shadowDom.appendChild(this.template);
this.addEventListener('click', () => {
if (this.checked) {
this.checked = false;
} else {
this.checked = true;
}
});
this.addEventListener('keydown', e => {
if (e.keyCode !== 32 && e.keyCode !== 13) {
return;
}
if (this.checked) {
this.checked = false;
} else {
this.checked = true;
}
});
}
connectedCallback() {
this.setAttribute('tabindex', '0');
this.setAttribute('role', 'checkbox');
if (this.checked) {
this.setAttribute('aria-checked', 'true');
} else {
this.setAttribute('aria-checked', 'false');
}
}
set checked(checked) {
if (checked) {
this.setAttribute('checked', '');
this.setAttribute('aria-checked', 'true');
} else {
this.removeAttribute('checked');
this.setAttribute('aria-checked', 'false');
}
this.dispatchEvent(new CustomEvent('change'));
}
get checked() {
return this.hasAttribute('checked');
}
get template() {
const template = document.createElement('template');
template.innerHTML = `
<style>
:host {
display:inline-block;
font-family:-apple-system,BlinkMacSystemFont,segoe ui,Roboto,Helvetica,Arial,sans-serif,apple color emoji,segoe ui emoji,segoe ui symbol;
font-size:1rem;
line-height:1rem;
height:1rem;
padding:var(--te-checkmark-padding, .35rem);
min-width:1rem;
border:1px solid black;
text-align:center;
border-radius:calc(2rem + var(--te-checkmark-padding, .35rem));
font-weight:lighter;
cursor:pointer;
user-select:none;
}
:host([checked]) {
color:var(--te-checkmark-checked-color, #4CC552);
border-color:var(--te-checkmark-checked-color, #4CC552);
}
</style>
<slot></slot>
<span>✓</span>`;
return template.content.cloneNode(true);
}
}
if (window.customElements) {
window.customElements.define('te-checkmark', TeCheckmark);
} else {
throw new Error(`te-checkmark could not be registered`);
}