Skip to content

Commit 242f19c

Browse files
committed
demo(youtube-player): add auto video resizing
- 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 5c92c06 commit 242f19c

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

src/dev-app/tsconfig.json

Lines changed: 2 additions & 1 deletion
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"]
Lines changed: 4 additions & 3 deletions
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>
Lines changed: 1 addition & 1 deletion
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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Component} from '@angular/core';
9+
import {
10+
AfterViewInit, ChangeDetectorRef, Component, ElementRef, OnDestroy, ViewChild
11+
} from '@angular/core';
1012

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

0 commit comments

Comments
 (0)