-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: image re-rendering for movie details
- Loading branch information
jgelin
committed
Jul 18, 2023
1 parent
366ad56
commit d0f2278
Showing
4 changed files
with
75 additions
and
30 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
49 changes: 46 additions & 3 deletions
49
projects/movies/src/app/ui/component/detail-grid/detail-grid.component.ts
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,22 +1,65 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
ContentChild, | ||
Input, | ||
OnChanges, | ||
SimpleChanges, | ||
TemplateRef, | ||
ViewChild, | ||
ViewContainerRef, | ||
ViewEncapsulation, | ||
} from '@angular/core'; | ||
import { NgTemplateOutlet } from '@angular/common'; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'ui-detail-grid', | ||
template: ` | ||
<div class="grid--item gradient"> | ||
<ng-content select="[detailGridMedia]"></ng-content> | ||
<ng-container | ||
#detailGridMediaOutlet | ||
[ngTemplateOutlet]="detailGridMediaRef" | ||
></ng-container> | ||
</div> | ||
<div class="grid--item media"> | ||
<ng-content select="[detailGridDescription]"></ng-content> | ||
<ng-container [ngTemplateOutlet]="detailGridDescription"></ng-container> | ||
</div> | ||
`, | ||
styleUrls: ['./detail-grid.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation: ViewEncapsulation.Emulated, | ||
imports: [NgTemplateOutlet], | ||
}) | ||
export class DetailGridComponent {} | ||
export class DetailGridComponent implements OnChanges { | ||
@ContentChild('detailGridMedia', { read: TemplateRef }) | ||
detailGridMediaRef!: TemplateRef<unknown>; | ||
@ContentChild('detailGridDescription', { read: TemplateRef }) | ||
detailGridDescription!: TemplateRef<unknown>; | ||
|
||
/** | ||
* Each time a new value is set to the input rerenderDetailGridMedia (any type), rerender the block `detailGridMedia` (usually the img) | ||
* Hacky way to solve issue https://github.com/angular/angular/issues/47813 | ||
*/ | ||
@Input() rerenderDetailGridMedia: unknown; | ||
@ViewChild('detailGridMediaOutlet', { read: ViewContainerRef }) | ||
outletRef!: ViewContainerRef; | ||
|
||
ngOnChanges(changes: SimpleChanges): void { | ||
if ( | ||
changes['rerenderDetailGridMedia'] && | ||
!changes['rerenderDetailGridMedia'].isFirstChange() | ||
) { | ||
this.doRerenderDetailGridMedia(); | ||
} | ||
} | ||
|
||
private doRerenderDetailGridMedia(): void { | ||
if (!this.outletRef) { | ||
return; | ||
} | ||
|
||
this.outletRef.clear(); | ||
this.outletRef.createEmbeddedView(this.detailGridMediaRef); | ||
} | ||
} |