forked from radix-ng/primitives
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip-close.directive.ts
47 lines (43 loc) · 1.36 KB
/
tooltip-close.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Directive, effect, ElementRef, forwardRef, inject, Renderer2, untracked } from '@angular/core';
import { RdxTooltipCloseToken } from './tooltip-close.token';
import { injectTooltipRoot } from './tooltip-root.inject';
/**
* TODO: to be removed? But it seems to be useful when controlled from outside
*/
@Directive({
selector: '[rdxTooltipClose]',
host: {
type: 'button',
'(click)': 'rootDirective.handleClose(true)'
},
providers: [
{
provide: RdxTooltipCloseToken,
useExisting: forwardRef(() => RdxTooltipCloseDirective)
}
]
})
export class RdxTooltipCloseDirective {
/** @ignore */
protected readonly rootDirective = injectTooltipRoot();
/** @ignore */
readonly elementRef = inject(ElementRef);
/** @ignore */
private readonly renderer = inject(Renderer2);
constructor() {
this.onIsControlledExternallyEffect();
}
/** @ignore */
private onIsControlledExternallyEffect() {
effect(() => {
const isControlledExternally = this.rootDirective.controlledExternally()();
untracked(() => {
this.renderer.setStyle(
this.elementRef.nativeElement,
'display',
isControlledExternally ? null : 'none'
);
});
});
}
}