'use client' import { useState } from 'react' type YouTubeEmbedProps = { /** Classes per element; nothing is styled by default (keep the aspect ratio in your CSS). */ classNames?: Partial> /** Short text under the play button, from the panel — e.g. that playing loads YouTube. */ note?: string /** Accessible name of the play button, from the panel ("Play: "). */ playLabel: string /** * Poster URL — your own upload (no request to Google until play) or omit to * use YouTube's thumbnail (i.ytimg.com, no cookies). */ poster?: string /** Video title, from the panel — iframe title for screen readers. */ title: string /** YouTube video id (the part after watch?v=). */ videoId: string } /** * Lightweight YouTube: a poster and a play button until the visitor clicks — * no ~1 MB of player JavaScript and no YouTube cookies on page load (better * LCP, no consent problem before interaction). On click it loads the * privacy-enhanced youtube-nocookie.com player with autoplay. Unstyled. */ export function YouTubeEmbed({ classNames = {}, note, playLabel, poster, title, videoId }: YouTubeEmbedProps) { const [playing, setPlaying] = useState(false) const id = /^[\w-]{6,20}$/.test(videoId) ? videoId : '' if (!id) {return null} if (playing) { return ( <iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen className={classNames.frame} // What the YouTube player needs, nothing more (no top navigation, no forms). sandbox="allow-scripts allow-same-origin allow-presentation allow-popups allow-popups-to-escape-sandbox" src={`https://www.youtube-nocookie.com/embed/${id}?autoplay=1`} title={title} /> ) } return ( <div className={classNames.root}> <img alt="" className={classNames.poster} decoding="async" loading="lazy" src={poster ?? `https://i.ytimg.com/vi/${id}/hqdefault.jpg`} /> <button aria-label={playLabel} className={classNames.button} onClick={() => setPlaying(true)} type="button"> ▶ </button> {note ? <p className={classNames.note}>{note}</p> : null} </div> ) }