Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Video for Astro

Play video files and YouTube URLs with shared controls. Use the same component for background video.

0:00 / 0:00

Installation

bunx --bun shadcn@latest add @gnostic/video

Usage

Video detects YouTube watch, share, Shorts, live, and embed URLs. Other sources use a native video element, including local files and media URLs without a file extension.

---
import { Video } from "@/components/ui/video"
---

<Video src="/demo.mp4" poster="/demo.webp" title="Product demonstration" />
<Video src="https://www.youtube.com/watch?v=M7lc1UVf-VE" title="YouTube demonstration" />

Controls are enabled by default. Use the control bar to play, pause, seek, change the volume or playback speed, and enter fullscreen. The speed selector lists the rates available for the video. Both sources use the same controls. YouTube loads its IFrame Player API only when a YouTube player is present.

Click a native video frame to play or pause. Set clickToPlay={false} to turn this off. For a video without a control bar, set clickToPlay to turn it on. YouTube handles clicks inside its own frame.

Set download to show a download button for a native file. The button links to the source file. For a file on another origin, configure the file server to send Content-Disposition: attachment.

Poster play overlay

Set playOverlay to show a dark layer and a circle play icon over the media. The overlay starts playback. It disappears after playback starts and stays hidden on pause or completion. If playback is blocked, the overlay stays available for another attempt.

<Video src="/demo.mp4" poster="/demo.webp" playOverlay />

Use the play-overlay slot to replace the icon with your own text or artwork. The slot enables the overlay unless playOverlay={false} is set. Video supplies the button and its accessible label from title; do not put a button or link inside this slot.

<Video src="/demo.mp4" poster="/demo.webp" title="Product demonstration">
  <span slot="play-overlay" class="rounded-full border px-6 py-3">
    Play demonstration
  </span>
</Video>

The overlay also works with YouTube and controls={false}. It appears when the player is ready. The root uses data-started="true" after first playback; CSS hides the overlay from that state. The separate overlay slot remains available for persistent content.

YouTube URLs

0:00 / 0:00
0:00 / 0:00

YouTube Shorts use a portrait frame. Start times in start or t are preserved. The player uses the youtube-nocookie.com embed host. YouTube still makes external requests and controls its own branding, overlays, and media availability.

Background playback

Set controls={false} to remove the control bar. Set autoplay, muted, and loop separately. Set decorative only when the video adds no information. Use the overlay slot for content or a separate play/pause button.

Watch the Flow

Autoplay respects reduced motion. Native background files load when playback is requested. Supply a poster for a static fallback. pauseWhenHidden pauses the player outside the viewport or in a hidden tab. It resumes only if playback was requested before the player became hidden. A manual pause remains in effect.

YouTube background playback uses the same options, but YouTube can still show its own overlays. poster applies to native files only.

Separate play/pause button

Place VideoToggle in the overlay slot, or set its for prop to the Video ID. It accepts Button variants and sizes. Named play and pause slots replace the button content.

The controller creates missing player and trigger IDs and sets aria-controls. It preserves IDs you supply. A toggle inside Video needs no for prop. A detached toggle needs a matching, unique Video ID. No preview-specific ID changes are required.

Captions

Pass native <track> elements through the default slot. A captions button appears when native caption or subtitle tracks are available. Repeated clicks select each track, then turn captions off. Choose YouTube videos with accurate captions; YouTube manages its caption settings, including its c keyboard shortcut when the iframe has focus.

<Video src="/demo.mp4" title="Product demonstration" crossorigin="anonymous">
  <track kind="captions" src="/demo.en.vtt" srclang="en" label="English" default />
</Video>

Keyboard and fallback behavior

Focus the player and press Space to play or pause. Press Left or Right Arrow to seek five seconds. Buttons use normal keyboard activation. Sliders support arrow keys, Home, End, and Page Up/Down. The playback slider reports the selected time as you drag.

The frame and control bar appear while the player loads. A spinner shows when the player is starting or buffering. With JavaScript disabled, the video uses browser controls. If the YouTube API fails, the iframe shows its own controls.

API Reference

Video slots

PropType
play-overlay?Astro content

Sets the content of the poster play button. Supplying this slot enables the button unless playOverlay is false.

TypeAstro content
DefaultPlay icon when playOverlay is true
overlay?Astro content

Places content above the player. It can stay visible during playback.

TypeAstro content

VideoToggle slots

PropType
play?Astro content

Sets the visible button content when the reader can start playback.

TypeAstro content
DefaultplayLabel prop
pause?Astro content

Sets the visible button content when the reader can pause playback.

TypeAstro content
DefaultpauseLabel prop

Migration from BackgroundVideo

Replace BackgroundVideo and BackgroundVideoMedia with one Video component. Replace BackgroundVideoToggle with VideoToggle. Set controls={false}, autoplay, muted, and loop explicitly for background playback. Use mediaClass for media positioning and the overlay slot for content above the video.

Native video attributes include src, poster, controls, autoplay, muted, loop, playsinline, preload, and crossorigin. controls and playsinline default to true; autoplay, mute, and loop default to false. Playback options also apply to YouTube where supported. Other native media attributes apply to native files only. class, style, and id apply to the player root. title labels the media. The root uses the selected preset.

PropType
playOverlay?boolean

Show a play overlay until playback starts. The play-overlay slot enables it unless this prop is false.

Typeboolean
Defaultfalse
clickToPlay?boolean

Toggle playback when the user clicks a native video frame. Defaults to true for a non-decorative video with controls.

Typeboolean
download?boolean

Show a download link for the native video file.

Typeboolean
Defaultfalse
mediaClass?string

Classes for the video or iframe, such as object-cover or object-position.

Typestring
pauseWhenHidden?boolean

Pause outside the viewport or in a hidden tab. A manual pause remains in effect.

Typeboolean
Defaultfalse
decorative?boolean

Hide decorative media from assistive technology. Does not change playback.

Typeboolean
Defaultfalse
PropType
for?string

ID of the Video to control. Omit when the button is inside the Video overlay slot.

Typestring
playLabel?string

Label and default text when paused.

Typestring
Default"Play"
pauseLabel?string

Label and default text when playing.

Typestring
Default"Pause"

Read the installed source for the complete component.

The player uses an HTML root and a factory. The component initializes on script load and astro:page-load, then stops playback and destroys its controller before an Astro page swap. For direct DOM changes, import createVideo from the component index and call controller.destroy() before removing the player.