-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from arturovt/feat/add-loading
feat: allow lazy-loading `tippy.js`
- Loading branch information
Showing
9 changed files
with
155 additions
and
92 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type tippy from 'tippy.js'; | ||
import { inject, Injectable, NgZone } from '@angular/core'; | ||
import { defer, from, map, type Observable, of, shareReplay } from 'rxjs'; | ||
|
||
import { TIPPY_CONFIG, type TippyProps } from './tippy.types'; | ||
|
||
// We need to use `isPromise` instead of checking whether | ||
// `value instanceof Promise`. In zone.js patched environments, `global.Promise` | ||
// is the `ZoneAwarePromise`. | ||
// `import(...)` returns a native promise (not a `ZoneAwarePromise`), causing | ||
// `instanceof` check to be falsy. | ||
function isPromise<T>(value: any): value is Promise<T> { | ||
return typeof value?.then === 'function'; | ||
} | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class TippyFactory { | ||
private readonly _ngZone = inject(NgZone); | ||
|
||
private readonly _config = inject(TIPPY_CONFIG); | ||
|
||
private _tippyImpl$: Observable<typeof tippy> | null = null; | ||
|
||
/** | ||
* This returns an observable because the user should provide a `loader` | ||
* function, which may return a promise if the tippy.js library is to be | ||
* loaded asynchronously. | ||
*/ | ||
create(target: HTMLElement, props?: Partial<TippyProps>) { | ||
// We use `shareReplay` to ensure that subsequent emissions are | ||
// synchronous and to avoid triggering the `defer` callback repeatedly | ||
// when new subscribers arrive. | ||
this._tippyImpl$ ||= defer(() => { | ||
const maybeTippy = this._ngZone.runOutsideAngular(() => this._config.loader()); | ||
return isPromise(maybeTippy) ? from(maybeTippy).pipe(map((tippy) => tippy.default)) : of(maybeTippy); | ||
}).pipe(shareReplay()); | ||
|
||
return this._tippyImpl$.pipe( | ||
map((tippy) => { | ||
return this._ngZone.runOutsideAngular(() => tippy(target, props)); | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.