import * as React from 'react' import { cs } from '../utils' const qs = (params: Record) => { return Object.keys(params) .map( (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}` ) .join('&') } export const LiteYouTubeEmbed: React.FC<{ id: string defaultPlay?: boolean mute?: boolean lazyImage?: boolean iframeTitle?: string alt?: string params?: Record adLinksPreconnect?: boolean style?: React.CSSProperties className?: string }> = ({ id, defaultPlay = false, mute = false, lazyImage = false, iframeTitle = 'YouTube video', alt = 'Video preview', params = {}, adLinksPreconnect = true, style, className }) => { const muteParam = mute || defaultPlay ? '1' : '0' // Default play must be muted const queryString = React.useMemo( () => qs({ autoplay: '1', mute: muteParam, ...params }), [muteParam, params] ) // const mobileResolution = 'hqdefault' // const desktopResolution = 'maxresdefault' const resolution = 'hqdefault' const posterUrl = `https://i.ytimg.com/vi/${id}/${resolution}.jpg` const ytUrl = 'https://www.youtube-nocookie.com' const iframeSrc = `${ytUrl}/embed/${id}?${queryString}` const [isPreconnected, setIsPreconnected] = React.useState(false) const [iframeInitialized, setIframeInitialized] = React.useState(defaultPlay) const [isIframeLoaded, setIsIframeLoaded] = React.useState(false) const warmConnections = React.useCallback(() => { if (isPreconnected) return setIsPreconnected(true) }, [isPreconnected]) const onLoadIframe = React.useCallback(() => { if (iframeInitialized) return setIframeInitialized(true) }, [iframeInitialized]) const onIframeLoaded = React.useCallback(() => { setIsIframeLoaded(true) }, []) return ( <> {isPreconnected && ( <> {/* The iframe document and most of its subresources come from youtube.com */} {/* The botguard script is fetched off from google.com */} )} {isPreconnected && adLinksPreconnect && ( <> {/* Not certain if these ad related domains are in the critical path. Could verify with domain-specific throttling. */} )}
{alt}
{iframeInitialized && (