Skip to content

Commit

Permalink
fix: unwanted keyboard event when unfocused on Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed May 15, 2024
1 parent 81843b9 commit 51347f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/sc-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ScButtonBase extends ScElement {
type: Boolean,
reflect: true,
},
disableKeyboard: {
type: Boolean,
reflect: true,
attribute: 'disable-keyboard',
},
_pressed: {
type: Boolean,
state: true,
Expand Down Expand Up @@ -184,7 +189,7 @@ class ScButtonBase extends ScElement {
}

_onKeyboardEvent(e) {
if (this.disabled) { return; }
if (this.disabled || this.disableKeyboard) { return; }

const eventName = e.type === 'keydown' ? 'press' : 'release';
this._dispatchEvent(eventName);
Expand All @@ -200,7 +205,7 @@ class ScButtonBase extends ScElement {
}

_dispatchEvent(eventName) {
// we don't want to trigger a release if no pressed has been recorded
// we don't want to trigger a release if no pressed has been recorded
if (eventName === 'release' && this._pressed === false) {
return;
}
Expand Down
18 changes: 5 additions & 13 deletions src/sc-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ScTab extends ScElement {
return html`
<sc-button
.value=${value}
disable-keyboard
?selected=${value === this.value}
@input="${this._onInput}"
@focus=${e => e.preventDefault()}
Expand All @@ -120,19 +121,10 @@ class ScTab extends ScElement {
if (e.type === 'keydown') {
let index = this.options.indexOf(this.value);

// ArrowUp / ArrowDown do not behave the same regarding direction
if (this.orientation === 'horizontal') {
if (e.code === 'ArrowUp' || e.code === 'ArrowRight' || e.code === 'Space' || e.code == 'Enter') {
index += 1;
} else if (e.code === 'ArrowDown' || e.code === 'ArrowLeft') {
index -= 1;
}
} else {
if (e.code === 'ArrowDown' || e.code === 'ArrowRight' || e.code === 'Space' || e.code == 'Enter') {
index += 1;
} else if (e.code === 'ArrowUp' || e.code === 'ArrowLeft') {
index -= 1;
}
if (e.code === 'ArrowDown' || e.code === 'ArrowRight' || e.code === 'Space' || e.code === 'Enter') {
index += 1;
} else if (e.code === 'ArrowUp' || e.code === 'ArrowLeft') {
index -= 1;
}

if (index < 0) {
Expand Down

0 comments on commit 51347f8

Please sign in to comment.