Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Lightbox Carousel for Astro

A carousel with items that when clicked, open the carousel in a lightbox dialog, allowing for a larger view with the same carousel layout.

Installation

bunx --bun shadcn@latest add @gnostic/lightbox-carousel

Usage

Use LightboxCarousel and LightboxCarouselItem with the normal Carousel content and controls. Pass one image with src and alt to each item. The thumbnail uses a 3:2 CSS crop. The dialog shows the full image.

---
import { CarouselContent, CarouselNext, CarouselPrevious } from "@/components/ui/carousel"
import { LightboxCarousel, LightboxCarouselItem } from "@/components/ui/lightbox-carousel"
import lake from "../assets/lake.jpg"
---

<LightboxCarousel label="Landscape gallery">
  <CarouselContent>
    <LightboxCarouselItem
      image={{ src: lake, alt: "Mountains reflected in a lake" }}
      caption="A quiet morning."
    />
  </CarouselContent>
  <CarouselPrevious />
  <CarouselNext />
</LightboxCarousel>

A click opens the selected item. Previous and Next in the dialog move one item at a time. Closing the dialog selects the page scroll group that contains the viewed item. Set image.thumbnail or image.content to pass Astro Image props to one view. For example, thumbnail: { class: "aspect-square" } gives the preview a square crop. A local import supplies its dimensions. For a URL, set width and height on image, or use inferSize: true for an allowed remote host.

Several slides and effects

Set each item’s basis to show several slides. Use carouselOptions for Embla options that can pass through JSON and effects for the page effects. The Carousel option limits also apply here. This example scrolls two page items at a time and applies a small scale effect.

Each LightboxCarouselItem includes a CarouselLayer around its trigger, so arc, scale, and fade effects work without an extra layer. For inner parallax, put CarouselParallax inside the optional preview slot. Allow space for an arc through CarouselContent’s viewportClass prop. See Carousel effects.

The enlarged view is horizontal and uses one item per snap. It inherits the page’s loop setting and current direction. Page grouping, orientation, and effects do not change its media layout.

Use gap on CarouselContent for spacing. CarouselDots and the standard previous and next controls also work in the page carousel.

API reference

Source: Lightbox Carousel registry item.

LightboxCarouselProps

PropType
label?string

Accessible name for the page carousel.

Typestring
Default'Image gallery'

The root also accepts the shared Carousel options:

PropType
carouselOptions?CarouselEmblaOptions

JSON-compatible Embla options. Orientation and Astro parts control the axis and elements.

TypeCarouselEmblaOptions
orientation?'horizontal' | 'vertical'

Scroll axis for the carousel.

Type'horizontal' | 'vertical'
Default"horizontal"
effects?CarouselEffects

Independent effects applied to the optional inner parts.

TypeCarouselEffects

LightboxCarouselItem slots

PropType
preview?Astro content

Renders inside the slide button that opens the lightbox.

TypeAstro content
content?Astro content

Renders in the lightbox when the reader opens the slide.

TypeAstro content

LightboxCarouselItemProps

PropType
imageLightboxItemImage

Image source and alt text, with optional props for the thumbnail and enlarged view.

TypeLightboxItemImage
caption?string

Optional text under the enlarged image.

Typestring
hoverZoom?boolean

Scale the slide preview on hover. The preview uses `hover:scale-105`.

Typeboolean
Defaulttrue

Each item accepts native div attributes. Use the optional slots for custom markup. The image prop supplies both views by default and names the slide button and dialog.

Client control

The component initializes itself. Use the controller to open an item from a client script:

<script>
  import { createLightboxCarousel } from "@/components/ui/lightbox-carousel/lightbox-carousel.ts"

  const root = document.querySelector<HTMLElement>("#landscapes")
  if (root) {
    const gallery = createLightboxCarousel(root)
    // Item indexes start at zero. This opens the third image.
    gallery.open(2)
    // gallery.close()
  }
</script>

Set id="landscapes" on the root for this example. Repeated setup calls return the same controller. create(scope) binds unbound galleries in that scope. Call destroy() before removing a gallery that your script initializes. Automatic setup cleans up on Astro navigation and when a root is removed.

The enlarged carousel and its media stay in templates until the first open. The carousel then remains available for later opens. No global window flag is used.

Enlarged images use lazy loading by default. Once the dialog opens, the browser can load nearby slides in advance. This is not a guarantee that only the selected image downloads. Use smaller images for previews and set sizes to match their display width.

Migration

Move the slide’s source and alt text to image. Move thumbnail options to image.thumbnail and enlarged image options to image.content. Remove the two image slots when the item can render both images.

Rename opts to carouselOptions. Replace LightboxCarouselContent, LightboxCarouselPrevious, and LightboxCarouselNext with the matching exports from Carousel. Remove old zero-gap overrides if you want the normal Carousel spacing; use gap="0" for a flush track.

Keyboard and behavior

Arrow keys move the open carousel, including when the close button has focus. Direction follows RTL settings. Editable fields keep their own arrow keys. Reduced motion disables animated keyboard and button movement. Escape, the close button, and an overlay click close the dialog. Focus returns to the thumbnail that opened it. Dragging a thumbnail does not open the dialog.

Use suitable thumbnail and full-size sources to limit image transfer size. Test captions, controls, and tall images at narrow widths.