Skip to content

Commit

Permalink
feat: 🎸 introduce injectTippyRef
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed Dec 13, 2023
1 parent a5cfcb1 commit 129b91a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
14 changes: 12 additions & 2 deletions projects/ngneat/helipopper/src/lib/providers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { makeEnvironmentProviders } from '@angular/core';
import { TIPPY_CONFIG, TippyConfig } from './tippy.types';
import { inject, makeEnvironmentProviders } from '@angular/core';
import { TIPPY_CONFIG, TIPPY_REF, TippyConfig, TippyInstance } from './tippy.types';

export function provideTippyConfig(config: Partial<TippyConfig> = {}) {
return makeEnvironmentProviders([{ provide: TIPPY_CONFIG, useValue: config }]);
}

export function injectTippyRef(): TippyInstance {
const instance = inject(TIPPY_REF, { optional: true });

if (instance) {
return instance;
}

throw new Error('tp is not provided in the current context or on one of its ancestors');
}
21 changes: 12 additions & 9 deletions projects/ngneat/helipopper/src/lib/tippy.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,24 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn

protected resolveContent(instance: TippyInstance) {
if (!this.viewOptions$ && !isString(this.content)) {
const injector = Injector.create({
providers: [
{
provide: TIPPY_REF,
useValue: this.instance,
},
],
parent: this.injector,
});
if (isComponent(this.content)) {
this.instance.data = this.data;

this.viewOptions$ = {
injector: Injector.create({
providers: [
{
provide: TIPPY_REF,
useValue: this.instance,
},
],
parent: this.injector,
}),
injector,
};
} else if (isTemplateRef(this.content)) {
this.viewOptions$ = {
injector,
context: {
$implicit: this.hide.bind(this),
data: this.data,
Expand Down
2 changes: 1 addition & 1 deletion projects/ngneat/helipopper/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export { tooltipVariation, popperVariation, withContextMenuVariation } from './l
export { TippyService } from './lib/tippy.service';
export { inView, overflowChanges } from './lib/utils';
export { ExtendedTippyInstance, TippyInstance, TIPPY_REF, TippyConfig, TIPPY_CONFIG } from './lib/tippy.types';
export { provideTippyConfig } from './lib/providers';
export { provideTippyConfig, injectTippyRef } from './lib/providers';
12 changes: 8 additions & 4 deletions src/app/example/example.component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { Component, Inject, OnDestroy, OnInit } from '@angular/core';
import { TIPPY_REF, TippyInstance } from '@ngneat/helipopper';
import { injectTippyRef } from 'projects/ngneat/helipopper/src/lib/providers';

@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.scss']
styleUrls: ['./example.component.scss'],
})
export class ExampleComponent implements OnInit, OnDestroy {
name: string;
tippy = injectTippyRef();

constructor(@Inject(TIPPY_REF) tippy: TippyInstance) {
console.log(tippy);
constructor() {
const { name = 'world!' } = this.tippy.data ?? {};

const { name = 'world!' } = tippy.data ?? {};
setTimeout(() => {
this.tippy.hide();
}, 1000);

this.name = name;
}
Expand Down
1 change: 1 addition & 0 deletions src/app/playground/playground.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ <h6>Custom Template</h6>
</div>

<ng-template #tpl let-hide>
<app-example />
<div *ngFor="let item of tooltipPositions" class="positions">
{{ item }}
</div>
Expand Down

0 comments on commit 129b91a

Please sign in to comment.