Lightbox for Astro
Astro image zoom and media previews built on Dialog.
Usage
---
import { Image } from "astro:assets"
import { Lightbox, LightboxContent, LightboxTrigger } from "@/components/ui/lightbox"
import lake from "../assets/lake.jpg"
---
<Lightbox>
<LightboxTrigger hoverZoom aria-label="Enlarge mountain lake">
<Image src={lake} alt="Mountains reflected in a lake" />
</LightboxTrigger>
<LightboxContent label="Mountain lake" caption="A quiet morning.">
<Image src={lake} alt="Mountains reflected in a lake" loading="lazy" />
</LightboxContent>
</Lightbox>
Lightbox is a direct export of Dialog. LightboxTrigger is a thumbnail
button. LightboxContent includes the overlay, close button, hidden accessible
title, media container, and optional caption.
Set label to name the dialog. It does not replace the image’s alt text.
hoverZoom adds a small preview scale on hover and respects reduced motion.
Keep links and buttons out of LightboxTrigger; it is already a button.
Captions and media
Use caption for plain text. Use the caption slot for links or other markup.
The slot takes precedence over the prop.
<LightboxContent label="Mountain lake">
<Image src={lake} alt="Mountains reflected in a lake" loading="lazy" />
<span slot="caption">Photograph by <a href="#photographer">Alex Morgan</a>.</span>
</LightboxContent>
The default slot accepts an image, video, iframe, or custom layout. Put media
directly in the slot. mediaClass changes the inner container and its direct
child sizing; class changes the dialog panel.
Direct images, pictures, and videos stay centered and keep their aspect ratio. The media container fits around them and caps their width at 90% of the viewport or the available width, whichever is smaller. In the default variant, click beside the media to close the lightbox. Carousels, iframes, and custom layouts use the full media width.
<LightboxContent
label="Document preview"
mediaClass="mx-auto max-w-3xl *:h-[min(70dvh,720px)]"
>
<iframe src={previewUrl} title="Document preview" class="border-0" />
</LightboxContent>
Give iframes a title and an explicit height or aspect ratio. The lightbox does not stop video or iframe playback when it closes.
Image loading
Use separate thumbnail and enlarged images so each can have suitable dimensions
and sizes. Astro can reuse matching image transforms. Keep the enlarged image
lazy. If you use srcset, specify the expected display width in sizes.
With sizes="auto", the browser can use a 300px fallback for an image that
determines its own width. Use priority only for a key thumbnail visible on
initial page load.
See Astro’s image options.
Panel view
The default variant uses a dark backdrop and fills the screen. Set
variant="panel" for a centered panel.
Installation
bunx --bun shadcn@latest add @gnostic/lightboxnpx shadcn@latest add @gnostic/lightboxpnpm dlx shadcn@latest add @gnostic/lightboxCopy the files
src/components/ui/lightbox/index.tssrc/components/ui/lightbox/types.tssrc/components/ui/lightbox/lightbox-item-images.tssrc/components/ui/lightbox/lightbox-image.astrosrc/components/ui/lightbox/lightbox-content.astrosrc/components/ui/lightbox/lightbox-trigger.astrosrc/components/ui/lightbox/lightbox-variants.ts
Simple image zoom
Use LightboxImage for a single image that expands from its position on the page.
It uses a small browser script and has no Dialog container. It accepts Astro
Image props, including src, alt, widths, and sizes.
By default, the zoom uses the thumbnail image. Pass Astro Image props in
zoomImage to use a separate crop, size, or source for the enlarged view.
The browser loads this image when the zoom opens.
Click the image, or use Enter or Space, to open it. Click again or press Escape to close it. Scrolling, resizing, and page changes also close the zoom. The animation respects reduced motion. Use the Dialog parts above for captions and other media, or Lightbox Carousel for a gallery.
Custom composition
Use standard Dialog parts for custom titles, controls,
and panels. LightboxContent supplies its own title and caption; do not add a
second DialogTitle or DialogDescription inside it. Set showCloseButton={false}
only when the content includes another close control.
Lightbox Carousel adds navigation between items. Lightbox Grid arranges independent lightboxes.
API reference
Source: Lightbox registry item.
LightboxProps
Root options are inherited from Dialog.
LightboxImageProps
zoomImage?LocalImageProps | RemoteImageProps
Image to show on zoom. By default the zoom uses the thumbnail source.
LocalImageProps | RemoteImagePropsLightboxItemImage
Grid and carousel items share this image prop. Both use the source and alt text
for the thumbnail and the enlarged view. Use thumbnail or content for
per-view Astro Image props.
srcImageMetadata
ImageMetadatawidth?number
numberheight?number
numberaltstring
Description of the image and accessible name for its control.
stringthumbnail?ViewProps
Astro Image props for the thumbnail. Use CSS classes to crop the visible area.
ViewPropscontent?ViewProps
Astro Image props for the enlarged view.
ViewPropsLightboxContent slots
caption?Astro content
Places a caption below the media. Use this slot to include links or other markup.
Astro contentcaption propLightboxContentProps
labelstring
Accessible name, rendered as a visually hidden dialog title.
stringcaption?string
Text below the media. The caption slot takes precedence.
stringmediaClass?string
Classes for the media container.
stringvariant?LightboxPopupVariantProps['variant']
`panel` centers the media in a dialog. `default` fills the screen and uses a dark backdrop.
LightboxPopupVariantProps['variant']'default'showCloseButton?boolean
Show the close button.
booleantrueoverlayClass?string
Extra classes for the backdrop.
stringLightboxTriggerProps
hoverZoom?boolean
Scale the preview on hover. Adds the class `hover:scale-105`.
booleanfalseMigration
Replace LightboxTitle with the label prop. Put media directly inside
LightboxContent and move any LightboxMedia classes to mediaClass. Replace
LightboxDescription with caption or the caption slot. Rename
variant="viewport" to variant="default" or omit it. For custom panels,
import DialogPortal, DialogOverlay, and DialogClose from Dialog.
Keyboard and layout
Dialog manages focus, Escape, outside clicks, and scroll locking. A close control must remain available. Check tall media and long captions on a phone.


