1
+ import { DOCUMENT } from '@angular/common' ;
1
2
import {
2
3
AfterViewInit ,
3
4
ChangeDetectorRef ,
@@ -7,32 +8,30 @@ import {
7
8
Directive ,
8
9
effect ,
9
10
ElementRef ,
10
- HostBinding ,
11
11
inject ,
12
- Inject ,
13
12
input ,
14
13
model ,
15
14
OnDestroy ,
16
15
OnInit ,
17
16
Renderer2 ,
18
17
TemplateRef ,
19
- ViewContainerRef ,
18
+ ViewContainerRef
20
19
} from '@angular/core' ;
21
- import { DOCUMENT } from '@angular/common' ;
20
+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
21
+ import { debounceTime , filter , finalize } from 'rxjs/operators' ;
22
22
import { createPopper , Instance , Options } from '@popperjs/core' ;
23
23
24
24
import { Triggers } from '../coreui.types' ;
25
- import { TooltipComponent } from './tooltip/tooltip.component' ;
26
25
import { IListenersConfig , IntersectionService , ListenersService } from '../services' ;
27
- import { debounceTime , filter , finalize } from 'rxjs/operators' ;
28
- import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
29
26
import { ElementRefDirective } from '../shared' ;
27
+ import { TooltipComponent } from './tooltip/tooltip.component' ;
30
28
31
29
@Directive ( {
32
30
selector : '[cTooltip]' ,
33
31
exportAs : 'cTooltip' ,
34
32
providers : [ ListenersService , IntersectionService ] ,
35
33
standalone : true ,
34
+ host : { '[attr.aria-describedby]' : 'ariaDescribedBy' }
36
35
} )
37
36
export class TooltipDirective implements OnDestroy , OnInit , AfterViewInit {
38
37
/**
@@ -57,7 +56,7 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
57
56
this . _popperOptions = {
58
57
...this . _popperOptions ,
59
58
placement : this . placement ( ) ,
60
- ...this . popperOptions ( ) ,
59
+ ...this . popperOptions ( )
61
60
} ;
62
61
} ) ;
63
62
@@ -70,18 +69,14 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
70
69
* @type : 'top' | 'bottom' | 'left' | 'right'
71
70
* @default : 'top'
72
71
*/
73
- readonly placement = input < 'top' | 'bottom' | 'left' | 'right' > ( 'top' , {
74
- alias : 'cTooltipPlacement' ,
75
- } ) ;
72
+ readonly placement = input < 'top' | 'bottom' | 'left' | 'right' > ( 'top' , { alias : 'cTooltipPlacement' } ) ;
76
73
77
74
/**
78
75
* ElementRefDirective for positioning the tooltip on reference element
79
76
* @type : ElementRefDirective
80
77
* @default : undefined
81
78
*/
82
- readonly reference = input < ElementRefDirective | undefined > ( undefined , {
83
- alias : 'cTooltipRef' ,
84
- } ) ;
79
+ readonly reference = input < ElementRefDirective | undefined > ( undefined , { alias : 'cTooltipRef' } ) ;
85
80
86
81
readonly referenceRef = computed ( ( ) => this . reference ( ) ?. elementRef ?? this . hostElement ) ;
87
82
@@ -101,7 +96,7 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
101
96
this . visible ( ) ? this . addTooltipElement ( ) : this . removeTooltipElement ( ) ;
102
97
} ) ;
103
98
104
- @ HostBinding ( 'attr.aria-describedby' ) get ariaDescribedBy ( ) : string | null {
99
+ get ariaDescribedBy ( ) : string | null {
105
100
return this . tooltipId ? this . tooltipId : null ;
106
101
}
107
102
@@ -115,22 +110,22 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
115
110
{
116
111
name : 'offset' ,
117
112
options : {
118
- offset : [ 0 , 5 ] ,
119
- } ,
120
- } ,
121
- ] ,
113
+ offset : [ 0 , 5 ]
114
+ }
115
+ }
116
+ ]
122
117
} ;
123
118
124
119
readonly #destroyRef = inject ( DestroyRef ) ;
120
+ readonly #document = inject ( DOCUMENT ) ;
125
121
126
122
constructor (
127
- @Inject ( DOCUMENT ) private document : Document ,
128
123
private renderer : Renderer2 ,
129
124
private hostElement : ElementRef ,
130
125
private viewContainerRef : ViewContainerRef ,
131
126
private listenersService : ListenersService ,
132
127
private changeDetectorRef : ChangeDetectorRef ,
133
- private intersectionService : IntersectionService ,
128
+ private intersectionService : IntersectionService
134
129
) { }
135
130
136
131
ngAfterViewInit ( ) : void {
@@ -158,7 +153,7 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
158
153
} ,
159
154
callbackOn : ( ) => {
160
155
this . visible . set ( true ) ;
161
- } ,
156
+ }
162
157
} ;
163
158
this . listenersService . setListeners ( config ) ;
164
159
}
@@ -176,7 +171,7 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
176
171
finalize ( ( ) => {
177
172
this . intersectionService . unobserve ( this . referenceRef ( ) ) ;
178
173
} ) ,
179
- takeUntilDestroyed ( this . #destroyRef) ,
174
+ takeUntilDestroyed ( this . #destroyRef)
180
175
)
181
176
. subscribe ( ( next ) => {
182
177
this . visible . set ( next . isIntersecting ? this . visible ( ) : false ) ;
@@ -187,7 +182,7 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
187
182
let uid = prefix ?? 'random-id' ;
188
183
do {
189
184
uid = `${ prefix } -${ Math . floor ( Math . random ( ) * 1000000 ) . toString ( 10 ) } ` ;
190
- } while ( this . document . getElementById ( uid ) ) ;
185
+ } while ( this . # document. getElementById ( uid ) ) ;
191
186
192
187
return uid ;
193
188
}
@@ -219,31 +214,30 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
219
214
this . createTooltipElement ( ) ;
220
215
}
221
216
222
- this . tooltipId = this . getUID ( 'tooltip' ) ;
223
- this . tooltipRef . instance . id = this . tooltipId ;
224
- this . tooltipRef . instance . content = this . content ( ) ?? '' ;
217
+ this . tooltipRef ?. setInput ( 'content' , this . content ( ) ?? '' ) ;
225
218
226
- this . tooltip = this . tooltipRef . location . nativeElement ;
219
+ this . tooltip = this . tooltipRef ? .location . nativeElement ;
227
220
this . renderer . addClass ( this . tooltip , 'd-none' ) ;
228
221
this . renderer . addClass ( this . tooltip , 'fade' ) ;
229
222
230
223
this . popperInstance ?. destroy ( ) ;
231
224
232
225
this . viewContainerRef . insert ( this . tooltipRef . hostView ) ;
233
- this . renderer . appendChild ( this . document . body , this . tooltip ) ;
226
+ this . renderer . appendChild ( this . # document. body , this . tooltip ) ;
234
227
235
228
this . popperInstance = createPopper ( this . referenceRef ( ) . nativeElement , this . tooltip , {
236
- ...this . popperOptionsComputed ( ) ,
229
+ ...this . popperOptionsComputed ( )
237
230
} ) ;
231
+
238
232
if ( ! this . visible ( ) ) {
239
233
this . removeTooltipElement ( ) ;
240
234
return ;
241
235
}
242
- this . renderer . removeClass ( this . tooltip , 'd-none' ) ;
243
- this . changeDetectorRef . markForCheck ( ) ;
244
-
245
236
setTimeout ( ( ) => {
246
- this . tooltipRef && ( this . tooltipRef . instance . visible = this . visible ( ) ) ;
237
+ this . tooltipId = this . getUID ( 'tooltip' ) ;
238
+ this . tooltipRef ?. setInput ( 'id' , this . tooltipId ) ;
239
+ this . renderer . removeClass ( this . tooltip , 'd-none' ) ;
240
+ this . tooltipRef ?. setInput ( 'visible' , this . visible ( ) ) ;
247
241
this . popperInstance ?. forceUpdate ( ) ;
248
242
this . changeDetectorRef ?. markForCheck ( ) ;
249
243
} , 100 ) ;
@@ -254,8 +248,8 @@ export class TooltipDirective implements OnDestroy, OnInit, AfterViewInit {
254
248
if ( ! this . tooltipRef ) {
255
249
return ;
256
250
}
257
- this . tooltipRef . instance . visible = false ;
258
- this . tooltipRef . instance . id = undefined ;
251
+ this . tooltipRef . setInput ( ' visible' , false ) ;
252
+ this . tooltipRef . setInput ( 'id' , undefined ) ;
259
253
this . changeDetectorRef . markForCheck ( ) ;
260
254
setTimeout ( ( ) => {
261
255
this . viewContainerRef ?. detach ( ) ;
0 commit comments