Skip to content

Commit 129b91a

Browse files
committed
feat: 🎸 introduce injectTippyRef
1 parent a5cfcb1 commit 129b91a

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
import { makeEnvironmentProviders } from '@angular/core';
2-
import { TIPPY_CONFIG, TippyConfig } from './tippy.types';
1+
import { inject, makeEnvironmentProviders } from '@angular/core';
2+
import { TIPPY_CONFIG, TIPPY_REF, TippyConfig, TippyInstance } from './tippy.types';
33

44
export function provideTippyConfig(config: Partial<TippyConfig> = {}) {
55
return makeEnvironmentProviders([{ provide: TIPPY_CONFIG, useValue: config }]);
66
}
7+
8+
export function injectTippyRef(): TippyInstance {
9+
const instance = inject(TIPPY_REF, { optional: true });
10+
11+
if (instance) {
12+
return instance;
13+
}
14+
15+
throw new Error('tp is not provided in the current context or on one of its ancestors');
16+
}

projects/ngneat/helipopper/src/lib/tippy.directive.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,21 +345,24 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
345345

346346
protected resolveContent(instance: TippyInstance) {
347347
if (!this.viewOptions$ && !isString(this.content)) {
348+
const injector = Injector.create({
349+
providers: [
350+
{
351+
provide: TIPPY_REF,
352+
useValue: this.instance,
353+
},
354+
],
355+
parent: this.injector,
356+
});
348357
if (isComponent(this.content)) {
349358
this.instance.data = this.data;
359+
350360
this.viewOptions$ = {
351-
injector: Injector.create({
352-
providers: [
353-
{
354-
provide: TIPPY_REF,
355-
useValue: this.instance,
356-
},
357-
],
358-
parent: this.injector,
359-
}),
361+
injector,
360362
};
361363
} else if (isTemplateRef(this.content)) {
362364
this.viewOptions$ = {
365+
injector,
363366
context: {
364367
$implicit: this.hide.bind(this),
365368
data: this.data,

projects/ngneat/helipopper/src/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export { tooltipVariation, popperVariation, withContextMenuVariation } from './l
33
export { TippyService } from './lib/tippy.service';
44
export { inView, overflowChanges } from './lib/utils';
55
export { ExtendedTippyInstance, TippyInstance, TIPPY_REF, TippyConfig, TIPPY_CONFIG } from './lib/tippy.types';
6-
export { provideTippyConfig } from './lib/providers';
6+
export { provideTippyConfig, injectTippyRef } from './lib/providers';

src/app/example/example.component.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { Component, Inject, OnDestroy, OnInit } from '@angular/core';
22
import { TIPPY_REF, TippyInstance } from '@ngneat/helipopper';
3+
import { injectTippyRef } from 'projects/ngneat/helipopper/src/lib/providers';
34

45
@Component({
56
selector: 'app-example',
67
templateUrl: './example.component.html',
7-
styleUrls: ['./example.component.scss']
8+
styleUrls: ['./example.component.scss'],
89
})
910
export class ExampleComponent implements OnInit, OnDestroy {
1011
name: string;
12+
tippy = injectTippyRef();
1113

12-
constructor(@Inject(TIPPY_REF) tippy: TippyInstance) {
13-
console.log(tippy);
14+
constructor() {
15+
const { name = 'world!' } = this.tippy.data ?? {};
1416

15-
const { name = 'world!' } = tippy.data ?? {};
17+
setTimeout(() => {
18+
this.tippy.hide();
19+
}, 1000);
1620

1721
this.name = name;
1822
}

src/app/playground/playground.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ <h6>Custom Template</h6>
8484
</div>
8585

8686
<ng-template #tpl let-hide>
87+
<app-example />
8788
<div *ngFor="let item of tooltipPositions" class="positions">
8889
{{ item }}
8990
</div>

0 commit comments

Comments
 (0)