Skip to content

Commit e963a59

Browse files
fix(keypad): style of pressed buttons (#19)
* improvement: style of pressed buttons * improvement: buttons pressed Co-authored-by: Karin <karinch@users.noreply.github.com>
1 parent 82ddfaa commit e963a59

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

src/membrane-keypad-element.ts

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@ import { customElement, html, LitElement, property, svg } from 'lit-element';
22

33
const SPACE_KEY = 32;
44

5+
function isNumeric(text: string) {
6+
return !isNaN(parseFloat(text));
7+
}
8+
59
@customElement('wokwi-membrane-keypad')
610
export class MembraneKeypadElement extends LitElement {
711
@property() threeColumns = false;
812

913
private pressedKeys = new Set<string>();
1014

1115
renderKey(text: string, x: number, y: number) {
16+
const keyClass = isNumeric(text) ? 'blue-key' : 'red-key';
17+
1218
return svg`<g
1319
transform="translate(${x} ${y})"
1420
tabindex="0"
21+
class=${keyClass}
22+
@blur=${(e: FocusEvent) => {
23+
this.up(text, e);
24+
}}
1525
@mousedown=${() => this.down(text)}
1626
@mouseup=${() => this.up(text)}
1727
@touchstart=${() => this.down(text)}
1828
@touchend=${() => this.up(text)}
19-
@keydown=${(e: KeyboardEvent) => e.keyCode === SPACE_KEY && this.down(text)}
20-
@keyup=${(e: KeyboardEvent) => e.keyCode === SPACE_KEY && this.up(text)}
29+
@keydown=${(e: KeyboardEvent) => e.keyCode === SPACE_KEY && this.down(text, e)}
30+
@keyup=${(e: KeyboardEvent) => e.keyCode === SPACE_KEY && this.up(text, e)}
2131
>
2232
<use xlink:href="#key" />
2333
<text x="5.6" y="8.1">${text}</text>
@@ -26,27 +36,48 @@ export class MembraneKeypadElement extends LitElement {
2636

2737
render() {
2838
const fourColumns = !this.threeColumns;
29-
const columnWidth = 14.647;
39+
const columnWidth = 15;
3040
const width = fourColumns ? 70.336 : 70.336 - columnWidth;
3141

3242
return html`
3343
<style>
3444
text {
35-
fill: #c2e4fd;
45+
fill: #dfe2e5;
3646
user-select: none;
3747
}
3848
3949
g[tabindex] {
4050
cursor: pointer;
4151
}
4252
43-
g[tabindex]:focus {
44-
fill: #4e50d7;
53+
g[tabindex]:focus,
54+
g[tabindex]:active {
4555
stroke: white;
4656
outline: none;
4757
}
4858
59+
.blue-key:focus,
60+
.red-key:focus {
61+
filter: url(#shadow);
62+
}
63+
64+
.blue-key:active,
65+
.blue-key.pressed {
66+
fill: #4e50d7;
67+
}
68+
69+
.red-key:active,
70+
.red-key.pressed {
71+
fill: #ab040b;
72+
}
73+
4974
g[tabindex]:focus text {
75+
stroke: none;
76+
}
77+
78+
g[tabindex]:active text,
79+
.blue-key.pressed text,
80+
.red-key.pressed text {
5081
fill: white;
5182
stroke: none;
5283
}
@@ -69,9 +100,13 @@ export class MembraneKeypadElement extends LitElement {
69100
height="11"
70101
rx="1.4"
71102
ry="1.4"
72-
stroke="#b3c7db"
103+
stroke="#b1b5b9"
73104
stroke-width=".75"
74105
/>
106+
107+
<filter id="shadow">
108+
<feDropShadow dx="0" dy="0" stdDeviation="0.5" flood-color="#ffff99" />
109+
</filter>
75110
</defs>
76111
77112
<!-- Keypad outline -->
@@ -84,7 +119,7 @@ export class MembraneKeypadElement extends LitElement {
84119
rx="3.5"
85120
ry="3.5"
86121
fill="none"
87-
stroke="#b3c7db"
122+
stroke="#b1b5b9"
88123
stroke-width="1"
89124
/>
90125
@@ -118,8 +153,12 @@ export class MembraneKeypadElement extends LitElement {
118153
`;
119154
}
120155

121-
private down(key: string) {
156+
private down(key: string, event?: UIEvent) {
122157
if (!this.pressedKeys.has(key)) {
158+
if (event) {
159+
const currTarget = event.currentTarget as SVGElement;
160+
currTarget.classList.add('pressed');
161+
}
123162
this.pressedKeys.add(key);
124163
this.dispatchEvent(
125164
new CustomEvent('button-press', {
@@ -129,8 +168,12 @@ export class MembraneKeypadElement extends LitElement {
129168
}
130169
}
131170

132-
private up(key: string) {
171+
private up(key: string, event?: UIEvent) {
133172
if (this.pressedKeys.has(key)) {
173+
if (event) {
174+
const currTarget = event.currentTarget as SVGElement;
175+
currTarget.classList.remove('pressed');
176+
}
134177
this.pressedKeys.delete(key);
135178
this.dispatchEvent(
136179
new CustomEvent('button-release', {

0 commit comments

Comments
 (0)