-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce ishBrowserLazyView directive (#1654)
- Loading branch information
Showing
5 changed files
with
123 additions
and
52 deletions.
There are no files selected for viewing
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,59 @@ | ||
import { | ||
DestroyRef, | ||
Directive, | ||
ElementRef, | ||
EmbeddedViewRef, | ||
Host, | ||
OnInit, | ||
TemplateRef, | ||
ViewContainerRef, | ||
inject, | ||
} from '@angular/core'; | ||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||
import { Subject, takeUntil } from 'rxjs'; | ||
|
||
import { fromIntersectionObserver } from './intersection-observer-util'; | ||
|
||
function findElement(element: { parentElement: HTMLElement } | HTMLElement): HTMLElement { | ||
if (element instanceof HTMLElement) { | ||
return element; | ||
} | ||
if (element.parentElement) { | ||
return findElement(element.parentElement); | ||
} | ||
} | ||
|
||
@Directive({ | ||
selector: '[ishBrowserLazyView]', | ||
}) | ||
export class BrowserLazyViewDirective implements OnInit { | ||
private view: EmbeddedViewRef<unknown>; | ||
|
||
private destroyRef = inject(DestroyRef); | ||
private viewCreated$ = new Subject<void>(); | ||
|
||
constructor( | ||
private viewContainer: ViewContainerRef, | ||
private template: TemplateRef<unknown>, | ||
@Host() private element: ElementRef | ||
) {} | ||
|
||
ngOnInit() { | ||
if (!SSR) { | ||
const element = findElement(this.element.nativeElement); | ||
if (!element) { | ||
console.warn('No element found for BrowserLazyViewDirective'); | ||
return; | ||
} | ||
fromIntersectionObserver(element) | ||
.pipe(takeUntil(this.viewCreated$), takeUntilDestroyed(this.destroyRef)) | ||
.subscribe(status => { | ||
if (status === 'Visible' && !this.view) { | ||
this.view = this.viewContainer.createEmbeddedView(this.template); | ||
this.view.markForCheck(); | ||
this.viewCreated$.next(); | ||
} | ||
}); | ||
} | ||
} | ||
} |
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,58 @@ | ||
import { Observable, Subject, debounceTime, filter } from 'rxjs'; | ||
|
||
export type IntersectionStatus = 'Visible' | 'Pending' | 'NotVisible'; | ||
|
||
export const fromIntersectionObserver = ( | ||
element: HTMLElement, | ||
config: IntersectionObserverInit = { | ||
root: undefined, | ||
rootMargin: '0px', | ||
threshold: undefined, | ||
}, | ||
debounce = 0 | ||
) => | ||
new Observable<IntersectionStatus>(subscriber => { | ||
const subject$ = new Subject<{ | ||
entry: IntersectionObserverEntry; | ||
observer: IntersectionObserver; | ||
}>(); | ||
|
||
const intersectionObserver = new IntersectionObserver((entries, observer) => { | ||
entries.forEach(entry => subject$.next({ entry, observer })); | ||
}, config); | ||
|
||
subject$.subscribe(() => { | ||
subscriber.next('Pending'); | ||
}); | ||
|
||
subject$.pipe(debounceTime(debounce), filter(Boolean)).subscribe(async ({ entry }) => { | ||
const isEntryVisible = await isVisible(entry.target as HTMLElement); | ||
|
||
if (isEntryVisible) { | ||
subscriber.next('Visible'); | ||
} else { | ||
subscriber.next('NotVisible'); | ||
} | ||
}); | ||
|
||
intersectionObserver.observe(element); | ||
|
||
return { | ||
unsubscribe() { | ||
intersectionObserver.disconnect(); | ||
// eslint-disable-next-line ban/ban, rxjs/no-subject-unsubscribe | ||
subject$.unsubscribe(); | ||
}, | ||
}; | ||
}); | ||
|
||
async function isVisible(element: HTMLElement) { | ||
return new Promise(resolve => { | ||
const observer = new IntersectionObserver(([entry]) => { | ||
resolve(entry.isIntersecting); | ||
observer.disconnect(); | ||
}); | ||
|
||
observer.observe(element); | ||
}); | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/app/extensions/tacton/pages/configure/tacton-group/tacton-group.component.ts
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