Skip to content

Commit cb09ee8

Browse files
committed
fix(site): attach yt-embed click handler directly in PtYouTube.astro for pages without PortableText body
1 parent 0715089 commit cb09ee8

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎apps/site/src/components/portable-text/PortableTextContent.astro‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const components = {
199199
// until a visitor actually asks for it.
200200
const play = target.closest("button");
201201
const embed = play?.closest<HTMLElement>(".yt-embed");
202-
if (!play || !embed) {
202+
if (!play || !embed || embed.querySelector("iframe")) {
203203
return;
204204
}
205205
const id = embed.dataset.yt;

‎apps/site/src/components/portable-text/PtYouTube.astro‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,32 @@ const alt = rawAlt && rawAlt.trim().length > 0 ? rawAlt : "";
136136
</div>
137137
)
138138
}
139+
140+
<script>
141+
if (!(window as unknown as { __ccYtEmbedInit?: boolean }).__ccYtEmbedInit) {
142+
(window as unknown as { __ccYtEmbedInit?: boolean }).__ccYtEmbedInit = true;
143+
document.addEventListener("click", (event) => {
144+
const target = event.target;
145+
if (!(target instanceof Element)) {
146+
return;
147+
}
148+
const play = target.closest("button");
149+
const embed = play?.closest<HTMLElement>(".yt-embed");
150+
if (!play || !embed || embed.querySelector("iframe")) {
151+
return;
152+
}
153+
const id = embed.dataset.yt;
154+
if (!id) {
155+
return;
156+
}
157+
const iframe = document.createElement("iframe");
158+
iframe.src = `https://www.youtube-nocookie.com/embed/${id}?autoplay=1&rel=0&modestbranding=1&fs=0`;
159+
iframe.allow =
160+
"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share";
161+
iframe.allowFullscreen = true;
162+
iframe.title = "YouTube video player";
163+
iframe.className = "absolute inset-0 size-full border-0";
164+
embed.replaceChildren(iframe);
165+
});
166+
}
167+
</script>

0 commit comments

Comments
 (0)