Headless helpers for the project's own <Media> (look stays in the project): - mediaImageProps: dimensions from the doc (no CLS), lazy by default, LCP image gets preload + fetchPriority=high + eager (Next 16 replaced 'priority'), focal point → object-position, blur placeholder + dominant colour. - MEDIA_SIZES suggestions for 'sizes' (any string still works). - videoProps: 'background' (muted loop, respects reduced motion) or 'player' (controls, preload=none, poster). - buildMediaPlaceholderHook/Field: tiny WebP blur + dominant colour at upload via the configured sharp (no new dependency). - buildImagesConfig: AVIF/WebP, R2 remotePatterns, 31-day cache. - <YouTubeEmbed> (/client): poster + button, youtube-nocookie iframe on click. - buildCsp youtube allows the i.ytimg.com poster. kod-sekcji Media template uses the helpers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
56 lines
2.2 KiB
TypeScript
56 lines
2.2 KiB
TypeScript
'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<Record<'button' | 'frame' | 'note' | 'poster' | 'root', string>>
|
|
/** 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: <title>"). */
|
|
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>
|
|
)
|
|
}
|