Skip to content

Commit bb9b0ce

Browse files
committed
feat(tooltip): only show tooltip on focus via Tab key
1 parent d00137f commit bb9b0ce

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/tooltip/tooltip.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ export default class TooltipElement extends HTMLElement {
1313
private showing: boolean = false;
1414
private cleanup?: () => void;
1515
private prevInnerHTML?: string;
16+
private tabPressed: boolean = false;
1617

1718
private onMouseEnter = this.afterDelay.bind(this, this.show);
18-
private onFocus = this.show.bind(this);
19+
private onFocus = () => {
20+
if (this.tabPressed) this.show();
21+
};
1922
private onMouseLeave = this.afterDelay.bind(this, this.hide);
2023
private onBlur = this.hide.bind(this);
2124

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

4346
document.addEventListener('keydown', this.onKeyDown);
47+
document.addEventListener('keyup', this.onKeyUp);
4448
document.addEventListener('scroll', this.onBlur);
4549
}
4650

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

6468
document.removeEventListener('keydown', this.onKeyDown);
69+
document.removeEventListener('keyup', this.onKeyUp);
6570
document.removeEventListener('scroll', this.onBlur);
6671

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

8388
private onKeyDown = (e: KeyboardEvent): void => {
89+
if (e.key === 'Tab') this.tabPressed = true;
8490
if (e.key === 'Escape') {
8591
this.hide();
8692
}
8793
};
8894

95+
private onKeyUp = (e: KeyboardEvent): void => {
96+
if (e.key === 'Tab') this.tabPressed = false;
97+
};
98+
8999
private show() {
90100
if (this.disabled) return;
91101

0 commit comments

Comments
 (0)