Searching Open Sourcery

Type to search Open Sourcery.

Media & Content

Responsive Video

Swaps video sources and poster images across breakpoints.

v0.2.0@magic-spells/responsive-video

$ npm i @magic-spells/responsive-video

GitHubnpm

Usage

<responsive-video
  mobile-video="https://cdn.example.com/video-portrait.mp4"
  desktop-video="https://cdn.example.com/video-landscape.mp4"
  mobile-poster="https://cdn.example.com/poster-portrait.jpg"
  desktop-poster="https://cdn.example.com/poster-landscape.jpg"
  breakpoint="900"
>
  <video autoplay muted playsinline loop></video>
</responsive-video>

About

A lightweight, zero-dependency web component that intelligently swaps video sources and poster images based on viewport width. Built for performance-critical hero sections and modern web applications where mobile users should never download desktop assets (and vice versa).

Features

  • Loading only what's needed: Mobile users download only mobile videos and posters, desktop users get desktop assets
  • Automatic switching: Responds to viewport changes and updates the active source seamlessly
  • Performance-first: Uses requestAnimationFrame for resize throttling and passive event listeners
  • Framework-agnostic: Pure web component that works with any framework or no framework at all
  • Tiny footprint: Single class, no dependencies, ~2KB minified

Attributes

AttributeTypeDefaultDescription
mobile-videoString—Video source URL for viewports narrower than the breakpoint
desktop-videoString—Video source URL for viewports equal to or wider than the breakpoint
mobile-posterString—Poster image URL for viewports narrower than the breakpoint (optional)
desktop-posterString—Poster image URL for viewports equal to or wider than the breakpoint (optional)
breakpointNumber768Viewport width (in pixels) where the switch between mobile and desktop occurs

About, features, usage and options are taken from the package README.