Skip to content

Commit 7ae2736

Browse files
authored
Merge pull request #322 from youda97/tooltip-focus
fix(tooltip): keyboard accessibility
2 parents f9475a4 + 3630d68 commit 7ae2736

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/dialog/dialog.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { throttleTime } from "rxjs/operators";
2020
// the AbsolutePosition is required to import the declaration correctly
2121
import position, { AbsolutePosition } from "./../utils/position";
22-
import { cycleTabs } from "./../common/tab.service";
22+
import { cycleTabs, getFocusElementList } from "./../common/tab.service";
2323
import { DialogConfig } from "./dialog-config.interface";
2424

2525

@@ -146,7 +146,9 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
146146
}
147147
}
148148
this.placeDialog();
149-
dialogElement.focus();
149+
if (getFocusElementList(this.dialog.nativeElement).length > 0) {
150+
dialogElement.focus();
151+
}
150152
const parentEl: HTMLElement = this.dialogConfig.parentRef.nativeElement;
151153
let node = parentEl;
152154
let observables = [];

src/dialog/dialog.directive.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
137137
// fix for safari hijacking clicks
138138
this.dialogService.singletonClickListen();
139139

140+
fromEvent(this.elementRef.nativeElement, "keydown").subscribe((event: KeyboardEvent) => {
141+
// "Esc" is an IE specific value
142+
if (event.target === this.dialogConfig.parentRef.nativeElement && (event.key === "Tab" || event.key === "Tab" && event.shiftKey) ||
143+
event.key === "Escape" || event.key === "Esc") {
144+
this.close();
145+
}
146+
});
147+
140148
// bind events for hovering or clicking the host
141149
if (this.trigger === "hover" || this.trigger === "mouseenter") {
142150
fromEvent(this.elementRef.nativeElement, "mouseenter").subscribe(() => this.toggle());
@@ -145,6 +153,12 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
145153
fromEvent(this.elementRef.nativeElement, "blur").subscribe(() => this.close());
146154
} else {
147155
fromEvent(this.elementRef.nativeElement, "click").subscribe(() => this.toggle());
156+
fromEvent(this.elementRef.nativeElement, "keydown").subscribe((event: KeyboardEvent) => {
157+
// "Spacebar" is an IE specific value
158+
if (event.key === "Enter" || event.key === " " || event.key === "Spacebar") {
159+
this.open();
160+
}
161+
});
148162
}
149163

150164
// call onClose when the dialog is closed

src/dialog/tooltip/tooltip.component.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {
22
Component,
33
TemplateRef,
4-
HostBinding
4+
HostBinding,
5+
Input
56
} from "@angular/core";
7+
import { getFocusElementList } from "./../../common/tab.service";
8+
69
import { Dialog } from "./../dialog.component";
710

811
/**
@@ -13,9 +16,9 @@ import { Dialog } from "./../dialog.component";
1316
template: `
1417
<div
1518
#dialog
19+
[tabindex]="tabIndex"
1620
[id]="dialogConfig.compID"
1721
role="tooltip"
18-
tabindex="0"
1922
class="bx--tooltip bx--tooltip--shown">
2023
<span class="bx--tooltip__caret" aria-hidden="true"></span>
2124
<ng-template
@@ -37,10 +40,13 @@ export class Tooltip extends Dialog {
3740
*/
3841
public hasContentTemplate = false;
3942

43+
@Input() tabIndex;
4044
/**
4145
* Check whether there is a template for the `Tooltip` content.
4246
*/
4347
onDialogInit() {
4448
this.hasContentTemplate = this.dialogConfig.content instanceof TemplateRef;
49+
50+
this.tabIndex = getFocusElementList(this.dialog.nativeElement).length > 0 ? 0 : -1;
4551
}
4652
}

src/dialog/tooltip/tooltip.directive.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export class TooltipDirective extends DialogDirective {
5151
// tslint:disable-next-line:no-input-rename
5252
@Input("tooltip-type") tooltipType: "warning" | "error" | "" = "";
5353

54+
55+
@HostBinding("tabindex") tabIndex = 0;
56+
5457
@HostBinding("attr.aria-describedby") get descriptorId(): string {
5558
return this.expanded ? this.dialogConfig.compID : null;
5659
}

0 commit comments

Comments
 (0)