src/app/components/youtube-model/youtube-model.component.ts
Displayes a Youtube player with title
OnInit
selector | youtube-model |
styleUrls | ./youtube-model.component.scss |
templateUrl | ./youtube-model.component.html |
Properties |
Methods |
Inputs |
HostListeners |
playerData | |
Type : YoutubeModel
|
|
Details of youtube player and video |
window:resize |
Arguments : '$event'
|
window:resize()
|
Function to handle resize event and change size of youtube player |
onResize |
onResize()
|
Decorators :
@HostListener('window:resize', ['$event'])
|
Function to handle resize event and change size of youtube player
Returns :
void
|
playerSize |
playerSize()
|
Changes the size of youtube player
Returns :
void
|
apiLoaded |
Default value : false
|
Flag to check if the script is loaded |
import { Component, HostListener, Input, OnInit } from '@angular/core';
import { YoutubeModel } from './youtube-model';
/** Displayes a Youtube player with title */
@Component({
selector: 'youtube-model',
templateUrl: './youtube-model.component.html',
styleUrls: ['./youtube-model.component.scss'],
})
export class YoutubeModelComponent implements OnInit {
/** Details of youtube player and video */
@Input() playerData!: YoutubeModel;
/** Flag to check if the script is loaded */
apiLoaded = false;
/** Loads the script in the current document */
ngOnInit(): void {
if (!this.apiLoaded) {
const tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
document.body.appendChild(tag);
this.apiLoaded = true;
}
this.playerSize();
}
/** Function to handle resize event and change size of youtube player */
@HostListener('window:resize', ['$event'])
onResize() {
this.playerSize();
}
/** Changes the size of youtube player */
playerSize() {
this.playerData.width = Math.min(552, window.innerWidth * 0.85);
this.playerData.height = Math.min(311, this.playerData.width * 0.5625);
}
}
<p class="player-title">{{ playerData.playerTitle }}</p>
<youtube-player [height]="playerData.height" [width]="playerData.width" [videoId]="playerData.videoId"></youtube-player>
./youtube-model.component.scss
:host {
.player-title {
font-weight: 300;
font-size: 1.5rem;
line-height: 2.125rem;
letter-spacing: 0.005rem;
margin-bottom: 2rem;
}
::ng-deep youtube-player iframe {
max-height: 584px;
}
@media (max-width: 26.75rem) {
::ng-deep youtube-player iframe {
height: 232px;
}
}
}