-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10efb71
commit d2066de
Showing
5 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
Empty file.
15 changes: 15 additions & 0 deletions
15
libs/ui/elements/src/lib/max-lines/max-lines.component.html
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,15 @@ | ||
<div | ||
#container | ||
class="max-lines overflow-hidden transition-[max-height] duration-300" | ||
[ngClass]="isExpanded ? 'ease-in' : 'ease-out'" | ||
[style.maxHeight]="maxHeight" | ||
> | ||
<ng-content></ng-content> | ||
</div> | ||
<div | ||
*ngIf="showToggleButton" | ||
(click)="toggleDisplay()" | ||
class="text-primary cursor-pointer pt-2.5" | ||
> | ||
{{ isExpanded ? 'Show Less' : 'Read More' }} | ||
</div> |
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,83 @@ | ||
import { | ||
Component, | ||
Input, | ||
ElementRef, | ||
ChangeDetectionStrategy, | ||
AfterViewInit, | ||
ViewChild, | ||
OnDestroy, | ||
ChangeDetectorRef, | ||
} from '@angular/core' | ||
|
||
@Component({ | ||
selector: 'gn-ui-max-lines', | ||
templateUrl: './max-lines.component.html', | ||
styleUrls: ['./max-lines.component.css'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class MaxLinesComponent implements AfterViewInit, OnDestroy { | ||
@Input() maxLines = 6 | ||
|
||
isExpanded = false | ||
maxHeight = '' | ||
showToggleButton = false | ||
observer: ResizeObserver | ||
|
||
@ViewChild('container') container!: ElementRef | ||
|
||
constructor(private cdr: ChangeDetectorRef) {} | ||
|
||
ngAfterViewInit() { | ||
this.calculateMaxHeight() | ||
|
||
this.observer = new ResizeObserver((mutations) => { | ||
mutations.forEach(() => { | ||
this.calculateMaxHeight() | ||
}) | ||
}) | ||
|
||
this.observer.observe(this.container.nativeElement.children[0]) | ||
} | ||
|
||
toggleDisplay() { | ||
this.isExpanded = !this.isExpanded | ||
this.calculateMaxHeight() | ||
} | ||
|
||
calculateMaxHeight() { | ||
const containerElement = this.container.nativeElement | ||
const contentElement = containerElement.children[0] | ||
const contentHeight = contentElement.getBoundingClientRect().height | ||
|
||
if (contentHeight) { | ||
if (contentHeight > this.maxLines * this.getLineHeight(contentElement)) { | ||
this.showToggleButton = true | ||
|
||
this.maxHeight = this.isExpanded | ||
? `${contentHeight}px` | ||
: `${this.maxLines * this.getLineHeight(contentElement)}px` | ||
} else { | ||
this.showToggleButton = false | ||
this.maxHeight = `${contentHeight}px` | ||
} | ||
containerElement.setAttribute( | ||
'style', | ||
`height: ${this.maxHeight}; max-height: ${this.maxHeight}; overflow: hidden` | ||
) | ||
|
||
this.cdr.detectChanges() | ||
} | ||
} | ||
|
||
getLineHeight(element: HTMLElement): number { | ||
const computedStyle = window.getComputedStyle(element) | ||
const lineHeight = parseFloat(computedStyle.lineHeight) | ||
const fontSize = parseFloat(computedStyle.fontSize || '14') | ||
const result = isNaN(lineHeight) ? fontSize * 1.2 : lineHeight // Use a default if line height is not specified | ||
return result | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.observer.unobserve(this.container.nativeElement.children[0]) | ||
} | ||
} |
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