skip to content
mpkcn

~/components/media-lightbox

Media Lightbox

An accessible image and video lightbox with arrows, keyboard and swipe navigation, thumbnails, click-to-zoom and a focus trap.

$ open ./gallery · click a picture, then ← → and esc

Installation

terminal
npx mpkcn add media-lightbox
deps:none beyond react· tested with react@19.3.0

Usage

You own the open state and the thumbnails; the lightbox only shows the items. Set startIndex to the one that was clicked.

tsx
"use client";

import { useState } from "react";
import { MediaLightbox } from "@/components/mpkcn/media-lightbox";

const shots = [
  { src: "/shots/home.png", alt: "Home screen", caption: "home" },
  { src: "/shots/demo.mp4", type: "video" as const, poster: "/shots/demo.png", caption: "demo" },
];

export function Gallery() {
  const [open, setOpen] = useState(false);
  const [start, setStart] = useState(0);
  return (
    <>
      {shots.map((s, i) => (
        <button key={s.src} onClick={() => { setStart(i); setOpen(true); }}>
          open {s.caption}
        </button>
      ))}
      <MediaLightbox items={shots} open={open} onOpenChange={setOpen} startIndex={start} title="Screenshots" />
    </>
  );
}

By default it is a fullscreen overlay rendered into document.body, and the page behind it stops scrolling.

Examples

Inside a box

portal={false} fills the nearest positioned parent instead of the screen (give the parent position: relative). That is how the preview above works. onIndexChange keeps the gallery behind it in sync: the outlined thumbnail is the last one you looked at.

$ open ./gallery

No loop, no zoom

loop={false} stops at both ends (the arrows turn off), zoom={false} keeps images at their fitted size and thumbnails={false} hides the strip.

$ open ./gallery · click a picture, then ← → and esc

Props

PropTypeDefaultDescription
items{ src; type?; alt?; poster?; caption? }[]—Images and videos, in order.
openboolean—Whether the lightbox is shown.
onOpenChange(open: boolean) => void—Called when it wants to close (Esc, ✕, backdrop click).
startIndexnumber0Item shown first each time it opens.
onIndexChange(index: number) => void—Called whenever the shown item changes.
titlestring"Media"Top-bar title and accessible name of the dialog.
loopbooleantrueWrap from the last item to the first and back.
thumbnailsbooleantrueStrip of thumbnails at the bottom.
zoombooleantrueClick an image to zoom 2× on that spot.
portalbooleantrueFullscreen in document.body; false fills the positioned parent.
classNamestring—Extra classes for the dialog.

Accessibility

  • It is a role="dialog" with aria-modal, named by its title. Opening it focuses the close button; Tab and Shift+Tab stay inside it; closing puts focus back on the element that opened it.
  • ← → move, Home and End jump to the ends, Esc closes. While a video has focus, the arrow keys go to the video's own controls.
  • Every button has a label ("Previous", "Next", "Close", and each thumbnail's alt text); the current thumbnail has aria-current. A polite live region reads out the item and its position, e.g. "Mountains at dusk, 4 of 4".
  • Images use alt, then caption, then "Image n". Videos never autoplay.
  • Zoom and the thumbnail scroll are instant under prefers-reduced-motion.