File

src/toggletip/toggletip.component.ts

Description

Get started with importing the module:

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

See demo

Extends

PopoverContainer

Implements

AfterViewInit OnDestroy

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
HostListeners

Constructor

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

Inputs

id
Type : string
Default value : `tooltip-${Toggletip.toggletipCounter++}`
isOpen
Type : boolean
Default value : false
Inherited from PopoverContainer
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

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--toggletip
Type : boolean
Default value : true
class.cds--popover-container
Type : boolean
Default value : true
Inherited from PopoverContainer

HostListeners

keyup
Arguments : '$event'
keyup(event: KeyboardEvent)

Methods

Private handleExpansion
handleExpansion(state, event: Event)
Parameters :
Name Type Optional Default value
state No false
event Event No
Returns : void
handleFocusOut
handleFocusOut(event)
Parameters :
Name Optional
event No
Returns : void
hostkeys
hostkeys(event: KeyboardEvent)
Decorators :
@HostListener('keyup', ['$event'])
Parameters :
Name Type Optional
event KeyboardEvent No
Returns : void
ngAfterViewInit
ngAfterViewInit()
Inherited from PopoverContainer
Returns : void
ngOnDestroy
ngOnDestroy()
Inherited from PopoverContainer
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
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
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

btn
Type : ElementRef
Decorators :
@ContentChild(ToggletipButton, {read: ElementRef})
documentClick
Default value : this.handleFocusOut.bind(this)
Private subscription
Type : Subscription
toggletipClass
Default value : true
Decorators :
@HostBinding('class.cds--toggletip')
Static toggletipCounter
Type : number
Default value : 0
_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 {
	AfterViewInit,
	ChangeDetectionStrategy,
	ChangeDetectorRef,
	Component,
	ContentChild,
	ElementRef,
	HostBinding,
	HostListener,
	Input,
	NgZone,
	OnDestroy,
	Renderer2
} from "@angular/core";
import { fromEvent, Subscription } from "rxjs";
import { PopoverContainer } from "carbon-components-angular/popover";
import { ToggletipButton } from "./toggletip-button.directive";

/**
 * Get started with importing the module:
 *
 * ```typescript
 * import { ToggletipModule } from 'carbon-components-angular';
 * ```
 *
 * [See demo](../../?path=/story/components-toggletip--basic)
 */
@Component({
	selector: "cds-toggletip, ibm-toggletip",
	changeDetection: ChangeDetectionStrategy.OnPush,
	template: `
		<ng-content select="[cdsToggletipButton]"></ng-content>
		<cds-popover-content [attr.id]="id" aria-live="polite">
			<ng-content select="[cdsToggletipContent]"></ng-content>
		</cds-popover-content>
	`
})
export class Toggletip extends PopoverContainer implements AfterViewInit, OnDestroy {
	static toggletipCounter = 0;

	@Input() id = `tooltip-${Toggletip.toggletipCounter++}`;

	@HostBinding("class.cds--toggletip") toggletipClass = true;
	@HostBinding("class.cds--toggletip--open") @Input() isOpen = false;

	@ContentChild(ToggletipButton, { read: ElementRef }) btn!: ElementRef;

	documentClick = this.handleFocusOut.bind(this);
	private subscription: Subscription;

	constructor(
		protected hostElement: ElementRef,
		protected ngZone: NgZone,
		protected renderer: Renderer2,
		protected changeDetectorRef: ChangeDetectorRef
	) {
		super(hostElement, ngZone, renderer, changeDetectorRef);
		this.highContrast = true;
		this.dropShadow = false;
	}

	ngAfterViewInit(): void {
		this.initializeReferences();

		// Listen for click events on trigger
		this.subscription = fromEvent(this.btn.nativeElement, "click")
			.subscribe((event: Event) => {
				// Add/Remove event listener based on isOpen to improve performance when there
				// are a lot of toggletips
				if (this.isOpen) {
					document.removeEventListener("click", this.documentClick);
				} else {
					document.addEventListener("click", this.documentClick);
				}

				this.handleExpansion(!this.isOpen, event);
			});

		// Toggletip is open on initial render, add 'click' event listener to document so users can close
		if (this.isOpen) {
			document.addEventListener("click", this.documentClick);
		}

		if (this.btn) {
			this.renderer.setAttribute(this.btn.nativeElement, "aria-controls", this.id);
		}
	}

	@HostListener("keyup", ["$event"])
	hostkeys(event: KeyboardEvent) {
		if (open && event.key === "Escape") {
			event.stopPropagation();
			this.handleExpansion(false, event);
		}
	}

	handleFocusOut(event) {
		if (!this.hostElement.nativeElement.contains(event.target)) {
			this.handleExpansion(false, event);
		}
	}

	ngOnDestroy(): void {
		this.subscription.unsubscribe();
	}

	private handleExpansion(state = false, event: Event) {
		this.handleChange(state, event);
		if (this.btn) {
			this.renderer.setAttribute(this.btn.nativeElement, "aria-expanded", this.isOpen.toString());
		}
	}
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""