Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions projects/ngneat/helipopper/src/lib/tippy.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ const defaultTriggerTarget: TippyProps['triggerTarget'] = null;
const defaultZIndex: TippyProps['zIndex'] = 9999;
const defaultAnimation: TippyProps['animation'] = 'fade';

// Available since Angular 20.
declare const ngServerMode: boolean;

@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
selector: '[tp]',
Expand Down Expand Up @@ -124,7 +121,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnInit {
{},
{
alias: 'tpPopperOptions',
}
},
);

readonly showOnCreate = input(false, {
Expand Down Expand Up @@ -217,7 +214,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnInit {
private contentChanged = new Subject<void>();

private host = computed<HTMLElement>(
() => this.customHost() || this.hostRef.nativeElement
() => this.customHost() || this.hostRef.nativeElement,
);

// It should be a getter because computations are cached until
Expand Down Expand Up @@ -265,7 +262,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnInit {
.filter((key) => key !== 'isVisible')
.reduce(
(accumulator, key) => ({ ...accumulator, [key]: changes[key].currentValue }),
{ ...this.globalConfig.variations?.[variation] }
{ ...this.globalConfig.variations?.[variation] },
);

this.updateProps(props);
Expand All @@ -287,7 +284,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnInit {
hostInView$
.pipe(
switchMap(() => this.isOverflowing$()),
takeUntilDestroyed(this.destroyRef)
takeUntilDestroyed(this.destroyRef),
)
.subscribe((isElementOverflow) => {
this.checkOverflow(isElementOverflow);
Expand Down Expand Up @@ -339,7 +336,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnInit {
if (this.props.animation) {
this.onHidden();
}
}
},
);
} else {
this.visibilityObserverCleanup?.();
Expand Down Expand Up @@ -407,7 +404,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnInit {
},
onCreate: (instance) => {
instance.popper.classList.add(
`tippy-variation-${this.variation() || this.globalConfig.defaultVariation}`
`tippy-variation-${this.variation() || this.globalConfig.defaultVariation}`,
);
if (this.className()) {
for (const klass of normalizeClassName(this.className())) {
Expand Down Expand Up @@ -542,15 +539,15 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnInit {
bottom: event.clientY,
left: event.clientX,
right: event.clientX,
} as DOMRectReadOnly),
}) as DOMRectReadOnly,
});

this.instance.show();
};

host.addEventListener('contextmenu', onContextMenu);
this.destroyRef.onDestroy(() =>
host.removeEventListener('contextmenu', onContextMenu)
host.removeEventListener('contextmenu', onContextMenu),
);
}

Expand Down Expand Up @@ -638,8 +635,8 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnInit {

return () => cancelAnimationFrame(id);
});
})
)
}),
),
);
}

Expand Down
26 changes: 11 additions & 15 deletions projects/ngneat/helipopper/src/lib/tippy.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { inject, Inject, Injectable, Injector } from '@angular/core';
import { inject, Injectable, Injector } from '@angular/core';
import {
isComponent,
isTemplateRef,
Expand All @@ -12,7 +12,6 @@ import {
CreateOptions,
ExtendedTippyInstance,
TIPPY_CONFIG,
TippyConfig,
TippyInstance,
} from '@ngneat/helipopper/config';

Expand All @@ -22,20 +21,17 @@ import { normalizeClassName, onlyTippyProps } from './utils';

@Injectable({ providedIn: 'root' })
export class TippyService {
private readonly _injector = inject(Injector);
private readonly _globalConfig = inject(TIPPY_CONFIG, { optional: true });
private readonly _viewService = inject(ViewService);
private readonly _tippyFactory = inject(TippyFactory);

constructor(
@Inject(TIPPY_CONFIG) private globalConfig: TippyConfig,
private view: ViewService,
private injector: Injector
) {}

create<T extends Content>(
host: HTMLElement,
content: T,
options: Partial<CreateOptions> = {}
options: Partial<CreateOptions> = {},
): Observable<ExtendedTippyInstance<T>> {
const variation = options.variation || this.globalConfig.defaultVariation || '';
const variation = options.variation || this._globalConfig?.defaultVariation || '';
const config = {
onShow: (instance: ExtendedTippyInstance<T>) => {
host.setAttribute('data-tippy-open', '');
Expand All @@ -48,7 +44,7 @@ export class TippyService {
useValue: instance,
},
],
parent: options.injector || this.injector,
parent: options.injector || this._injector,
}),
};

Expand All @@ -63,7 +59,7 @@ export class TippyService {
}
}

instance.view ||= this.view.createView(content, {
instance.view ||= this._viewService.createView(content, {
...options,
...instance.$viewOptions,
}) as ResolveViewRef<T>;
Expand All @@ -80,8 +76,8 @@ export class TippyService {
}
options?.onHidden?.(instance);
},
...onlyTippyProps(this.globalConfig),
...this.globalConfig.variations?.[variation],
...onlyTippyProps(this._globalConfig),
...this._globalConfig?.variations?.[variation],
...onlyTippyProps(options),
onCreate: (instance: TippyInstance) => {
instance.popper.classList.add(`tippy-variation-${variation}`);
Expand All @@ -90,7 +86,7 @@ export class TippyService {
instance.popper.classList.add(klass);
}
}
this.globalConfig.onCreate?.(instance);
this._globalConfig?.onCreate?.(instance);
options.onCreate?.(instance);
},
};
Expand Down
10 changes: 6 additions & 4 deletions projects/ngneat/helipopper/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import type { TippyElement } from '@ngneat/helipopper/config';

import { IntersectionObserver } from './intersection-observer';

declare const ResizeObserver: any;

let supportsIntersectionObserver = false;
let supportsResizeObserver = false;

Expand All @@ -24,7 +22,7 @@ export function inView(
options: IntersectionObserverInit = {
root: null,
threshold: 0.3,
}
},
) {
const element = coerceElement(host);

Expand Down Expand Up @@ -65,7 +63,7 @@ export function overflowChanges(host: TippyElement) {

return dimensionsChanges(element).pipe(
auditTime(150),
map(() => isElementOverflow(element))
map(() => isElementOverflow(element)),
);
}

Expand All @@ -86,6 +84,10 @@ export function dimensionsChanges(target: HTMLElement) {
export function onlyTippyProps(allProps: any) {
const tippyProps: any = {};

if (!allProps) {
return tippyProps;
}

const ownProps = [
'useTextContent',
'variations',
Expand Down