Skip to content

Commit 98bbf29

Browse files
committed
fix(tooltip): keyboard accessibility
1 parent 7b709f2 commit 98bbf29

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

src/dialog/dialog.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
146146
}
147147
}
148148
this.placeDialog();
149-
dialogElement.focus();
150149
const parentEl: HTMLElement = this.dialogConfig.parentRef.nativeElement;
151150
let node = parentEl;
152151
let observables = [];
@@ -308,7 +307,9 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
308307
* @memberof Dialog
309308
*/
310309
ngOnDestroy() {
311-
this.resizeSubscription.unsubscribe();
310+
if (this.resizeSubscription) {
311+
this.resizeSubscription.unsubscribe();
312+
}
312313
this.scrollSubscription.unsubscribe();
313314
}
314315
}

src/dialog/dialog.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class DialogService {
143143
* @param {any} [evt]
144144
* @memberof DialogService
145145
*/
146-
close(viewContainer: ViewContainerRef) {
146+
close(viewContainer: ViewContainerRef, focusPreviouslyFocusedElement = true) {
147147
this.isClosed.emit(true);
148148

149149
if (this.dialogRef) {
@@ -155,7 +155,9 @@ export class DialogService {
155155
}
156156
this.dialogRef = null;
157157
this.isOpen = false;
158-
elementToFocus.focus();
158+
if (focusPreviouslyFocusedElement) {
159+
elementToFocus.focus();
160+
}
159161

160162
if (this.dialogSubscription) {
161163
this.dialogSubscription.unsubscribe();

src/dialog/tooltip/tooltip.component.ts

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

@@ -15,7 +16,6 @@ import { Dialog } from "./../dialog.component";
1516
#dialog
1617
[id]="dialogConfig.compID"
1718
role="tooltip"
18-
tabindex="0"
1919
class="bx--tooltip bx--tooltip--shown">
2020
<span class="bx--tooltip__caret" aria-hidden="true"></span>
2121
<ng-template
@@ -43,4 +43,9 @@ export class Tooltip extends Dialog {
4343
onDialogInit() {
4444
this.hasContentTemplate = this.dialogConfig.content instanceof TemplateRef;
4545
}
46+
47+
@HostListener("keydown", ["$event"])
48+
onBlur(event: KeyboardEvent) {
49+
this.doClose();
50+
}
4651
}

src/dialog/tooltip/tooltip.directive.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
Injector,
77
ComponentFactoryResolver,
88
ViewContainerRef,
9-
HostBinding
9+
HostBinding,
10+
HostListener
1011
} from "@angular/core";
1112
import { DialogDirective } from "./../dialog.directive";
1213
import { Tooltip } from "./tooltip.component";
@@ -51,7 +52,13 @@ export class TooltipDirective extends DialogDirective {
5152
// tslint:disable-next-line:no-input-rename
5253
@Input("tooltip-type") tooltipType: "warning" | "error" | "" = "";
5354

54-
@HostBinding("attr.aria-describedby") descriptorId: string;
55+
@HostBinding("tabindex") tabIndex = 0;
56+
57+
@HostBinding("attr.aria-describedby") get descriptorId(): string {
58+
if (this.expanded) {
59+
return this.dialogConfig.compID;
60+
}
61+
}
5562

5663
/**
5764
* Creates an instance of `TooltipDirective`.
@@ -73,6 +80,21 @@ export class TooltipDirective extends DialogDirective {
7380
this.dialogConfig.compID = "tooltip-" + TooltipDirective.tooltipCounter;
7481
this.dialogConfig.content = this.ibmTooltip;
7582
this.dialogConfig.type = this.tooltipType;
76-
this.descriptorId = this.dialogConfig.compID;
83+
}
84+
85+
close() {
86+
this.dialogService.close(this.viewContainerRef, false);
87+
this.expanded = false;
88+
}
89+
90+
@HostListener("keydown", ["$event"])
91+
onKeydown(event: KeyboardEvent) {
92+
console.log(event.key);
93+
if (this.trigger === "click" && (event.key === "Enter" || event.key === " " )) {
94+
this.open();
95+
}
96+
if (event.key === "Escape") {
97+
this.close();
98+
}
7799
}
78100
}

0 commit comments

Comments
 (0)