Skip to content

Commit

Permalink
Add component max-lines with toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
Angi-Kinas committed Dec 13, 2023
1 parent 10efb71 commit d2066de
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 6 deletions.
Empty file.
15 changes: 15 additions & 0 deletions libs/ui/elements/src/lib/max-lines/max-lines.component.html
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>
83 changes: 83 additions & 0 deletions libs/ui/elements/src/lib/max-lines/max-lines.component.ts
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])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
</p>
<div class="mb-6 md-description sm:mb-4 sm:pr-16">
<gn-ui-content-ghost ghostClass="h-32" [showContent]="fieldReady('abstract')">
<p
class="whitespace-pre-line break-words"
[innerHTML]="metadata.abstract"
*ngIf="metadata.abstract"
></p>
<gn-ui-max-lines [maxLines]="6" *ngIf="metadata.abstract">
<p
class="whitespace-pre-line break-words"
[innerHTML]="metadata.abstract"
></p>
</gn-ui-max-lines>
</gn-ui-content-ghost>
</div>

Expand Down
4 changes: 3 additions & 1 deletion libs/ui/elements/src/lib/ui-elements.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import { FormsModule } from '@angular/forms'
import { AvatarComponent } from './avatar/avatar.component'
import { UserPreviewComponent } from './user-preview/user-preview.component'
import { GnUiLinkifyDirective } from './metadata-info/linkify.directive'
import { PaginationButtonsComponent } from './pagination-buttons/pagination-buttons.component'
import { PaginationButtonsComponent } from './pagination-buttons/pagination-buttons.component';
import { MaxLinesComponent } from './max-lines/max-lines.component'

@NgModule({
imports: [
Expand Down Expand Up @@ -61,6 +62,7 @@ import { PaginationButtonsComponent } from './pagination-buttons/pagination-butt
UserPreviewComponent,
GnUiLinkifyDirective,
PaginationButtonsComponent,
MaxLinesComponent,
],
exports: [
MetadataInfoComponent,
Expand Down

0 comments on commit d2066de

Please sign in to comment.