Skip to content

Commit 2ae2d16

Browse files
authored
Merge pull request #2278 from Akshat55/tooltip-access-next
fix: add aria-labelledby attr to buttons in tooltip
2 parents 1c5ba27 + bb56d23 commit 2ae2d16

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/tooltip/tooltip.component.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import {
2+
AfterContentChecked,
23
Component,
4+
ElementRef,
35
HostBinding,
46
HostListener,
57
Input,
6-
TemplateRef
8+
TemplateRef,
9+
ViewChild
710
} from "@angular/core";
811
import { PopoverContainer } from "carbon-components-angular/popover";
912

@@ -13,16 +16,14 @@ import { PopoverContainer } from "carbon-components-angular/popover";
1316
@Component({
1417
selector: "ibm-tooltip",
1518
template: `
16-
<span
17-
[attr.aria-labelledby]="this.isTemplate(this.description) ? id : null"
18-
[attr.aria-describedby]="!this.isTemplate(this.description) ? id : null">
19+
<span #contentWrapper>
1920
<ng-content></ng-content>
2021
</span>
2122
<span
2223
*ngIf="description"
2324
class="cds--popover"
2425
[id]="id"
25-
[attr.aria-hidden]="isOpen"
26+
[attr.aria-hidden]="!isOpen"
2627
role="tooltip">
2728
<ng-container *ngIf="!disabled">
2829
<span class="cds--popover-content cds--tooltip-content">
@@ -34,7 +35,7 @@ import { PopoverContainer } from "carbon-components-angular/popover";
3435
</span>
3536
`
3637
})
37-
export class Tooltip extends PopoverContainer {
38+
export class Tooltip extends PopoverContainer implements AfterContentChecked {
3839
static tooltipCount = 0;
3940

4041
@HostBinding("class.cds--tooltip") tooltipClass = true;
@@ -57,6 +58,8 @@ export class Tooltip extends PopoverContainer {
5758
*/
5859
@Input() description: string | TemplateRef<any>;
5960

61+
@ViewChild("contentWrapper") wrapper: ElementRef<HTMLSpanElement>;
62+
6063
constructor() {
6164
super();
6265
this.highContrast = true;
@@ -96,7 +99,19 @@ export class Tooltip extends PopoverContainer {
9699
this.handleChange(false, event);
97100
}
98101

99-
public isTemplate(value) {
102+
isTemplate(value) {
100103
return value instanceof TemplateRef;
101104
}
105+
106+
/**
107+
* Check for any changes in the projected content & apply accessibility attribute if needed
108+
*/
109+
ngAfterContentChecked() {
110+
if (this.wrapper) {
111+
const buttonElement = this.wrapper.nativeElement.querySelector("button");
112+
if (buttonElement && !buttonElement.getAttribute("aria-labelledby")) {
113+
buttonElement.setAttribute("aria-labelledby", this.id);
114+
}
115+
}
116+
}
102117
}

0 commit comments

Comments
 (0)