/
index.ts
71 lines (58 loc) · 1.72 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Base } from "../base/index.js";
import type { Attributes } from "../types/index.js";
export type YouTubeAttributes = Attributes<YouTube, "uid">;
/**
* Embeds a YouTube video iframe into a website with the video uid, using www.youtube-nocookie.com.
*/
export class YouTube extends Base {
static observedAttributes = ["autoplay", "start", "uid"] as const;
constructor() {
super();
}
/** The `HTMLIFrameElement` within the element. */
get iframe() {
return this.getContent(HTMLIFrameElement);
}
/** Whether the video should start playing when loaded. */
get autoplay() {
return this.hasAttribute("autoplay");
}
set autoplay(value) {
if (value) this.setAttribute("autoplay", "");
else this.removeAttribute("autoplay");
}
/** The start time of the video (seconds). */
get start() {
return this.getAttribute("start") ?? "0";
}
set start(value) {
this.setAttribute("start", value);
}
/**
* The video's YouTube uid, found within the url of the video.
*
* For example if the video url is https://youtu.be/gouiY85kD2o,
* the `uid` is `"gouiY85kD2o"`.
*/
get uid() {
const uid = this.getAttribute("uid");
if (!uid) throw new Error("YouTube: missing `uid` attribute.");
return uid;
}
set uid(v) {
this.setAttribute("uid", v);
}
mount() {
this.iframe.allowFullscreen = true;
this.iframe.allow =
"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture";
}
attributeChangedCallback() {
// BREAKING TODO: make consistent with other components - decide on observed attributes
queueMicrotask(() => {
this.iframe.src = `https://www.youtube-nocookie.com/embed/${this.uid}?start=${this.start}${
this.autoplay ? "&autoplay=1" : ""
}`;
});
}
}