11import { useAnimation } from '@angular/animations' ;
2- import { Directive , OnInit , OnDestroy , Output , ElementRef , Optional , ViewContainerRef , HostListener , Input , EventEmitter , booleanAttribute , TemplateRef , ComponentRef , Renderer2 } from '@angular/core' ;
2+ import { Directive , OnInit , OnDestroy , Output , ElementRef , Optional , ViewContainerRef , HostListener , Input , EventEmitter , booleanAttribute , TemplateRef , ComponentRef , Renderer2 , AfterViewInit } from '@angular/core' ;
33import { Subject } from 'rxjs' ;
44import { takeUntil } from 'rxjs/operators' ;
55import { IgxNavigationService } from '../../core/navigation' ;
66import { IBaseEventArgs } from '../../core/utils' ;
7- import { AutoPositionStrategy , HorizontalAlignment , PositionSettings } from '../../services/public_api' ;
7+ import { PositionSettings } from '../../services/public_api' ;
88import { IgxToggleActionDirective } from '../toggle/toggle.directive' ;
99import { IgxTooltipComponent } from './tooltip.component' ;
1010import { IgxTooltipDirective } from './tooltip.directive' ;
1111import { IgxTooltipCloseButtonComponent } from './tooltip-close-button.component' ;
1212import { fadeOut , scaleInCenter } from 'igniteui-angular/animations' ;
13+ import { TooltipPlacement } from './enums' ;
14+ import { IgxTooltipPositionStrategy , PositionsMap , TooltipRegexes } from './tooltip.common' ;
15+ import { IgxDirectionality } from '../../services/direction/directionality' ;
1316
1417export interface ITooltipShowEventArgs extends IBaseEventArgs {
1518 target : IgxTooltipTargetDirective ;
@@ -41,7 +44,7 @@ export interface ITooltipHideEventArgs extends IBaseEventArgs {
4144 selector : '[igxTooltipTarget]' ,
4245 standalone : true
4346} )
44- export class IgxTooltipTargetDirective extends IgxToggleActionDirective implements OnInit , OnDestroy {
47+ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implements OnInit , AfterViewInit , OnDestroy {
4548
4649 private _closeButtonRef ?: ComponentRef < IgxTooltipCloseButtonComponent > ;
4750 private _closeTemplate : TemplateRef < any > ;
@@ -81,6 +84,32 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
8184 @Input ( )
8285 public hideDelay = 300 ;
8386
87+
88+ /**
89+ * Where to place the tooltip relative to the target element. Default value is `top`.
90+ * ```html
91+ * <igx-icon [igxTooltipTarget]="tooltipRef" placement="bottom-start">info</igx-icon>
92+ * <span #tooltipRef="tooltip" igxTooltip>Hello there, I am a tooltip!</span>
93+ * ```
94+ */
95+ @Input ( )
96+ public set placement ( value : TooltipPlacement ) {
97+ this . _placement = value ;
98+
99+ if ( this . _overlayDefaults && this . target ) {
100+ this . _overlayDefaults . positionStrategy = new IgxTooltipPositionStrategy ( this . _positionsSettingsByPlacement , value , this . offset ) ;
101+ this . target . positionArrow ( value , this . _arrowOffset ) ;
102+ }
103+ }
104+
105+ public get placement ( ) : TooltipPlacement {
106+ return this . _placement ;
107+ }
108+
109+ /** The offset of the tooltip from the target in pixels. Default value is 6. */
110+ @Input ( )
111+ public offset = 6 ;
112+
84113 /**
85114 * Controls whether the arrow element of the tooltip is rendered.
86115 * Set to true to hide the arrow. Default value is `false`.
@@ -286,9 +315,15 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
286315 private _destroy$ = new Subject < void > ( ) ;
287316 private _autoHideDelay = 180 ;
288317 private _isForceClosed = false ;
289-
290- constructor ( private _element : ElementRef ,
291- @Optional ( ) private _navigationService : IgxNavigationService , private _viewContainerRef : ViewContainerRef , private _renderer : Renderer2 ) {
318+ private _placement : TooltipPlacement = TooltipPlacement . top ;
319+
320+ constructor (
321+ private _element : ElementRef ,
322+ @Optional ( ) private _navigationService : IgxNavigationService ,
323+ private _viewContainerRef : ViewContainerRef ,
324+ private _renderer : Renderer2 ,
325+ private _dir : IgxDirectionality ,
326+ ) {
292327 super ( _element , _navigationService ) ;
293328 }
294329
@@ -363,14 +398,7 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
363398 public override ngOnInit ( ) {
364399 super . ngOnInit ( ) ;
365400
366- const positionSettings : PositionSettings = {
367- horizontalDirection : HorizontalAlignment . Center ,
368- horizontalStartPoint : HorizontalAlignment . Center ,
369- openAnimation : useAnimation ( scaleInCenter , { params : { duration : '150ms' } } ) ,
370- closeAnimation : useAnimation ( fadeOut , { params : { duration : '75ms' } } )
371- } ;
372-
373- this . _overlayDefaults . positionStrategy = new AutoPositionStrategy ( positionSettings ) ;
401+ this . _overlayDefaults . positionStrategy = new IgxTooltipPositionStrategy ( this . _positionsSettingsByPlacement , this . placement , this . offset ) ;
374402 this . _overlayDefaults . closeOnOutsideClick = false ;
375403 this . _overlayDefaults . closeOnEscape = true ;
376404
@@ -387,6 +415,13 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
387415 this . target . onHide = this . _hideOnInteraction . bind ( this ) ;
388416 }
389417
418+ /**
419+ * @hidden
420+ */
421+ public ngAfterViewInit ( ) {
422+ this . target . positionArrow ( this . placement , this . _arrowOffset ) ;
423+ }
424+
390425 /**
391426 * @hidden
392427 */
@@ -421,6 +456,36 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
421456 return Object . assign ( { } , this . _overlayDefaults , this . overlaySettings ) ;
422457 }
423458
459+ private get _positionsSettingsByPlacement ( ) : PositionSettings {
460+ const positions = PositionsMap . get ( this . placement ) ;
461+ const animations = {
462+ openAnimation : useAnimation ( scaleInCenter , { params : { duration : '150ms' } } ) ,
463+ closeAnimation : useAnimation ( fadeOut , { params : { duration : '75ms' } } )
464+ }
465+ return Object . assign ( { } , animations , positions ) ;
466+ }
467+
468+ private get _arrowOffset ( ) : number {
469+ if ( this . placement . includes ( '-' ) ) {
470+ // Horizontal start | end placement
471+ if ( TooltipRegexes . horizontalStart . test ( this . placement ) ) {
472+ return - 8 ;
473+ }
474+ if ( TooltipRegexes . horizontalEnd . test ( this . placement ) ) {
475+ return 8 ;
476+ }
477+
478+ // Vertical start | end placement
479+ if ( TooltipRegexes . start . test ( this . placement ) ) {
480+ return this . _dir . rtl ? 8 : - 8 ;
481+ }
482+ if ( TooltipRegexes . end . test ( this . placement ) ) {
483+ return this . _dir . rtl ? - 8 : 8 ;
484+ }
485+ }
486+ return 0 ;
487+ }
488+
424489 private _checkOutletAndOutsideClick ( ) : void {
425490 if ( this . outlet ) {
426491 this . _overlayDefaults . outlet = this . outlet ;
0 commit comments