11import { useAnimation } from '@angular/animations' ;
2- import { Directive , OnInit , OnDestroy , Output , ElementRef , Optional , ViewContainerRef , HostListener , Input , EventEmitter , booleanAttribute , TemplateRef } from '@angular/core' ;
2+ import { Directive , OnInit , OnDestroy , Output , ElementRef , Optional , ViewContainerRef , HostListener , Input , EventEmitter , booleanAttribute , TemplateRef , ComponentRef , Renderer2 } from '@angular/core' ;
33import { Subject } from 'rxjs' ;
44import { takeUntil } from 'rxjs/operators' ;
55import { IgxNavigationService } from '../../core/navigation' ;
@@ -8,6 +8,7 @@ import { AutoPositionStrategy, HorizontalAlignment, PositionSettings } from '../
88import { IgxToggleActionDirective } from '../toggle/toggle.directive' ;
99import { IgxTooltipComponent } from './tooltip.component' ;
1010import { IgxTooltipDirective } from './tooltip.directive' ;
11+ import { IgxTooltipCloseButtonComponent } from './tooltip-close-button.component' ;
1112import { fadeOut , scaleInCenter } from 'igniteui-angular/animations' ;
1213
1314export interface ITooltipShowEventArgs extends IBaseEventArgs {
@@ -41,6 +42,11 @@ export interface ITooltipHideEventArgs extends IBaseEventArgs {
4142 standalone : true
4243} )
4344export class IgxTooltipTargetDirective extends IgxToggleActionDirective implements OnInit , OnDestroy {
45+
46+ private _closeButtonRef ?: ComponentRef < IgxTooltipCloseButtonComponent > ;
47+ private _closeTemplate : TemplateRef < any > ;
48+ private _sticky = false ;
49+
4450 /**
4551 * Gets/sets the amount of milliseconds that should pass before showing the tooltip.
4652 *
@@ -96,11 +102,11 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
96102 */
97103 @Input ( )
98104 public set disableArrow ( value : boolean ) {
99- this . target . _disableArrow = value ;
105+ this . target . disableArrow = value ;
100106 }
101107
102108 public get disableArrow ( ) : boolean {
103- return this . target . _disableArrow ;
109+ return this . target . disableArrow ;
104110 }
105111
106112 /**
@@ -123,13 +129,20 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
123129 */
124130 @Input ( )
125131 public set sticky ( value : boolean ) {
126- this . target . _sticky = value ;
132+ const changed = this . _sticky !== value ;
133+ this . _sticky = value ;
134+
135+ if ( changed ) {
136+ this . _evaluateCloseButtonState ( ) ;
137+ this . target . role = value ? "status" : "tooltip"
138+ }
127139 } ;
128140
129141 public get sticky ( ) : boolean {
130- return this . target . _sticky ;
142+ return this . _sticky ;
131143 }
132144
145+
133146 /**
134147 * Allows full control over the appearance of the close button inside the tooltip.
135148 *
@@ -152,17 +165,12 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
152165 * ```
153166 */
154167 @Input ( 'closeButtonTemplate' )
155- public set customCloseTemplate ( value : TemplateRef < any > ) {
156- this . target . _customCloseTemplate = value ;
157- if ( value ) {
158- this . target . renderCustomCloseTemplate ( ) ;
159- } else {
160- this . target . appendDefaultCloseIcon ( ) ;
161- }
168+ public set closeTemplate ( value : TemplateRef < any > ) {
169+ this . _closeTemplate = value ;
170+ this . _evaluateCloseButtonState ( ) ;
162171 }
163-
164- public get customCloseTemplate ( ) : TemplateRef < any > | undefined {
165- return this . target . _customCloseTemplate ;
172+ public get closeTemplate ( ) : TemplateRef < any > | undefined {
173+ return this . _closeTemplate ;
166174 }
167175
168176 /**
@@ -280,7 +288,7 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
280288 private _isForceClosed = false ;
281289
282290 constructor ( private _element : ElementRef ,
283- @Optional ( ) private _navigationService : IgxNavigationService , private _viewContainerRef : ViewContainerRef ) {
291+ @Optional ( ) private _navigationService : IgxNavigationService , private _viewContainerRef : ViewContainerRef , private _renderer : Renderer2 ) {
284292 super ( _element , _navigationService ) ;
285293 }
286294
@@ -358,7 +366,7 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
358366 const positionSettings : PositionSettings = {
359367 horizontalDirection : HorizontalAlignment . Center ,
360368 horizontalStartPoint : HorizontalAlignment . Center ,
361- openAnimation : useAnimation ( scaleInCenter , { params : { duration : '150ms ' } } ) ,
369+ openAnimation : useAnimation ( scaleInCenter , { params : { duration : '1150ms ' } } ) ,
362370 closeAnimation : useAnimation ( fadeOut , { params : { duration : '75ms' } } )
363371 } ;
364372
@@ -499,4 +507,45 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
499507 this . _isForceClosed = true ;
500508 }
501509 }
510+
511+
512+ private _evaluateCloseButtonState ( ) : void {
513+ if ( ! this . target || ! ( this . target instanceof IgxTooltipDirective ) ) {
514+ return ;
515+ }
516+
517+ if ( this . _sticky ) {
518+ this . _removeCloseButton ( ) ;
519+ this . _createCloseTemplate ( this . _closeTemplate ) ;
520+ } else {
521+ this . _removeCloseButton ( ) ;
522+ }
523+ }
524+
525+
526+ private _createCloseTemplate ( template ?: TemplateRef < any > | undefined ) {
527+ if ( this . target instanceof IgxTooltipDirective ) {
528+
529+ this . _closeTemplate = template ;
530+
531+ const componentRef = this . _viewContainerRef . createComponent ( IgxTooltipCloseButtonComponent ) ;
532+ this . _closeButtonRef = componentRef ;
533+
534+ componentRef . instance . customTemplate = template ;
535+ componentRef . instance . clicked . pipe ( takeUntil ( this . _destroy$ ) ) . subscribe ( ( ) => {
536+ this . hideTooltip ( ) ;
537+ } ) ;
538+
539+ if ( this . target . element instanceof HTMLElement ) {
540+ this . _renderer . appendChild ( this . target . element , componentRef . location . nativeElement ) ;
541+ }
542+ }
543+ }
544+
545+ private _removeCloseButton ( ) {
546+ if ( this . _closeButtonRef ) {
547+ this . _closeButtonRef . destroy ( ) ;
548+ this . _closeButtonRef = undefined ;
549+ }
550+ }
502551}
0 commit comments