forked from radix-ng/primitives
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip-anchor.directive.ts
64 lines (59 loc) · 2.18 KB
/
tooltip-anchor.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { CdkOverlayOrigin } from '@angular/cdk/overlay';
import { computed, Directive, ElementRef, forwardRef, inject } from '@angular/core';
import { injectDocument } from '@radix-ng/primitives/core';
import { RdxTooltipAnchorToken } from './tooltip-anchor.token';
import { RdxTooltipRootDirective } from './tooltip-root.directive';
import { injectTooltipRoot } from './tooltip-root.inject';
@Directive({
selector: '[rdxTooltipAnchor]',
exportAs: 'rdxTooltipAnchor',
hostDirectives: [CdkOverlayOrigin],
host: {
type: 'button',
'[attr.id]': 'name()',
'[attr.aria-haspopup]': '"dialog"',
'(click)': 'click()'
},
providers: [
{
provide: RdxTooltipAnchorToken,
useExisting: forwardRef(() => RdxTooltipAnchorDirective)
}
]
})
export class RdxTooltipAnchorDirective {
/**
* @ignore
* If outside the rootDirective then null, otherwise the rootDirective directive - with optional `true` passed in as the first param.
* If outside the rootDirective and non-null value that means the html structure is wrong - tooltip inside tooltip.
* */
protected rootDirective = injectTooltipRoot(true);
/** @ignore */
readonly elementRef = inject(ElementRef);
/** @ignore */
readonly overlayOrigin = inject(CdkOverlayOrigin);
/** @ignore */
readonly document = injectDocument();
/** @ignore */
readonly name = computed(() => `rdx-tooltip-external-anchor-${this.rootDirective?.uniqueId()}`);
/** @ignore */
click(): void {
this.emitOutsideClick();
}
/** @ignore */
setRoot(root: RdxTooltipRootDirective) {
this.rootDirective = root;
}
private emitOutsideClick() {
if (!this.rootDirective?.isOpen() || this.rootDirective?.contentDirective().onOverlayOutsideClickDisabled()) {
return;
}
const clickEvent = new MouseEvent('click', {
view: this.document.defaultView,
bubbles: true,
cancelable: true,
relatedTarget: this.elementRef.nativeElement
});
this.rootDirective?.triggerDirective().elementRef.nativeElement.dispatchEvent(clickEvent);
}
}