Skip to content

Commit adc7ba3

Browse files
committed
fix(ky-040): user experience #91
1. pressing the handle and dragging the mouse outside now releases the button 2. enlarge arrows' hit boxes 3. implement arrow long press fix #91
1 parent ebff6b1 commit adc7ba3

File tree

1 file changed

+91
-40
lines changed

1 file changed

+91
-40
lines changed

src/ky-040-element.ts

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ export class KY040Element extends LitElement {
88
@property() stepSize = 18;
99
@property() private pressed = false;
1010

11+
private arrowTimer: ReturnType<typeof setInterval> | null = null;
12+
1113
static get styles() {
1214
return css`
1315
svg {
1416
user-select: none;
1517
}
1618
17-
.arrow {
19+
.arrow-container {
1820
cursor: pointer;
1921
}
2022
@@ -25,8 +27,8 @@ export class KY040Element extends LitElement {
2527
transition: stroke-width 0.3s;
2628
}
2729
28-
svg:hover .arrow:hover {
29-
fill: black;
30+
.arrow-container:hover .arrow {
31+
fill: white;
3032
}
3133
3234
svg:hover .handle:hover {
@@ -45,6 +47,14 @@ export class KY040Element extends LitElement {
4547
.handle {
4648
cursor: pointer;
4749
}
50+
51+
g[tabindex]:focus {
52+
outline: none;
53+
}
54+
55+
g[tabindex]:focus + .focus-indicator {
56+
opacity: 1;
57+
}
4858
`;
4959
}
5060

@@ -96,57 +106,77 @@ export class KY040Element extends LitElement {
96106
</g>
97107
98108
<g tabindex="0" @keydown=${this.keydown} @keyup=${this.keyup}>
99-
<rect
100-
x="12.2"
101-
y="8.05"
102-
width="48.4"
103-
height="41"
104-
rx="7.12"
105-
fill="#e6e6e6"
106-
fill-rule="evenodd"
107-
/>
108-
<g>
109-
<g fill-rule="evenodd">
110-
<circle cx="36.6" cy="28.5" r="13.5" fill="#666" />
111-
<rect x="32.5" y="7.87" width="7.42" height="41.5" fill="#666" />
112-
113-
<!-- handle -->
114-
<path
115-
transform="rotate(${this.angle}, 36.244, 28.5)"
116-
d="m36.3 21.4a7.03 7.14 0 0
109+
<rect x="12.2" y="8.05" width="48.4" height="41" rx="7.12" fill="#e6e6e6" />
110+
111+
<circle cx="36.6" cy="28.5" r="13.5" fill="#666" />
112+
<rect x="32.5" y="7.87" width="7.42" height="41.5" fill="#666" />
113+
114+
<!-- Handle -->
115+
<path
116+
transform="rotate(${this.angle}, 36.244, 28.5)"
117+
d="m36.3 21.4a7.03 7.14 0 0
117118
0-3.74 1.1v12.1a7.03 7.14 0 0 0 3.74 1.1 7.03 7.14 0 0 0 7.03-7.14 7.03 7.14 0 0
118119
0-7.03-7.14z"
119-
fill="#ccc"
120-
stroke="#060606"
121-
stroke-width=".3"
122-
class="handle ${this.pressed ? 'active' : ''}"
123-
@mousedown=${this.press}
124-
@mouseup=${this.release}
125-
/>
126-
</g>
127-
128-
<!-- Counter Clockwise Arrow -->
120+
fill="#ccc"
121+
stroke="#060606"
122+
stroke-width=".3"
123+
class="handle ${this.pressed ? 'active' : ''}"
124+
@mousedown=${this.press}
125+
@mouseup=${this.release}
126+
@mouseleave=${this.release}
127+
/>
128+
129+
<!-- Counter Clockwise Arrow -->
130+
<g
131+
class="arrow-container"
132+
@click=${this.counterClockwiseStep}
133+
@mousedown=${this.counterclockwiseArrowPress}
134+
@mouseup=${this.arrowRelease}
135+
@mouseleave=${this.arrowRelease}
136+
>
137+
<circle cx="20" cy="43" r="12" fill="red" opacity="0" />
129138
<path
130139
d="m21 44.5c-5.17-1.78-7.55-5.53-6.6-11.2 0.0662-0.327 0.107-0.938 0.272-1.06 0.204-0.137 0.312-0.116 0.39-0.1 0.0775 0.0152 0.139 0.0274 0.189 0.102 0.846 3.81 3.13 6.84 6.57 7.59 0.304-0.787 0.461-3.32 0.826-3.24 0.428 0.0848 4.31 5.73 4.93 6.65-0.978 0.839-6.07 4.44-6.95 4.28 0 0 0.206-2.19 0.362-2.96z"
131140
fill="#b3b3b3"
132141
stroke="#000"
133142
stroke-width=".0625px"
134143
class="arrow"
135-
@click=${this.counterClockwiseClick}
136144
/>
145+
</g>
137146
138-
<!-- Clockwise Arrow -->
147+
<!-- Clockwise Arrow -->
148+
<g
149+
class="arrow-container"
150+
@click=${this.clockwiseStep}
151+
@mousedown=${this.clockwiseArrowPress}
152+
@mouseup=${this.arrowRelease}
153+
@mouseleave=${this.arrowRelease}
154+
>
155+
<circle cx="20" cy="15" r="12" fill="red" opacity="0" />
139156
<path
140157
d="m21.2 12.1c-5.17 1.78-7.55 5.53-6.6 11.2 0.0662 0.327 0.107 0.938 0.272 1.06 0.204 0.137 0.312 0.116 0.39 0.1 0.0775-0.0152 0.139-0.0274 0.189-0.102 0.846-3.81 3.13-6.84 6.57-7.59 0.304 0.787 0.461 3.32 0.826 3.24 0.428-0.0848 4.31-5.73 4.93-6.65-0.978-0.839-6.07-4.44-6.95-4.28 0 0 0.206 2.19 0.362 2.96z"
141158
fill="#b3b3b3"
142159
stroke="#022"
143160
stroke-width=".0625px"
144161
class="arrow"
145-
@click=${this.clockwiseClick}
146162
/>
147163
</g>
148164
</g>
149165
166+
<!-- Focus indicator for the group above-->
167+
<rect
168+
class="focus-indicator"
169+
x="10.2"
170+
y="6.05"
171+
width="52.4"
172+
height="45"
173+
rx="7.12"
174+
stroke="white"
175+
stroke-width="2"
176+
fill="none"
177+
opacity="0"
178+
/>
179+
150180
<!-- Chip Pins -->
151181
<rect
152182
x="83"
@@ -177,12 +207,12 @@ export class KY040Element extends LitElement {
177207
`;
178208
}
179209

180-
private clockwiseClick() {
210+
private clockwiseStep() {
181211
this.angle = (this.angle + this.stepSize) % 360;
182212
this.dispatchEvent(new InputEvent('rotate-cw'));
183213
}
184214

185-
private counterClockwiseClick() {
215+
private counterClockwiseStep() {
186216
this.angle = (this.angle - this.stepSize + 360) % 360;
187217
this.dispatchEvent(new InputEvent('rotate-ccw'));
188218
}
@@ -195,21 +225,42 @@ export class KY040Element extends LitElement {
195225
}
196226

197227
private release() {
198-
this.dispatchEvent(new InputEvent('button-release'));
199-
this.pressed = false;
228+
if (this.pressed) {
229+
this.dispatchEvent(new InputEvent('button-release'));
230+
this.pressed = false;
231+
}
232+
}
233+
234+
private counterclockwiseArrowPress() {
235+
this.arrowTimer = setInterval(() => {
236+
this.counterClockwiseStep();
237+
}, 300);
238+
}
239+
240+
private clockwiseArrowPress() {
241+
this.arrowTimer = setInterval(() => {
242+
this.clockwiseStep();
243+
}, 300);
244+
}
245+
246+
private arrowRelease() {
247+
if (this.arrowTimer != null) {
248+
clearInterval(this.arrowTimer);
249+
this.arrowTimer = null;
250+
}
200251
}
201252

202253
private keydown(e: KeyboardEvent) {
203254
switch (e.key) {
204255
case 'ArrowUp':
205256
case 'ArrowRight':
206-
this.clockwiseClick();
257+
this.clockwiseStep();
207258
e.preventDefault();
208259
break;
209260

210261
case 'ArrowDown':
211262
case 'ArrowLeft':
212-
this.counterClockwiseClick();
263+
this.counterClockwiseStep();
213264
e.preventDefault();
214265
break;
215266

0 commit comments

Comments
 (0)