|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | + |
1 | 5 | interface YouTubeProps { |
2 | 6 | id: string; |
3 | 7 | title?: string; |
| 8 | + splash?: string; |
| 9 | + width?: number; |
| 10 | + height?: number; |
4 | 11 | } |
5 | 12 |
|
6 | 13 | /** |
7 | 14 | * A YouTube embed component for documentation. |
8 | | - * Uses lite-youtube-embed style with a thumbnail preview. |
| 15 | + * When a splash image URL is provided, shows the image with a play button overlay. |
| 16 | + * Clicking loads the YouTube player with autoplay. |
9 | 17 | */ |
10 | | -export function YouTube({ id, title = 'YouTube Video' }: YouTubeProps) { |
| 18 | +export function YouTube({ id, title = 'YouTube Video', splash, width, height }: YouTubeProps) { |
| 19 | + const [playing, setPlaying] = useState(false); |
| 20 | + |
| 21 | + const w = width ? Number(width) : undefined; |
| 22 | + const h = height ? Number(height) : undefined; |
| 23 | + const hasSize = w || h; |
| 24 | + const containerClass = hasSize |
| 25 | + ? "relative my-4 overflow-hidden rounded-lg" |
| 26 | + : "relative my-4 aspect-video w-full overflow-hidden rounded-lg"; |
| 27 | + const containerStyle = hasSize ? { width: w, height: h } : undefined; |
| 28 | + |
| 29 | + if (splash && !playing) { |
| 30 | + return ( |
| 31 | + <div className={containerClass} style={containerStyle}> |
| 32 | + <button |
| 33 | + type="button" |
| 34 | + className="group relative block h-full w-full cursor-pointer border-0 bg-transparent p-0" |
| 35 | + onClick={() => setPlaying(true)} |
| 36 | + aria-label={`Play ${title}`} |
| 37 | + > |
| 38 | + <img |
| 39 | + src={splash} |
| 40 | + alt={title} |
| 41 | + className="h-full w-full object-cover" |
| 42 | + /> |
| 43 | + <div className="absolute inset-0 flex items-center justify-center bg-black/5 transition-colors group-hover:bg-black/15"> |
| 44 | + <svg |
| 45 | + className="h-16 w-16 text-white drop-shadow-lg transition-transform group-hover:scale-110" |
| 46 | + viewBox="0 0 68 48" |
| 47 | + fill="none" |
| 48 | + > |
| 49 | + <path |
| 50 | + d="M66.52 7.74c-.78-2.93-2.49-5.41-5.42-6.19C55.79.13 34 0 34 0S12.21.13 6.9 1.55C3.97 2.33 2.27 4.81 1.48 7.74 0.06 13.05 0 24 0 24s0.06 10.95 1.48 16.26c0.78 2.93 2.49 5.41 5.42 6.19C12.21 47.87 34 48 34 48s21.79-.13 27.1-1.55c2.93-.78 4.64-3.26 5.42-6.19C67.94 34.95 68 24 68 24s-.06-10.95-1.48-16.26z" |
| 51 | + fill="#FF0000" |
| 52 | + /> |
| 53 | + <path d="M45 24L27 14v20" fill="white" /> |
| 54 | + </svg> |
| 55 | + </div> |
| 56 | + </button> |
| 57 | + </div> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
11 | 61 | return ( |
12 | | - <div className="relative my-4 aspect-video w-full overflow-hidden rounded-lg"> |
| 62 | + <div className={containerClass} style={containerStyle}> |
13 | 63 | <iframe |
14 | | - className="absolute inset-0 h-full w-full" |
15 | | - src={`https://www.youtube.com/embed/${id}`} |
| 64 | + className={hasSize ? "h-full w-full" : "absolute inset-0 h-full w-full"} |
| 65 | + src={`https://www.youtube.com/embed/${id}${splash ? '?autoplay=1' : ''}`} |
16 | 66 | title={title} |
17 | 67 | allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" |
18 | 68 | allowFullScreen |
19 | 69 | /> |
20 | 70 | </div> |
21 | 71 | ); |
22 | 72 | } |
23 | | - |
|
0 commit comments