forked from carbon-design-system/carbon-components-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefinition-tooptip.component.ts
More file actions
115 lines (103 loc) · 2.82 KB
/
definition-tooptip.component.ts
File metadata and controls
115 lines (103 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
HostListener,
Input,
NgZone,
Renderer2,
TemplateRef
} from "@angular/core";
import { PopoverContainer } from "carbon-components-angular/popover";
/**
* Get started with importing the module:
*
* ```typescript
* import { TooltipModule } from 'carbon-components-angular';
* ```
*
* [See demo](../../?path=/story/components-tooltip-definition--basic)
*/
@Component({
selector: "cds-tooltip-definition, ibm-tooltip-definition",
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<button
class="cds--definition-term"
[attr.aria-controls]="id"
[attr.aria-expanded]="isOpen"
[attr.aria-describedby]="isOpen ? id : null"
(blur)="onBlur($event)"
(click)="onClick($event)"
type="button">
<ng-content></ng-content>
</button>
<span
*ngIf="description"
class="cds--popover"
[id]="id"
[attr.aria-hidden]="!isOpen"
role="tooltip">
<span class="cds--popover-content cds--definition-tooltip" aria-live="polite">
<ng-container *ngIf="!isTemplate(description)">{{description}}</ng-container>
<ng-template *ngIf="isTemplate(description)" [ngTemplateOutlet]="description" [ngTemplateOutletContext]="{ $implicit: templateContext }"></ng-template>
<span *ngIf="autoAlign" class="cds--popover-caret cds--popover--auto-align"></span>
</span>
<span *ngIf="!autoAlign" class="cds--popover-caret"></span>
</span>
`
})
export class TooltipDefinition extends PopoverContainer {
static tooltipCount = 0;
@Input() id = `tooltip-definition-${TooltipDefinition.tooltipCount++}`;
/**
* The string or template content to be exposed by the tooltip.
*/
@Input() description: string | TemplateRef<any>;
/**
* Optional data for templates passed as implicit context
*/
@Input() templateContext: any;
@Input() openOnHover = false;
constructor(
protected elementRef: ElementRef,
protected ngZone: NgZone,
protected renderer: Renderer2,
protected changeDetectorRef: ChangeDetectorRef
) {
super(elementRef, ngZone, renderer, changeDetectorRef);
this.highContrast = true;
this.dropShadow = false;
}
onBlur(event: Event) {
this.handleChange(false, event);
}
onClick(event: Event) {
this.handleChange(!this.isOpen, event);
}
@HostListener("keyup", ["$event"])
hostkeys(event: KeyboardEvent) {
if (this.isOpen && event.key === "Escape") {
event.stopPropagation();
this.handleChange(false, event);
}
}
@HostListener("mouseleave", ["$event"])
mouseleave(event) {
this.handleChange(false, event);
}
@HostListener("mouseenter", ["$event"])
mouseenter(event) {
if (this.openOnHover) {
this.handleChange(true, event);
}
}
@HostListener("focusin", ["$event"])
onFocus(event) {
this.handleChange(true, event);
}
public isTemplate(value) {
return value instanceof TemplateRef;
}
}