Chánh Đại

Lightbox

Component

About

Image viewer that morphs from the thumbnail, with swipe, zoom, and pull to close. From chanhdai.com (ncdai), MIT.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Lightbox to your project and fits it to your theme.

Code

How Lightbox is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client"

import { cn } from "@/lib/utils"
import { Lightbox, LightboxCaption, LightboxClose, LightboxContent, LightboxFooter, LightboxHeader, LightboxItem, LightboxMedia, LightboxNext, LightboxPrevious, LightboxSlide, LightboxSlides, LightboxThumbnail, LightboxThumbnailStrip, LightboxThumbnailStripTrack, LightboxTrigger, LightboxZoom } from "@/components/ui/chanhdai-lightbox"

export default function LightboxThumbnailsDemo() {
  return (
    <Lightbox>
      <div className="grid w-full max-w-xl grid-cols-2 gap-2 sm:grid-cols-3">
        {PHOTOS.map((photo, index) => (
          <LightboxTrigger
            key={photo.src}
            index={index}
            className={cn(
              "overflow-hidden rounded-lg",
              index === 0 && "col-span-2 sm:col-span-3"
            )}
          >
            {({ imageRef }) => (
              <img
                ref={imageRef}
                className="block h-auto w-full rounded-[inherit] outline -outline-offset-1 outline-foreground/10"
                // The wide cover tile needs the full-size image to stay sharp.
                src={index === 0 ? photo.src : photo.thumbnail}
                alt={photo.alt}
                width={photo.width}
                height={photo.height}
              />
            )}
          </LightboxTrigger>
        ))}
      </div>

      <LightboxContent aria-label="Photos of Vietnam">
        <LightboxSlides aria-label="Photos" aria-roledescription="carousel">
          {PHOTOS.map((photo, index) => (
            <LightboxSlide key={photo.src}>
              <LightboxItem
                index={index}
                caption={photo.caption}
                aria-roledescription="slide"
              >
                <LightboxZoom>
                  <LightboxMedia width={photo.width} height={photo.height}>
                    <img
                      src={photo.src}
                      alt={photo.alt}
                      width={photo.width}
                      height={photo.height}
                      draggable={false}
                    />
                  </LightboxMedia>
                </LightboxZoom>
              </LightboxItem>
            </LightboxSlide>
          ))}
        </LightboxSlides>

        <LightboxHeader>
          <LightboxClose />
        </LightboxHeader>

        <LightboxPrevious />
        <LightboxNext />

        <LightboxFooter>
          <LightboxCaption />
          <LightboxThumbnailStrip>
            <LightboxThumbnailStripTrack aria-label="Photo thumbnails">
              {PHOTOS.map((photo, index) => (
                <LightboxThumbnail key={photo.src} index={index}>
                  <img src={photo.thumbnail} alt={photo.caption} />
                </LightboxThumbnail>
              ))}
            </LightboxThumbnailStripTrack>
          </LightboxThumbnailStrip>
        </LightboxFooter>
      </LightboxContent>
    </Lightbox>
  )
}

const PHOTOS = [
  {
    src: "https://cdn.21st.dev/assets/mirror/7c/7c8b11add499c8a22dbeb694c3dbd91caa0901eab26f8473be4641b072354aba.webp",
    thumbnail:
      "https://cdn.21st.dev/assets/mirror/fa/fab2b74669d8526a2c2c2fbc44628a771d33023a6d611369ef7b2227fed97039.webp",
    alt: "Limestone islands rising from turquoise water in Ha Long Bay, with tour boats near a sandy beach.",
    caption: "Ha Long Bay, Quang Ninh",
    width: 2048,
    height: 1152,
  },
  {
    src: "https://cdn.21st.dev/assets/mirror/df/dfb0f4bcd711984e9a79a79066e06e247ca65c79b315f8e1912409a8312e8bf5.webp",
    thumbnail:
      "https://cdn.21st.dev/assets/mirror/95/953a5f351c499eb897d60d648a9c59671cb6a767a91ff2d5969bfbb2fed684fd.webp",
    alt: "A narrow waterway under an arch of cajuput trees, their trunks reflected in the still green water.",
    caption: "Tra Su Forest, An Giang",
    width: 2048,
    height: 1152,
  },
  {
    src: "https://cdn.21st.dev/assets/mirror/cf/cf7b7f830618e010b1257c071fe3e74d29b6696611606b1573e3ca327bdec926.webp",
    thumbnail:
      "https://cdn.21st.dev/assets/mirror/91/9111fdaa776a76ae18a762c7cc24886c3cbdebc89a616466f013afd599016329.webp",
    alt: "A traditional pavilion on the water at the foot of steep limestone mountains.",
    caption: "Trang An, Ninh Binh",
    width: 2048,
    height: 1152,
  },
  {
    src: "https://cdn.21st.dev/assets/mirror/f4/f47819b7f3934ae0f5e72d0466d10dda31882ff8da1e027d6e32b8391ddf1cd2.webp",
    thumbnail:
      "https://cdn.21st.dev/assets/mirror/a3/a34c4f98782a19e3d40f2f61e43221d3595bb2ac0d6cd8326c1808ba0e8ed6e2.webp",
    alt: "A gate tower with tiered roofs on top of an old stone wall, framed by bare trees.",
    caption: "Imperial City, Hue",
    width: 2048,
    height: 1152,
  },
  {
    src: "https://cdn.21st.dev/assets/mirror/e8/e8180de00c4046855323dc6a773f2e380aee1af94c1578b43931931b499fb244.webp",
    thumbnail:
      "https://cdn.21st.dev/assets/mirror/c8/c8695fd0d0f1f9f8100aa7dad71491d59afb35a1c76e5a58f0b896f96f2e551a.webp",
    alt: "An aerial view of a pagoda complex among trees, with a lake and mountains in the distance.",
    caption: "Bai Dinh Pagoda, Ninh Binh",
    width: 2048,
    height: 1152,
  },
  {
    src: "https://cdn.21st.dev/assets/mirror/46/4615281751adc8255c5b8f8f8c33c02448b6032c6caeb8f711292fc06bd5c7be.webp",
    thumbnail:
      "https://cdn.21st.dev/assets/mirror/9c/9c3cde1f4b4bd09c278e1226706d07ff712dae011cc8fa6d7ebf3a40534f7259.webp",
    alt: "Skyscrapers along the Saigon River at sunset, with the Bitexco Financial Tower in the middle.",
    caption: "Saigon River, Ho Chi Minh City",
    width: 2048,
    height: 1152,
  },
  {
    src: "https://cdn.21st.dev/assets/mirror/dd/ddb12fb91a78771c3362878d2580a2f70e9a10668e63da791b76132038a92562.webp",
    thumbnail:
      "https://cdn.21st.dev/assets/mirror/24/241ab7b58d63a39e108ee06298f2c37faa530f483272660c2c9f02bf127efdf3.webp",
    alt: "The Dragon Bridge lit up at night under a purple sky, reflected in the river.",
    caption: "Dragon Bridge, Da Nang",
    width: 2048,
    height: 1152,
  },
]

Dependencies

More by Chánh Đại

View all
  • Apple Hello Effect

    Apple Hello Effect

    692
  • Theme Switcher

    Theme Switcher

    364
  • Hero Golden Spiral

    Hero Golden Spiral

    121
  • Mobius Loop Icon

    Mobius Loop Icon

    90
  • Wheel Picker

    Wheel Picker

    79
  • Testimonial 2

    Testimonial 2

    50
  • Glow Card Grid

    Glow Card Grid

    49
  • Spinning Circular Text

    Spinning Circular Text

    26