All files / components/line-clamp line-clamp.component.ts

75% Statements 15/20
50% Branches 1/2
60% Functions 3/5
82.35% Lines 14/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521x                     1x                       1x 11x 11x   11x 11x   11x   11x     11x 11x   11x                 13x 13x      
import {
    afterNextRender,
    ChangeDetectionStrategy,
    Component,
    DestroyRef,
    ElementRef,
    inject,
    input,
    signal,
    viewChild,
} from '@angular/core';
import { MatTooltip } from '@angular/material/tooltip';
 
@Component({
    selector: 'ui-line-clamp',
    imports: [MatTooltip],
    templateUrl: './line-clamp.component.html',
    styleUrl: './line-clamp.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
    host: {
        '[style.--lines]': 'lines()',
    },
})
export class LineClampComponent {
    readonly lines = input(2);
    readonly tooltip = input('');
 
    private readonly _isTruncated = signal(false);
    protected readonly contentEl = viewChild.required<ElementRef<HTMLElement>>('content');
 
    private readonly destroyRef = inject(DestroyRef);
 
    readonly isTruncated = this._isTruncated.asReadonly();
 
    constructor() {
        afterNextRender(() => {
            this.checkTruncation();
 
            Iif (typeof ResizeObserver !== 'undefined') {
                const observer = new ResizeObserver(() => this.checkTruncation());
                observer.observe(this.contentEl().nativeElement);
                this.destroyRef.onDestroy(() => observer.disconnect());
            }
        });
    }
 
    private checkTruncation(): void {
        const el = this.contentEl().nativeElement;
        this._isTruncated.set(el.scrollHeight > el.clientHeight);
    }
}