Skip to content

Commit b5146bf

Browse files
Splaktarmmalerba
authored andcommitted
demo(youtube-player): add auto video resizing (angular#17648)
- automatically resize the video to fit the screen - this was noticeable on mobile where the video would just get cut off - rename video to selectedVideo for clarity - fix TypeScript path mappings so that IDEs don't complain about module - use classes to style Material components as recommended
1 parent b8b0f57 commit b5146bf

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

src/dev-app/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"@angular/material-moment-adapter": ["../material-moment-adapter/public-api.ts"],
1717
"@angular/google-maps": ["../google-maps"],
1818
"@angular/components-examples": ["../components-examples"],
19-
"@angular/components-examples/*": ["../components-examples/*"]
19+
"@angular/components-examples/*": ["../components-examples/*"],
20+
"@angular/youtube-player": ["../youtube-player"]
2021
}
2122
},
2223
"include": ["./**/*.ts"]
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
<div>
1+
<div #demoYouTubePlayer class="demo-youtube-player">
22
<h1>Basic Example</h1>
33
<section>
44
<div class="demo-video-selection">
55
<label>Pick the video:</label>
6-
<mat-radio-group aria-label="Select a video" [(ngModel)]="video">
6+
<mat-radio-group aria-label="Select a video" [(ngModel)]="selectedVideo">
77
<mat-radio-button *ngFor="let video of videos" [value]="video">
88
{{video.name}}
99
</mat-radio-button>
1010
<mat-radio-button [value]="undefined">Unset</mat-radio-button>
1111
</mat-radio-group>
1212
</div>
13-
<youtube-player [videoId]="video && video.id"></youtube-player>
13+
<youtube-player [videoId]="selectedVideo && selectedVideo.id"
14+
[width]="videoWidth" [height]="videoHeight"></youtube-player>
1415
</section>
1516
</div>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.demo-video-selection {
22
margin-bottom: 20px;
33

4-
mat-radio-button {
4+
.mat-radio-button {
55
margin: 8px;
66
}
77
}

src/dev-app/youtube-player/youtube-player-demo.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Component} from '@angular/core';
9+
import {
10+
AfterViewInit,
11+
ChangeDetectorRef,
12+
Component,
13+
ElementRef,
14+
OnDestroy,
15+
ViewChild
16+
} from '@angular/core';
1017

1118
interface Video {
1219
id: string;
@@ -33,8 +40,28 @@ const VIDEOS: Video[] = [
3340
templateUrl: 'youtube-player-demo.html',
3441
styleUrls: ['youtube-player-demo.css'],
3542
})
36-
export class YouTubePlayerDemo {
37-
video: Video | undefined = VIDEOS[0];
43+
export class YouTubePlayerDemo implements AfterViewInit, OnDestroy {
44+
@ViewChild('demoYouTubePlayer') demoYouTubePlayer: ElementRef<HTMLDivElement>;
45+
selectedVideo: Video | undefined = VIDEOS[0];
3846
videos = VIDEOS;
39-
apiLoaded = false;
47+
videoWidth: number | undefined;
48+
videoHeight: number | undefined;
49+
50+
constructor(private _changeDetectorRef: ChangeDetectorRef) {}
51+
52+
ngAfterViewInit(): void {
53+
this.onResize();
54+
window.addEventListener('resize', this.onResize);
55+
}
56+
57+
onResize = (): void => {
58+
// Automatically expand the video to fit the page up to 1200px x 720px
59+
this.videoWidth = Math.min(this.demoYouTubePlayer.nativeElement.clientWidth, 1200);
60+
this.videoHeight = this.videoWidth * 0.6;
61+
this._changeDetectorRef.detectChanges();
62+
}
63+
64+
ngOnDestroy(): void {
65+
window.removeEventListener('resize', this.onResize);
66+
}
4067
}

0 commit comments

Comments
 (0)