src/app/modules/content/video-modal/video-modal/video-modal.component.ts
Component for displaying a youtube video inside of an angular material modal.
OnInit
changeDetection | ChangeDetectionStrategy.OnPush |
selector | ccf-video-modal |
styleUrls | ./video-modal.component.scss |
templateUrl | ./video-modal.component.html |
Properties |
Methods |
HostBindings |
constructor(renderer2: Renderer2, dialogRef: MatDialogRef<VideoModalComponent>, data, document: Document)
|
|||||||||||||||
Creates an instance of video modal component.
Parameters :
|
class |
Type : "ccf-video-modal"
|
Default value : 'ccf-video-modal'
|
HTML class name |
close |
close()
|
Closes the video modal component
Returns :
void
|
loadYoutubePlayerAPI |
loadYoutubePlayerAPI()
|
loads the IFrame Player API code asynchronously from YouTube.
Returns :
void
|
Readonly clsName |
Type : string
|
Default value : 'ccf-video-modal'
|
Decorators :
@HostBinding('class')
|
HTML class name |
Public data |
Decorators :
@Inject(MAT_DIALOG_DATA)
|
Public Readonly dialogRef |
Type : MatDialogRef<VideoModalComponent>
|
import { DOCUMENT } from '@angular/common';
import { ChangeDetectionStrategy, Component, HostBinding, Inject, OnInit, Renderer2 } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
/**
* Component for displaying a youtube video inside of an angular material modal.
*/
@Component({
selector: 'ccf-video-modal',
templateUrl: './video-modal.component.html',
styleUrls: ['./video-modal.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class VideoModalComponent implements OnInit {
/** HTML class name */
@HostBinding('class') readonly clsName = 'ccf-video-modal';
/**
* Creates an instance of video modal component.
*/
constructor(
private readonly renderer2: Renderer2,
public readonly dialogRef: MatDialogRef<VideoModalComponent>,
@Inject(MAT_DIALOG_DATA) public data: unknown,
@Inject(DOCUMENT) private readonly document: Document,
) {}
/**
* load the youtube player api in on init
*/
ngOnInit(): void {
this.loadYoutubePlayerAPI();
}
/**
* loads the IFrame Player API code asynchronously from YouTube.
*/
loadYoutubePlayerAPI(): void {
const script = this.renderer2.createElement('script') as HTMLScriptElement;
script.src = 'https://www.youtube.com/iframe_api';
this.renderer2.appendChild(this.document.body, script);
}
/**
* Closes the video modal component
*/
close(): void {
this.dialogRef.close();
}
}
<div class="video-modal wrapper">
<div class="container">
<mat-icon class="material-icons close-icon" (click)="close()">clear</mat-icon>
<div class="content">
<mat-dialog-content class="mat-typography">
<youtube-player videoId="142hGer4xvU"></youtube-player>
</mat-dialog-content>
</div>
</div>
</div>
./video-modal.component.scss
::ng-deep {
.mat-dialog-container {
padding: 0;
}
.mat-dialog-content {
display: block;
margin: auto;
padding: 0;
max-height: 65vh;
overflow: auto;
width: fit-content;
}
}
:host {
.video-modal.wrapper {
width: fit-content;
.container {
position: relative;
.content {
padding: 0.5rem;
padding-top: 3rem;
width: fit-content;
border-radius: 0.5rem;
}
.close-icon {
cursor: pointer;
position: absolute;
right: 0rem;
font-size: 2.5rem;
width: 2.5rem;
height: 2.5rem;
transition: 0.5s color;
}
}
}
}