Skip to content

Commit

Permalink
fix(shared): prevent focusing focusable root on second click
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook Johnson authored and Westbrook committed Jan 15, 2020
1 parent 7abf085 commit 0fb5006
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/shared/src/focusable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export class Focusable extends FocusVisiblePolyfillMixin(LitElement) {
}

protected manageFocusIn(): void {
this.addEventListener('mousedown', (event) => {
event.preventDefault();
});
this.addEventListener('focusin', (event) => {
if (event.composedPath()[0] === this) {
this.handleFocus();
Expand Down
13 changes: 13 additions & 0 deletions packages/tab-list/src/tab-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ export class TabList extends Focusable {
}
}

private isListeningToKeyboard = false;

public startListeningToKeyboard = (): void => {
this.addEventListener('keydown', this.handleKeydown);
this.isListeningToKeyboard = true;
};

public stopListeningToKeyboard = (): void => {
this.isListeningToKeyboard = false;
this.removeEventListener('keydown', this.handleKeydown);
};

Expand All @@ -136,6 +140,15 @@ export class TabList extends Focusable {
private onClick(event: Event): void {
const target = event.target as HTMLElement;
this.selectTarget(target);
if (this.isListeningToKeyboard) {
/* Trick :focus-visible polyfill into thinking keyboard based focus */
this.dispatchEvent(
new KeyboardEvent('keydown', {
code: 'Tab',
})
);
target.focus();
}
}

private onKeyDown(event: KeyboardEvent): void {
Expand Down

0 comments on commit 0fb5006

Please sign in to comment.