File

src/tooltip/tooltip.component.ts

Description

Get started with importing the module:

Example :
import { TooltipModule } from 'carbon-components-angular';

See demo

Extends

PopoverContainer

Implements

OnChanges AfterContentChecked

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
HostListeners

Constructor

constructor(elementRef: ElementRef, ngZone: NgZone, renderer: Renderer2, changeDetectorRef: ChangeDetectorRef)
Parameters :
Name Type Optional
elementRef ElementRef No
ngZone NgZone No
renderer Renderer2 No
changeDetectorRef ChangeDetectorRef No

Inputs

description
Type : string | TemplateRef<any>

The string or template content to be exposed by the tooltip.

disabled
Type : boolean
Default value : false

Prevent tooltip from showing, used by icon button

enterDelayMs
Type : number
Default value : 100

Set delay before tooltip is shown

id
Type : string
Default value : `tooltip-${Tooltip.tooltipCount++}`
leaveDelayMs
Type : number
Default value : 300

Set delay when tooltip disappears

templateContext
Type : any

Optional data for templates passed as implicit context

align
Type : oldPlacement | Placement
Inherited from PopoverContainer

Set alignment of popover As of v5, oldPlacements are now deprecated in favor of Placements

When autoAlign is set to true, alignment may change for best placement

autoAlign
Type : boolean
Default value : false
Inherited from PopoverContainer

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

Show caret at the alignment position

dropShadow
Type : boolean
Default value : true
Inherited from PopoverContainer

Enable drop shadow around the popover container

highContrast
Type : boolean
Default value : false
Inherited from PopoverContainer

Enable high contrast for popover container

isOpen
Type : boolean
Default value : false
Inherited from PopoverContainer

Outputs

isOpenChange
Type : EventEmitter
Inherited from PopoverContainer

Emits an event when the state of isOpen changes. Allows isOpen to be double bound

onClose
Type : EventEmitter<Event>
Inherited from PopoverContainer

Emits an event when the dialog is closed

onOpen
Type : EventEmitter<Event>
Inherited from PopoverContainer

Emits an event when the dialog is opened

HostBindings

class.cds--tooltip
Type : boolean
Default value : true
class.cds--popover-container
Type : boolean
Default value : true
Inherited from PopoverContainer

HostListeners

focusin
Arguments : '$event'
focusin(event: Event)
focusout
Arguments : '$event'
focusout(event: Event)
keyup
Arguments : '$event'
keyup(event: KeyboardEvent)
mouseenter
Arguments : '$event'
mouseenter(event)
mouseleave
Arguments : '$event'
mouseleave(event)

Methods

handleFocus
handleFocus(event: Event)
Decorators :
@HostListener('focusin', ['$event'])
Parameters :
Name Type Optional
event Event No
Returns : void
handleFocusOut
handleFocusOut(event: Event)
Decorators :
@HostListener('focusout', ['$event'])
Parameters :
Name Type Optional
event Event No
Returns : void
hostkeys
hostkeys(event: KeyboardEvent)
Decorators :
@HostListener('keyup', ['$event'])
Parameters :
Name Type Optional
event KeyboardEvent No
Returns : void
isTemplate
isTemplate(value)
Parameters :
Name Optional
value No
Returns : boolean
mouseenter
mouseenter(event)
Decorators :
@HostListener('mouseenter', ['$event'])
Parameters :
Name Optional
event No
Returns : void
mouseleave
mouseleave(event)
Decorators :
@HostListener('mouseleave', ['$event'])
Parameters :
Name Optional
event No
Returns : void
ngAfterContentChecked
ngAfterContentChecked()

Check for any changes in the projected content & apply accessibility attribute if needed

Returns : void
ngOnChanges
ngOnChanges(changes: SimpleChanges)
Inherited from PopoverContainer

Close the popover and reopen it with updated values without emitting an event

Parameters :
Name Type Optional
changes SimpleChanges No
Returns : void
cleanUp
cleanUp()
Inherited from PopoverContainer

Clean up autoUpdate if auto alignment is enabled

Returns : void
handleChange
handleChange(open: boolean, event?: Event)
Inherited from PopoverContainer

Handles emitting open/close event

Parameters :
Name Type Optional Description
open boolean No
  • Is the popover container open
event Event Yes
  • Event
Returns : void
initializeReferences
initializeReferences()
Inherited from PopoverContainer
Returns : void
ngAfterViewInit
ngAfterViewInit()
Inherited from PopoverContainer

Handle initialization of element

Returns : void
ngOnDestroy
ngOnDestroy()
Inherited from PopoverContainer

Clean up

Returns : void
recomputePosition
recomputePosition()
Inherited from PopoverContainer

Compute position of tooltip when autoAlign is enabled

Returns : void
roundByDPR
roundByDPR(value)
Inherited from PopoverContainer
Parameters :
Name Optional
value No
Returns : number
updateAlignmentClass
updateAlignmentClass(newAlignment: string, previousAlignment?: string)
Inherited from PopoverContainer

Replace existing previous alignment class with new

Parameters :
Name Type Optional
newAlignment string No
previousAlignment string Yes
Returns : void

Properties

Private timeoutId
Type : any
tooltipClass
Default value : true
Decorators :
@HostBinding('class.cds--tooltip')
Static tooltipCount
Type : number
Default value : 0
wrapper
Type : ElementRef<HTMLSpanElement>
Decorators :
@ViewChild('contentWrapper')
_align
Type : Placement
Default value : "bottom"
Inherited from PopoverContainer
Readonly alignmentClassPrefix
Type : string
Default value : "cds--popover--"
Inherited from PopoverContainer
Protected caretHeight
Type : number
Inherited from PopoverContainer
Protected caretOffset
Type : number
Inherited from PopoverContainer
Protected caretRef
Type : HTMLElement
Inherited from PopoverContainer
containerClass
Default value : true
Decorators :
@HostBinding('class.cds--popover-container')
Inherited from PopoverContainer
Protected popoverContentRef
Type : HTMLElement
Inherited from PopoverContainer
Protected unmountFloatingElement
Type : Function
Inherited from PopoverContainer
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);
			}
		}
	}
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""