--- // A video, map or player from another site, loaded only when the reader asks // for it. Until then it is a link to the provider's page drawn as a poster: // no iframe, no third-party request, no cookie (YouTube plays from // youtube-nocookie.com). A small script turns the click into the player in // place; without it the link simply opens the provider. Unknown providers are // never framed — they stay a plain link. import { tv } from 'tailwind-variants' import KitImage from '../_shared/KitImage.astro' import type { ImageInput } from '../_shared/types' import { embedTarget, mapTarget } from '../_shared/embed-target' const embed = tv({ slots: { root: 'mx-auto my-8 w-full', frame: 'relative overflow-hidden rounded-card bg-ink text-surface', facade: 'group absolute inset-0 flex items-end justify-start no-underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent', poster: 'absolute inset-0 h-full w-full object-cover opacity-80 transition-opacity group-hover:opacity-65', play: 'absolute top-1/2 left-1/2 grid size-16 -translate-x-1/2 -translate-y-1/2 place-items-center rounded-full bg-accent text-accent-ink shadow-lg transition-transform group-hover:scale-105 group-focus-visible:scale-105', label: 'relative m-0 w-full bg-gradient-to-t from-ink/90 to-transparent px-4 pt-10 pb-3 text-sm font-semibold text-balance text-surface md:px-5 md:pb-4 md:text-base', caption: 'mt-2 text-sm text-ink-muted', }, variants: { aspect: { auto: {}, video: { frame: 'aspect-video' }, portrait: { root: 'max-w-sm', frame: 'aspect-[9/16]' }, square: { root: 'max-w-xl', frame: 'aspect-square' }, map: { frame: 'aspect-[4/3] md:aspect-[16/9]' }, }, width: { prose: { root: 'max-w-(--container-prose)' }, page: {} }, }, defaultVariants: { aspect: 'auto', width: 'prose' }, }) interface Props { /** A video page (YouTube, Vimeo) or a known provider's embed address (Google Maps embed, Spotify, SoundCloud, Calendly, Typeform, Google Forms, Loom, Wistia). */ url?: string | undefined /** A place or address to show on a Google map, when there is no `url`. */ address?: string | undefined /** What it is ("Studio tour", "Our office"): the iframe's accessible name and the poster's text. */ title?: string | undefined caption?: string | undefined /** Local poster image; without one the poster is a plain dark panel. */ poster?: ImageInput | undefined /** Poster action for a video or player (ui-strings embed.play). */ playLabel?: string | undefined /** Poster action for a map or widget (ui-strings embed.show). */ showLabel?: string | undefined aspect?: 'auto' | 'video' | 'portrait' | 'square' | 'map' | undefined width?: 'prose' | 'page' | undefined /** VideoObject structured data (JSON-LD) for a video with a name (`title` or `caption`); on by default, `false`: none. */ schema?: boolean | undefined /** When the video was published (ISO date), for the VideoObject's uploadDate; left out when unknown. */ uploaded?: string | undefined id?: string | undefined } const { url, address, title, caption, poster, playLabel = 'Play', showLabel = 'Show', aspect = 'auto', width, schema = true, uploaded, id } = Astro.props const target = (url ? embedTarget(url) : null) ?? (address && !url ? mapTarget(address) : null) const shape = aspect !== 'auto' ? aspect : target?.portrait ? 'portrait' : target?.kind === 'map' ? 'map' : 'video' // A player strip (Spotify, SoundCloud) has a fixed height, not a ratio. const strip = aspect === 'auto' && target?.kind === 'audio' const s = embed({ aspect: strip ? 'auto' : shape, width }) const action = target && (target.kind === 'video' || target.kind === 'audio') ? playLabel : showLabel const name = title ?? caption ?? '' const linkUrl = url && /^https?:\/\//i.test(url) ? url : undefined // VideoObject states only what the embed knows: its name, its page and player, a poster (the site's own, else the // provider's still for a YouTube video: a URL in data, never fetched by the visitor) and an upload date when given. const youtubeId = target?.provider === 'youtube' ? /(?:[?&]v=|\/shorts\/)([\w-]{11})/.exec(target.href)?.[1] : undefined const thumbnail = poster?.src ? new URL(poster.src, Astro.site ?? Astro.url).href : youtubeId ? `https://i.ytimg.com/vi/${youtubeId}/hqdefault.jpg` : undefined const structured = schema && target?.kind === 'video' && name ? JSON.stringify({ '@context': 'https://schema.org', '@type': 'VideoObject', name, ...(caption && caption !== name ? { description: caption } : {}), url: target.href, embedUrl: target.src.split('?')[0], ...(thumbnail ? { thumbnailUrl: thumbnail } : {}), ...(uploaded ? { uploadDate: uploaded } : {}), }).replace(/
{poster && }
{caption &&
{caption}
} {structured && } ) : linkUrl && (

{name || linkUrl}

)}