Skip to content

Instantly share code, notes, and snippets.

@mstaack
Forked from inxilpro/youtube-video.blade.php
Created September 15, 2025 20:51
Show Gist options
  • Save mstaack/7c7bdcf772be7c0aafb51dde14c67fee to your computer and use it in GitHub Desktop.
Save mstaack/7c7bdcf772be7c0aafb51dde14c67fee to your computer and use it in GitHub Desktop.
@props([
'src',
'title' => null,
'alt' => null,
])
@php
preg_match('/.*\/embed\/([^?&\/]+)/m', $src, $matches);
$id = $matches[1];
$alt ??= $title ?? 'Embedded YouTube video';
@endphp
<figure
x-data="{ activated: false, loaded: false }"
{{ $attributes->except('x-data')->class('aspect-video w-full grid grid-cols-1 grid-rows-1') }}
>
<template x-if="activated">
<iframe
@load="loaded = true"
class="w-full aspect-video rounded-md border col-start-1 row-start-1 z-10"
src="https://www.youtube-nocookie.com/embed/{{ $id }}?autoplay=1"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
></iframe>
</template>
<div
class="w-full aspect-video col-start-1 row-start-1 z-0 relative"
@click.prevent="activated = true"
x-show="! loaded"
>
<img
src="https://i.ytimg.com/vi/{{ $id }}/maxresdefault.jpg"
class="w-full aspect-video rounded-md border"
alt="{{ $alt }}"
/>
@if($title)
<div
class=""
></div>
<figcaption
class="absolute bg-gradient-to-b from-black/60 to-transparent top-0 left-0 right-0 px-4 py-2 pb-8 rounded-t-md text-white text-shadow font-semibold text-sm"
>
{{ $title }}
</figcaption>
@endif
<button
x-show="! activated"
class="absolute inset-0 flex items-center justify-center border-0 bg-transparent cursor-pointer"
>
<svg viewBox="0 0 68 48" class="opacity-90 w-16 md:w-24">
<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="#f03"
></path>
<path d="M 45,24 27,14 27,34" fill="#fff"></path>
</svg>
</button>
</div>
</figure>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment