libs/core/src/lib/services/sticky.service.ts
Methods |
|
Public addStickyElement | ||||||
addStickyElement(element: ElementRef
|
||||||
Parameters :
Returns :
void
|
Public getStickyElementHeights |
getStickyElementHeights()
|
Returns :
any
|
import { ElementRef, Injectable } from '@angular/core';
@Injectable()
export class TalyStickyService {
private stickyElements = new Set<ElementRef<HTMLElement>>();
public addStickyElement(element: ElementRef<HTMLElement> | unknown) {
// Since the element is a result of a runtime query, we cannot guarantee that it will be of the ElementRef<HTMLElement> type.
// Therefore, a runtime type check is required
if (
!(element instanceof ElementRef) ||
typeof element?.nativeElement?.offsetHeight !== 'number'
) {
console.warn(
'Unable to register element in TalyStickyService: Missing "offsetHeight" property. Please provide a DOM element of type ElementRef<HTMLElement>.'
);
return;
}
this.stickyElements.add(element);
}
public getStickyElementHeights() {
const totalHeight = Array.from(this.stickyElements)
.map((element) => element.nativeElement.offsetHeight ?? 0)
.reduce((height, acc) => acc + height, 0);
return totalHeight;
}
}