A poster with a play button that opens a modal player. The modal is a native <dialog>: the button opens it through invoker commands (commandfor and command="show-modal"), so it works without JavaScript, and the browser handles focus, Esc and the inert page. A client island of about 0.5 kB starts the video right inside the click, so Safari allows sound, and pauses it on close. Without JavaScript the player opens with controls and the viewer presses play.
Browser Support
Displays the modal dialog
Controls the dialog with buttons
Animates the initial appearance
Animates closing
Controls how the dialog closes
Locks page scrolling while open
Styles the video dialog backdrop
Preview
Click the poster or focus it with Tab and press Enter. Close with Esc, the backdrop or the button.
Installation
Usage
import { VideoDialog } from "@/components/ui/video-dialog"
export default function Example() {
return (
<VideoDialog
src="/videos/walkthrough.mp4"
poster="/videos/walkthrough-poster.jpg"
title="Product walkthrough"
className="max-w-xl"
/>
)
}Examples
Hero block
className styles the preview: aspect ratio, radius, shadow. The player keeps its own 16:9 frame.
Listen to the sea
Twelve seconds of surf with sound. Click the preview to open the player.
<div className="grid items-center gap-6 sm:grid-cols-2">
<div>
<h3 className="text-2xl font-semibold">See it in two minutes</h3>
<p className="text-muted-foreground">A short tour of the editor.</p>
</div>
<VideoDialog
src="/videos/walkthrough.mp4"
poster="/videos/walkthrough-poster.jpg"
labels={{ play: "Play the walkthrough", close: "Close video" }}
className="aspect-[4/3] rounded-2xl shadow-lg"
/>
</div>Open state events
onOpenChange reports opening and closing. It works when VideoDialog is rendered from a client component. restartOnOpen plays from the start every time.
<VideoDialog
src="/videos/walkthrough.mp4"
poster="/videos/walkthrough-poster.jpg"
restartOnOpen
onOpenChange={(open) => console.log(open ? "opened" : "closed")}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| src | string | - | Video URL for the player |
| poster | string | - | Poster for the preview and the player |
| title | string | "Video" | Dialog title for screen readers, visually hidden |
| labels | { play?: string; close?: string } | - | Accessible names of the preview and close buttons |
| restartOnOpen | boolean | false | Play from the beginning on every open |
| onOpenChange | (open: boolean) => void | - | Called when the dialog opens or closes (client components only) |
| dialogClassName | string | - | Classes for the player frame in the dialog |
| className | string | - | Classes for the preview button |
Features
- Opens without JavaScript: native <dialog> with invoker commands, the close button is a <form method="dialog">
- Sound on Safari: the video is already in the page, so play() runs inside the click
- Muted fallback: if the browser still blocks sound, the video plays muted and controls stay available
- No preloading: preload="none": the video does not download until the dialog is opened
- Accessible modal: native focus trap, Esc, the page behind is inert, focus returns to the preview