src/tooltip/tooltip.component.ts
Get started with importing the module:
Example :import { TooltipModule } from 'carbon-components-angular';
OnChanges
AfterContentChecked
changeDetection | ChangeDetectionStrategy.OnPush |
selector | cds-tooltip, ibm-tooltip |
template |
|
Properties |
|
Methods |
Inputs |
Outputs |
HostBindings |
HostListeners |
constructor(elementRef: ElementRef, ngZone: NgZone, renderer: Renderer2, changeDetectorRef: ChangeDetectorRef)
|
|||||||||||||||
Defined in src/tooltip/tooltip.component.ts:81
|
|||||||||||||||
Parameters :
|
description | |
Type : string | TemplateRef<any>
|
|
Defined in src/tooltip/tooltip.component.ts:73
|
|
The string or template content to be exposed by the tooltip. |
disabled | |
Type : boolean
|
|
Default value : false
|
|
Defined in src/tooltip/tooltip.component.ts:69
|
|
Prevent tooltip from showing, used by icon button |
enterDelayMs | |
Type : number
|
|
Default value : 100
|
|
Defined in src/tooltip/tooltip.component.ts:61
|
|
Set delay before tooltip is shown |
id | |
Type : string
|
|
Default value : `tooltip-${Tooltip.tooltipCount++}`
|
|
Defined in src/tooltip/tooltip.component.ts:57
|
leaveDelayMs | |
Type : number
|
|
Default value : 300
|
|
Defined in src/tooltip/tooltip.component.ts:65
|
|
Set delay when tooltip disappears |
templateContext | |
Type : any
|
|
Defined in src/tooltip/tooltip.component.ts:77
|
|
Optional data for templates passed as implicit context |
align | |
Type : oldPlacement | Placement
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:45
|
|
Set alignment of popover
As of v5, When |
autoAlign | |
Type : boolean
|
|
Default value : false
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:115
|
|
Experimental: Use floating-ui to position the tooltip This is not toggleable - should be assigned once |
caret | |
Type : boolean
|
|
Default value : true
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:102
|
|
Show caret at the alignment position |
dropShadow | |
Type : boolean
|
|
Default value : true
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:106
|
|
Enable drop shadow around the popover container |
highContrast | |
Type : boolean
|
|
Default value : false
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:110
|
|
Enable high contrast for popover container |
isOpen | |
Type : boolean
|
|
Default value : false
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:117
|
isOpenChange | |
Type : EventEmitter
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:98
|
|
Emits an event when the state of |
onClose | |
Type : EventEmitter<Event>
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:90
|
|
Emits an event when the dialog is closed |
onOpen | |
Type : EventEmitter<Event>
|
|
Inherited from
PopoverContainer
|
|
Defined in
PopoverContainer:94
|
|
Emits an event when the dialog is opened |
class.cds--tooltip |
Type : boolean
|
Default value : true
|
Defined in src/tooltip/tooltip.component.ts:55
|
class.cds--popover-container |
Type : boolean
|
Default value : true
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:116
|
focusin |
Arguments : '$event'
|
focusin(event: Event)
|
Defined in src/tooltip/tooltip.component.ts:124
|
focusout |
Arguments : '$event'
|
focusout(event: Event)
|
Defined in src/tooltip/tooltip.component.ts:129
|
keyup |
Arguments : '$event'
|
keyup(event: KeyboardEvent)
|
Defined in src/tooltip/tooltip.component.ts:115
|
mouseenter |
Arguments : '$event'
|
mouseenter(event)
|
Defined in src/tooltip/tooltip.component.ts:95
|
mouseleave |
Arguments : '$event'
|
mouseleave(event)
|
Defined in src/tooltip/tooltip.component.ts:105
|
handleFocus | ||||||
handleFocus(event: Event)
|
||||||
Decorators :
@HostListener('focusin', ['$event'])
|
||||||
Defined in src/tooltip/tooltip.component.ts:124
|
||||||
Parameters :
Returns :
void
|
handleFocusOut | ||||||
handleFocusOut(event: Event)
|
||||||
Decorators :
@HostListener('focusout', ['$event'])
|
||||||
Defined in src/tooltip/tooltip.component.ts:129
|
||||||
Parameters :
Returns :
void
|
hostkeys | ||||||
hostkeys(event: KeyboardEvent)
|
||||||
Decorators :
@HostListener('keyup', ['$event'])
|
||||||
Defined in src/tooltip/tooltip.component.ts:115
|
||||||
Parameters :
Returns :
void
|
isTemplate | ||||
isTemplate(value)
|
||||
Defined in src/tooltip/tooltip.component.ts:133
|
||||
Parameters :
Returns :
boolean
|
mouseenter | ||||
mouseenter(event)
|
||||
Decorators :
@HostListener('mouseenter', ['$event'])
|
||||
Defined in src/tooltip/tooltip.component.ts:95
|
||||
Parameters :
Returns :
void
|
mouseleave | ||||
mouseleave(event)
|
||||
Decorators :
@HostListener('mouseleave', ['$event'])
|
||||
Defined in src/tooltip/tooltip.component.ts:105
|
||||
Parameters :
Returns :
void
|
ngAfterContentChecked |
ngAfterContentChecked()
|
Defined in src/tooltip/tooltip.component.ts:173
|
Check for any changes in the projected content & apply accessibility attribute if needed
Returns :
void
|
ngOnChanges | ||||||
ngOnChanges(changes: SimpleChanges)
|
||||||
Inherited from
PopoverContainer
|
||||||
Defined in
PopoverContainer:141
|
||||||
Close the popover and reopen it with updated values without emitting an event
Parameters :
Returns :
void
|
cleanUp |
cleanUp()
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:285
|
Clean up
Returns :
void
|
handleChange | ||||||||||||
handleChange(open: boolean, event?: Event)
|
||||||||||||
Inherited from
PopoverContainer
|
||||||||||||
Defined in
PopoverContainer:137
|
||||||||||||
Handles emitting open/close event
Parameters :
Returns :
void
|
initializeReferences |
initializeReferences()
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:264
|
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:260
|
Handle initialization of element
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:278
|
Clean up
Returns :
void
|
recomputePosition |
recomputePosition()
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:185
|
Compute position of tooltip when autoAlign is enabled
Returns :
void
|
roundByDPR | ||||
roundByDPR(value)
|
||||
Inherited from
PopoverContainer
|
||||
Defined in
PopoverContainer:177
|
||||
Parameters :
Returns :
number
|
updateAlignmentClass |
updateAlignmentClass(newAlignment: string, previousAlignment?: string)
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:296
|
Replace existing previous alignment class with new
Returns :
void
|
Private timeoutId |
Type : any
|
Defined in src/tooltip/tooltip.component.ts:81
|
tooltipClass |
Default value : true
|
Decorators :
@HostBinding('class.cds--tooltip')
|
Defined in src/tooltip/tooltip.component.ts:55
|
Static tooltipCount |
Type : number
|
Default value : 0
|
Defined in src/tooltip/tooltip.component.ts:53
|
wrapper |
Type : ElementRef<HTMLSpanElement>
|
Decorators :
@ViewChild('contentWrapper')
|
Defined in src/tooltip/tooltip.component.ts:79
|
_align |
Type : Placement
|
Default value : "bottom"
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:84
|
Readonly alignmentClassPrefix |
Type : string
|
Default value : "cds--popover--"
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:85
|
Protected caretHeight |
Type : number
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:122
|
Protected caretOffset |
Type : number
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:121
|
Protected caretRef |
Type : HTMLElement
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:120
|
containerClass |
Default value : true
|
Decorators :
@HostBinding('class.cds--popover-container')
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:116
|
Protected popoverContentRef |
Type : HTMLElement
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:119
|
Protected unmountFloatingElement |
Type : Function
|
Inherited from
PopoverContainer
|
Defined in
PopoverContainer:123
|
import {
AfterContentChecked,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
HostBinding,
HostListener,
Input,
NgZone,
OnChanges,
Renderer2,
SimpleChanges,
TemplateRef,
ViewChild
} 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--basic)
*/
@Component({
selector: "cds-tooltip, ibm-tooltip",
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<span #contentWrapper>
<ng-content></ng-content>
</span>
<span
*ngIf="description"
class="cds--popover"
[id]="id"
[attr.aria-hidden]="!isOpen"
role="tooltip">
<ng-container *ngIf="!disabled">
<span class="cds--popover-content cds--tooltip-content">
<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>
</ng-container>
</span>
`
})
export class Tooltip extends PopoverContainer implements OnChanges, AfterContentChecked {
static tooltipCount = 0;
@HostBinding("class.cds--tooltip") tooltipClass = true;
@Input() id = `tooltip-${Tooltip.tooltipCount++}`;
/**
* Set delay before tooltip is shown
*/
@Input() enterDelayMs = 100;
/**
* Set delay when tooltip disappears
*/
@Input() leaveDelayMs = 300;
/**
* Prevent tooltip from showing, used by icon button
*/
@Input() disabled = false;
/**
* 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;
@ViewChild("contentWrapper") wrapper: ElementRef<HTMLSpanElement>;
private timeoutId: any; // it should be number, but setTimeout below is matching the NodeJs type instead of the JS type
constructor(
protected elementRef: ElementRef,
protected ngZone: NgZone,
protected renderer: Renderer2,
protected changeDetectorRef: ChangeDetectorRef
) {
super(elementRef, ngZone, renderer, changeDetectorRef);
this.highContrast = true;
this.dropShadow = false;
}
@HostListener("mouseenter", ["$event"])
mouseenter(event) {
// If a mouseleave is triggered before the tooltip is displayed (before setTimeout of mouseenter completes)
// we trigger the mouseleave only avoiding having to unecessary show the tooltip
clearTimeout(this.timeoutId);
this.timeoutId = setTimeout(() => {
this.handleChange(true, event);
}, this.enterDelayMs);
}
@HostListener("mouseleave", ["$event"])
mouseleave(event) {
// If a mouseleave is triggered before the tooltip is displayed (before setTimeout of mouseenter completes)
// we trigger the mouseleave only avoiding having to unecessary show the tooltip
clearTimeout(this.timeoutId);
this.timeoutId = setTimeout(() => {
this.handleChange(false, event);
}, this.leaveDelayMs);
}
@HostListener("keyup", ["$event"])
hostkeys(event: KeyboardEvent) {
if (open && event.key === "Escape") {
event.stopPropagation();
this.handleChange(false, event);
}
}
// We are not focusing on entire popover, only the trigger
@HostListener("focusin", ["$event"])
handleFocus(event: Event) {
this.handleChange(true, event);
}
@HostListener("focusout", ["$event"])
handleFocusOut(event: Event) {
this.handleChange(false, event);
}
isTemplate(value) {
return value instanceof TemplateRef;
}
/**
* Close the popover and reopen it with updated values without emitting an event
* @param changes
*/
ngOnChanges(changes: SimpleChanges): void {
// Close and reopen the popover, handle alignment/programmatic open/close
const originalState = this.isOpen;
this.handleChange(false);
// Ignore first change since content is not initialized
if (
(changes.autoAlign && !changes.autoAlign.firstChange)
|| (changes.disabled && !changes.disabled.firstChange && !changes.disabled.currentValue)
// If description is set to empty string when open & autoAlign is true then set to a new value
// positioning of popover is broken because popover content ref/caret no longer exists
|| changes.description
) {
/**
* When `disabled` is `true`, popover content node is removed. So when re-enabling `disabled`,
* we manually update view so querySelector can detect the popover content node.
* Otherwise, the position of the popover will be incorrect when autoAlign is enabled.
*/
this.changeDetectorRef.detectChanges();
// Reset the inline styles
this.popoverContentRef = this.elementRef.nativeElement.querySelector(".cds--popover-content");
this.popoverContentRef?.setAttribute("style", "");
this.caretRef = this.elementRef.nativeElement.querySelector("span.cds--popover-caret");
}
this.handleChange(originalState);
}
/**
* Check for any changes in the projected content & apply accessibility attribute if needed
*/
ngAfterContentChecked() {
if (this.wrapper) {
const buttonElement = this.wrapper.nativeElement.querySelector("button");
if (buttonElement && !buttonElement.getAttribute("aria-labelledby")) {
buttonElement.setAttribute("aria-labelledby", this.id);
}
}
}
}