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 { Lightbox, LightboxCaption, LightboxClose, LightboxContent, LightboxCounter, LightboxFooter, LightboxHeader, LightboxItem, LightboxMedia, LightboxNext, LightboxPrevious, LightboxSlide, LightboxSlides, LightboxTrigger, LightboxZoom, LightboxZoomIn, LightboxZoomOut } from "@/components/ui/chanhdai-lightbox"

export default function LightboxDemo() {
  return (
    <Lightbox>
      <div className="grid w-full max-w-xl grid-cols-2 gap-2 sm:grid-cols-4">
        {PHOTOS.map((photo, index) => (
          <LightboxTrigger
            key={photo.src}
            index={index}
            className="aspect-square overflow-hidden rounded-lg"
          >
            {({ imageRef }) => (
              <img
                ref={imageRef}
                className="size-full rounded-[inherit] object-cover outline -outline-offset-1 outline-foreground/10"
                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"
                aria-label={`${index + 1} of ${PHOTOS.length}`}
              >
                <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>
          <LightboxZoomOut />
          <LightboxZoomIn />
          <LightboxClose />
        </LightboxHeader>

        <LightboxPrevious />
        <LightboxNext />

        <LightboxFooter>
          <div className="flex flex-col items-center gap-0.5">
            <LightboxCaption />
            <LightboxCounter>
              {({ current, total }) => `${current} of ${total}`}
            </LightboxCounter>
          </div>
        </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/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/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