Skip to content

Commit

Permalink
feat(tooltip): only show tooltip on focus via Tab key
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyzerner committed Nov 6, 2023
1 parent d00137f commit bb9b0ce
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ export default class TooltipElement extends HTMLElement {
private showing: boolean = false;
private cleanup?: () => void;
private prevInnerHTML?: string;
private tabPressed: boolean = false;

private onMouseEnter = this.afterDelay.bind(this, this.show);
private onFocus = this.show.bind(this);
private onFocus = () => {
if (this.tabPressed) this.show();
};
private onMouseLeave = this.afterDelay.bind(this, this.hide);
private onBlur = this.hide.bind(this);

Expand All @@ -41,6 +44,7 @@ export default class TooltipElement extends HTMLElement {
}

document.addEventListener('keydown', this.onKeyDown);
document.addEventListener('keyup', this.onKeyUp);
document.addEventListener('scroll', this.onBlur);
}

Expand All @@ -62,6 +66,7 @@ export default class TooltipElement extends HTMLElement {
}

document.removeEventListener('keydown', this.onKeyDown);
document.removeEventListener('keyup', this.onKeyUp);
document.removeEventListener('scroll', this.onBlur);

this.disabledObserver?.disconnect();
Expand All @@ -81,11 +86,16 @@ export default class TooltipElement extends HTMLElement {
}

private onKeyDown = (e: KeyboardEvent): void => {
if (e.key === 'Tab') this.tabPressed = true;
if (e.key === 'Escape') {
this.hide();
}
};

private onKeyUp = (e: KeyboardEvent): void => {
if (e.key === 'Tab') this.tabPressed = false;
};

private show() {
if (this.disabled) return;

Expand Down

0 comments on commit bb9b0ce

Please sign in to comment.