-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(card-list): replace resize observer with CSS (#262)
- Loading branch information
Showing
4 changed files
with
38 additions
and
97 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
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 |
---|---|---|
@@ -1,73 +1,16 @@ | ||
import { | ||
AfterViewInit, | ||
ChangeDetectorRef, | ||
Component, | ||
ElementRef, | ||
EventEmitter, | ||
Input, | ||
OnDestroy, | ||
Output, | ||
ViewChild, | ||
} from '@angular/core'; | ||
import { Component, EventEmitter, Input, Output } from '@angular/core'; | ||
import { CardItem, ClickableCardComponent } from '../clickable-card/clickable-card.component'; | ||
import { NgClass } from '@angular/common'; | ||
|
||
const CARD_WIDTH = 128; | ||
const GAB_WIDTH = 24; | ||
|
||
@Component({ | ||
selector: 'app-card-list', | ||
templateUrl: './card-list.component.html', | ||
standalone: true, | ||
imports: [NgClass, ClickableCardComponent], | ||
imports: [ClickableCardComponent], | ||
}) | ||
export class CardListComponent implements AfterViewInit, OnDestroy { | ||
export class CardListComponent { | ||
@Input({ required: true }) items!: CardItem[] | null; | ||
|
||
@Input() overflow: 'wrap' | 'scroll' = 'wrap'; | ||
|
||
@Output() itemClick = new EventEmitter<CardItem>(); | ||
|
||
@ViewChild('container') containerElement!: ElementRef<HTMLElement>; | ||
|
||
maxItemsForOneLine = 3; | ||
skeletonItems = Array(this.maxItemsForOneLine) | ||
.fill(0) | ||
.map((_, i) => i); | ||
gridColsClass = 'grid-cols-3'; | ||
|
||
private resizeObserver: ResizeObserver; | ||
|
||
constructor(private cdr: ChangeDetectorRef) { | ||
this.resizeObserver = new ResizeObserver(this.onResize); | ||
} | ||
|
||
ngAfterViewInit(): void { | ||
this.resizeObserver.observe(this.containerElement.nativeElement); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.resizeObserver.disconnect(); | ||
} | ||
|
||
private onResize: ResizeObserverCallback = ([entry]) => { | ||
if (this.overflow === 'scroll') { | ||
return; | ||
} | ||
|
||
const oldMaxItemsForOneLine = this.maxItemsForOneLine; | ||
this.maxItemsForOneLine = Math.max( | ||
2, | ||
Math.floor((entry.contentBoxSize[0].inlineSize + GAB_WIDTH) / (CARD_WIDTH + GAB_WIDTH)), | ||
); | ||
this.gridColsClass = `grid-cols-${this.maxItemsForOneLine}`; | ||
this.skeletonItems = Array(this.maxItemsForOneLine) | ||
.fill(0) | ||
.map((_, i) => i); | ||
|
||
// ResizeObserver does not run in Angular Zone, so trigger change detection if value has changed | ||
if (oldMaxItemsForOneLine !== this.maxItemsForOneLine) { | ||
this.cdr.detectChanges(); | ||
} | ||
}; | ||
} |
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