Repository navigation
Expand file tree
/
Copy pathPtYouTube.astro
More file actions
88 lines (83 loc) · 2.91 KB
/
Copy pathPtYouTube.astro
File metadata and controls
88 lines (83 loc) · 2.91 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
import { stegaClean } from "@sanity/client/stega";
import type { SanityImageSource } from "@sanity/image-url";
import type { Props as PtProps } from "astro-portabletext/types";
import { youtubeId } from "@/lib/youtube";
type YouTubeNode = { _type: "youtube"; youtube?: string };
type Props = PtProps<YouTubeNode> & {
coverImage?: (SanityImageSource & { alt?: string }) | null;
priority?: boolean;
};
const { node, coverImage, priority = false } = Astro.props;
const id = youtubeId(stegaClean(node.youtube));
const builder = Astro.locals.sanity.urlForImage(coverImage ?? undefined);
const sanitySrc = builder?.width(1920).height(1080).quality(82).url();
const sanitySrcset = builder
? [480, 640, 960, 1280, 1600, 1920]
.map((w) => {
const h = Math.round((w * 9) / 16);
return `${builder.width(w).height(h).quality(82).url()} ${w}w`;
})
.join(", ")
: undefined;
const rawAlt = stegaClean((coverImage as { alt?: string } | null)?.alt);
const alt = rawAlt && rawAlt.trim().length > 0 ? rawAlt : "";
---
{
id && (
<div class="not-prose my-6">
<div class="yt-embed relative aspect-video isolate overflow-hidden rounded-md" data-yt={id}>
{sanitySrc ? (
<img
class="size-full object-cover"
src={sanitySrc}
srcset={sanitySrcset}
sizes="(max-width: 1280px) 100vw, 1280px"
alt={alt}
width={1920}
height={1080}
loading={priority ? "eager" : "lazy"}
fetchpriority={priority ? "high" : "auto"}
decoding={priority ? "sync" : "async"}
/>
) : (
<picture>
<source
type="image/webp"
sizes="(max-width: 1280px) 100vw, 1280px"
srcset={[
`https://i.ytimg.com/vi_webp/${id}/mqdefault.webp 320w`,
`https://i.ytimg.com/vi_webp/${id}/hqdefault.webp 480w`,
`https://i.ytimg.com/vi_webp/${id}/sddefault.webp 640w`,
`https://i.ytimg.com/vi_webp/${id}/maxresdefault.webp 1280w`,
].join(", ")}
/>
<img
class="size-full object-cover"
src={`https://i.ytimg.com/vi/${id}/maxresdefault.jpg`}
alt=""
width={1280}
height={720}
loading={priority ? "eager" : "lazy"}
fetchpriority={priority ? "high" : "auto"}
decoding={priority ? "sync" : "async"}
/>
</picture>
)}
<button
type="button"
class="absolute inset-0 grid size-full place-items-center border-0 bg-transparent"
aria-label="Play video"
>
<svg viewBox="0 0 68 48" height="48" width="68" aria-hidden="true">
<path
d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z"
fill="#f00"
/>
<path d="M 45,24 27,14 27,34" fill="#fff" />
</svg>
</button>
</div>
</div>
)
}