Skip to content

Commit

Permalink
fix(pushbutton): button release doesn't work properly
Browse files Browse the repository at this point in the history
#578
  • Loading branch information
AriellaE committed Jun 29, 2023
1 parent 9568cc0 commit c493ea4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/pushbutton-element.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { css, html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { ElementPin } from './pin';
import { SPACE_KEYS } from './utils/keys';
import { ctrlCmdPressed, SPACE_KEYS } from './utils/keys';

@customElement('wokwi-pushbutton')
export class PushbuttonElement extends LitElement {
Expand Down Expand Up @@ -74,11 +74,12 @@ export class PushbuttonElement extends LitElement {
<button
aria-label="${label} ${color} pushbutton"
@mousedown=${this.down}
@mouseup=${(e: MouseEvent) => !e.ctrlKey && this.up()}
@mouseup=${this.up}
@touchstart=${this.down}
@touchend=${this.up}
@pointerleave=${this.up}
@keydown=${(e: KeyboardEvent) => SPACE_KEYS.includes(e.key) && this.down()}
@keyup=${(e: KeyboardEvent) => SPACE_KEYS.includes(e.key) && !e.ctrlKey && this.up()}
@keyup=${(e: KeyboardEvent) => SPACE_KEYS.includes(e.key) && this.up(e)}
>
<svg
width="17.802mm"
Expand Down Expand Up @@ -156,8 +157,8 @@ export class PushbuttonElement extends LitElement {
}
}

private up() {
if (this.pressed) {
private up(e: KeyboardEvent | MouseEvent) {
if (this.pressed && !ctrlCmdPressed(e)) {
this.pressed = false;
this.dispatchEvent(new Event('button-release'));
}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/keys.ts
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
export const SPACE_KEYS = [' ', 'Spacebar'];

export function getUserAgent() {
return typeof navigator === 'object' ? navigator.userAgent : '';
}

function isMac() {
return getUserAgent().indexOf('Macintosh') >= 0;
}

export function ctrlCmdPressed(e: KeyboardEvent | MouseEvent) {
return isMac() ? e.metaKey : e.ctrlKey;
}

0 comments on commit c493ea4

Please sign in to comment.