-
-
Notifications
You must be signed in to change notification settings - Fork 258
/
key.html
160 lines (139 loc) · 3.77 KB
/
key.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<template id="key-template">
<style>
.key {
position: relative;
display: block;
width: 100%;
height: 100%;
float: left;
font-family: "Overpass", sans-serif;
font-size: 12px;
background-color: #fff;
line-height: 16px;
border-radius: 2px;
cursor: pointer;
border: 1px solid rgb(130, 130, 130);
text-align: left;
transition: background-color 0.25s;
}
.key:hover {
background-color: #e3e3e3;
}
.key:active {
background-color: #8f8f8f;
}
:host([pressed="true"]) .key {
outline: 1px solid #333333;
background-color: #8f8f8f !important;
}
.key .content-top {
position: absolute;
top: 5px;
left: 5px;
}
.key .content-bottom {
position: absolute;
bottom: 5px;
left: 5px;
}
:host(.hidden) .key {
visibility: hidden;
}
:host(.accented) .key {
background-color: #dbdbdb;
}
</style>
<button type="button" class="key">
<span class="content-top">
<!-- default slot -->
<slot></slot>
</span>
<span class="content-bottom">
<slot name="bottom"></slot>
</span>
</button>
</template>
<script type="module">
import { isModifierCode } from "/js/keycodes.js";
(function () {
const template = document.querySelector("#key-template");
customElements.define(
"keyboard-key",
class extends HTMLElement {
constructor() {
super();
this.addEventListener("click", () => {
this.togglePressedState();
// Don't send event if click removed "pressed" state
// on a modifier key.
if (!this.isModifier || this.isPressed) {
this.dispatchEvent(
new CustomEvent("keyclick", {
detail: {
key: this.key,
code: this.code,
isModifier: this.isModifier,
},
bubbles: true,
composed: true,
})
);
} else {
this.dispatchEvent(
new CustomEvent("keyrelease", {
detail: {
key: this.key,
code: this.code,
},
bubbles: true,
composed: true,
})
);
}
});
}
connectedCallback() {
this.attachShadow({ mode: "open" }).appendChild(
template.content.cloneNode(true)
);
}
get code() {
return this.getAttribute("code");
}
get key() {
// If a key has a shifted label and an unshifted label, return the
// unshifted label.
const bottomLabel = this.querySelector("[slot=bottom]");
if (bottomLabel) {
return bottomLabel.innerText;
}
if (this.getAttribute("use-code")) {
return this.getAttribute("code");
}
const keyLabel = this.firstChild.data;
// If the key is a single A-Z character, return the lowercase
// version.
if (/^[A-Z]$/.test(keyLabel)) {
return keyLabel.toLowerCase();
}
return keyLabel;
}
get isModifier() {
return isModifierCode(this.code);
}
get isPressed() {
return this.getAttribute("pressed") === "true";
}
set isPressed(newValue) {
// Only modifier keys can be "pressed".
if (this.isModifier) {
this.setAttribute("pressed", newValue);
}
}
togglePressedState() {
this.isPressed = !this.isPressed;
}
}
);
})();
</script>