Number 20: Video poster that opens the player
Specification
- Catalogue
- No. 20 of 110
- Section
- Media and galleries
- Interaction
- Press, Layout Animation
- Element
- Media, Overlay
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
A still with a play button; press it and the still grows into the player, which loads only then.
The page stays fast because the video is not fetched until asked for, and the visitor sees the player come from the picture they pressed.
Poster grows into the player 450ms cubic-bezier(.16, 1, .3, 1); back 300ms. The video element exists only while open; WebM with an MP4 fallback. Local sample clip (public/samples/kitchen-tour), no third-party embed. Changed from the library: no glow behind the poster.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useId, useRef, useState } from "react";
import { growBetween } from "@/components/pieces/motion-helpers";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import "@/components/pieces/styles/lightbox-bar.css";
import "./hero-video.css";
const S = "/samples";
function HeroVideoDemo() {
const dialog = useRef<HTMLDialogElement>(null);
const poster = useRef<HTMLButtonElement>(null);
const frame = useRef<HTMLDivElement>(null);
const [open, setOpen] = useState(false);
const [closing, setClosing] = useState(false);
const [rect, setRect] = useState<DOMRect | null>(null);
const [error, setError] = useState("");
const uid = useId();
const target = () => {
const w = Math.min(window.innerWidth - 32, 960, (window.innerHeight - 120) * 1.6);
const h = w / 1.6;
return new DOMRect((window.innerWidth - w) / 2, (window.innerHeight - h) / 2 - 20, w, h);
};
const show = () => {
const d = dialog.current;
if (!d || !poster.current) return;
const to = target();
setRect(to);
setError("");
setOpen(true);
d.showModal();
const from = poster.current.getBoundingClientRect();
requestAnimationFrame(() => {
if (frame.current) void growBetween(frame.current, from, to, 450, ["14px", "10px"]);
});
};
const hide = async () => {
const d = dialog.current;
if (!d || !open || closing) return;
setClosing(true);
frame.current?.querySelector("video")?.pause();
if (frame.current && poster.current) await growBetween(frame.current, frame.current.getBoundingClientRect(), poster.current.getBoundingClientRect(), 300, ["10px", "14px"]);
d.close();
setOpen(false);
setClosing(false);
poster.current?.focus();
};
return (
<div className="dm">
<button ref={poster} type="button" className="hv-poster" aria-haspopup="dialog" onClick={show} data-hidden={open || undefined}>
<img src={`${S}/kitchen-oak.svg`} alt="" />
<span className="hv-play" aria-hidden="true">
<svg viewBox="0 0 24 24" width="22" height="22">
<path d="M8 5.5v13l10.5-6.5z" fill="currentColor" />
</svg>
</span>
<span className="hv-cap">
<strong>Watch the finished kitchen</strong>
<span>0:06 · loads only when you press play</span>
</span>
</button>
<dialog
ref={dialog}
className="lb"
aria-labelledby={`${uid}-t`}
data-closing={closing || undefined}
onCancel={(e) => {
e.preventDefault();
void hide();
}}
onClick={(e) => {
if (e.target === e.currentTarget) void hide();
}}
>
<h2 id={`${uid}-t`} className="sr-only">
The finished kitchen, video
</h2>
{open && rect && (
<div ref={frame} className="hv-frame" style={{ left: rect.left, top: rect.top, width: rect.width, height: rect.height }}>
<video
controls
autoPlay
playsInline
poster={`${S}/kitchen-oak.svg`}
>
<source src={`${S}/kitchen-tour.webm`} type="video/webm" />
{/* The last source failing means none could play. */}
<source src={`${S}/kitchen-tour.mp4`} type="video/mp4" onError={() => setError("The video could not be loaded. Try again later.")} />
</video>
</div>
)}
<div className="lb-bar">
<div className="lb-caption">
<strong>The finished kitchen</strong>
<span>{error || "Sample clip, made for this demo"}</span>
</div>
<div className="lb-controls">
<button type="button" className="lb-btn" data-wide onClick={() => void hide()}>
Close
</button>
</div>
</div>
</dialog>
</div>
);
}
export default function HeroVideoEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Video poster that opens the player" sample>
<HeroVideoDemo />
</PieceStage>
);
}