|
| 1 | +import React from "react"; |
| 2 | +import clsx from "clsx"; |
| 3 | + |
| 4 | +export type YouTubeEmbedProps = { |
| 5 | + /** Full YouTube URL or bare video id */ |
| 6 | + src: string; |
| 7 | + /** Accessible title for the iframe */ |
| 8 | + title?: string; |
| 9 | + /** 16/9 by default. Provide a ratio like 16/9, 4/3, or a custom string (e.g., '56.25%'). */ |
| 10 | + aspect?: "16/9" | "4/3" | "1/1" | string; |
| 11 | + /** Defer loading until scrolled into view */ |
| 12 | + lazy?: boolean; |
| 13 | + /** Start time in seconds */ |
| 14 | + start?: number; |
| 15 | + /** Auto-play after user interaction (applies on first load when clicking the thumbnail) */ |
| 16 | + autoplay?: boolean; |
| 17 | + /** Hide related videos */ |
| 18 | + rel?: 0 | 1; |
| 19 | + /** Reduce YouTube branding */ |
| 20 | + modestBranding?: 0 | 1; |
| 21 | + /** Show player controls */ |
| 22 | + controls?: 0 | 1; |
| 23 | + /** Additional class names on wrapper */ |
| 24 | + className?: string; |
| 25 | + /** Render a lightweight click-to-play thumbnail instead of immediate iframe */ |
| 26 | + lite?: boolean; |
| 27 | + /** Optional custom thumbnail URL; defaults to YouTube HQ thumbnail */ |
| 28 | + thumbnailUrl?: string; |
| 29 | +}; |
| 30 | + |
| 31 | +function extractId(src: string) { |
| 32 | + // Accept: youtu.be/<id>, youtube.com/watch?v=<id>, youtube.com/embed/<id>, or raw id |
| 33 | + const short = /youtu\.be\/(^[\n\r\s]+)?([\w-]{11})/; |
| 34 | + const watch = /v=([\w-]{11})/; |
| 35 | + const embed = /embed\/([\w-]{11})/; |
| 36 | + const raw = /^[\w-]{11}$/; |
| 37 | + if (raw.test(src)) return src; |
| 38 | + const s = src.toString(); |
| 39 | + const m1 = s.match(watch); |
| 40 | + if (m1) return m1[1]; |
| 41 | + const m2 = s.match(embed); |
| 42 | + if (m2) return m2[1]; |
| 43 | + const m3 = s.match(short); |
| 44 | + if (m3) return m3[2]; |
| 45 | + return src; // best effort |
| 46 | +} |
| 47 | + |
| 48 | +export function YouTubeEmbed({ |
| 49 | + src, |
| 50 | + title = "YouTube video player", |
| 51 | + aspect = "16/9", |
| 52 | + lazy = true, |
| 53 | + start, |
| 54 | + autoplay = false, |
| 55 | + rel = 0, |
| 56 | + modestBranding = 1, |
| 57 | + controls = 1, |
| 58 | + className, |
| 59 | + lite = true, |
| 60 | + thumbnailUrl, |
| 61 | +}: YouTubeEmbedProps) { |
| 62 | + const id = React.useMemo(() => extractId(src), [src]); |
| 63 | + |
| 64 | + const padTop = React.useMemo(() => { |
| 65 | + if (typeof aspect === "string" && aspect.includes("/")) { |
| 66 | + const [w, h] = aspect.split("/").map(Number); |
| 67 | + if (w && h) return `${(h / w) * 100}%`; |
| 68 | + } |
| 69 | + if (aspect.endsWith("%")) return aspect; // custom string like '56.25%' |
| 70 | + return "56.25%"; // default 16/9 |
| 71 | + }, [aspect]); |
| 72 | + |
| 73 | + const params = new URLSearchParams({ |
| 74 | + rel: String(rel), |
| 75 | + modestbranding: String(modestBranding), |
| 76 | + controls: String(controls), |
| 77 | + playsinline: "1", |
| 78 | + }); |
| 79 | + if (start) params.set("start", String(start)); |
| 80 | + if (autoplay) params.set("autoplay", "1"); |
| 81 | + |
| 82 | + const embedUrl = `https://www.youtube-nocookie.com/embed/${id}?${params.toString()}`; |
| 83 | + const thumb = |
| 84 | + thumbnailUrl || `https://i.ytimg.com/vi_webp/${id}/hqdefault.webp`; |
| 85 | + |
| 86 | + const [showIframe, setShowIframe] = React.useState(!lite); |
| 87 | + |
| 88 | + return ( |
| 89 | + <div |
| 90 | + className={clsx( |
| 91 | + "relative w-full overflow-hidden rounded-lg border bg-black", |
| 92 | + className |
| 93 | + )} |
| 94 | + style={{ paddingTop: padTop }} |
| 95 | + > |
| 96 | + {showIframe ? ( |
| 97 | + <iframe |
| 98 | + className="absolute inset-0 h-full w-full" |
| 99 | + src={embedUrl} |
| 100 | + title={title} |
| 101 | + loading={lazy ? "lazy" : undefined} |
| 102 | + allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" |
| 103 | + allowFullScreen |
| 104 | + referrerPolicy="strict-origin-when-cross-origin" |
| 105 | + /> |
| 106 | + ) : ( |
| 107 | + <button |
| 108 | + type="button" |
| 109 | + onClick={() => setShowIframe(true)} |
| 110 | + className={clsx( |
| 111 | + "absolute inset-0 grid place-items-center", |
| 112 | + "text-white focus:outline-none focus-visible:ring-2 focus-visible:ring-ring/60 focus-visible:ring-offset-2" |
| 113 | + )} |
| 114 | + aria-label="Play video" |
| 115 | + > |
| 116 | + {/* Thumbnail background */} |
| 117 | + <img |
| 118 | + src={thumb} |
| 119 | + alt="Video thumbnail" |
| 120 | + className="absolute inset-0 h-full w-full object-cover opacity-90" |
| 121 | + loading={lazy ? "lazy" : undefined} |
| 122 | + /> |
| 123 | + {/* Play button */} |
| 124 | + <span |
| 125 | + className="relative z-10 inline-block rounded-full bg-white/90 p-4 shadow-md transition hover:scale-105" |
| 126 | + > |
| 127 | + <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="currentColor" viewBox="0 0 16 16" className="text-black"> |
| 128 | + <path d="M11.596 8.697l-6.363 3.692A.75.75 0 0 1 4 11.742V4.258a.75.75 0 0 1 1.233-.647l6.363 3.692a.75.75 0 0 1 0 1.294z"/> |
| 129 | + </svg> |
| 130 | + </span> |
| 131 | + </button> |
| 132 | + )} |
| 133 | + </div> |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +export default YouTubeEmbed; |
0 commit comments