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

Lightbox Grid for Astro

Responsive Astro gallery grids with normal grid, bento, and masonry layouts.

The default grid fills each row. On a wide screen, these six images form two rows of three. Select a thumbnail to open the full image.

Installation

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

Usage

Pass one image object to each LightboxGridItem. Both views use its source and alt text. Select an image to zoom in. Add a caption to open the image and its caption in a dialog.

---
import { LightboxGrid, LightboxGridItem } from "@/components/ui/lightbox-grid"
import lake from "../assets/lake.jpg"

const images = [{ src: lake, alt: "Mountains reflected in a lake" }]
---

<LightboxGrid>
    {images.map((image) => <LightboxGridItem image={image} />)}
</LightboxGrid>

To migrate an existing item, move its alt to image.alt and move the two named slots’ image props to image.thumbnail and image.content.

Thumbnail and enlarged image

The grid uses object-cover to crop the thumbnail inside its tile. With no caption or image.content override, the item renders one image. On zoom, LightboxImage uses the same source and responsive candidates with sizes="90vw". The enlarged image keeps the source aspect ratio.

Set CSS classes on image.thumbnail to control the crop. For example, this masonry tile shows a square thumbnail with the top of the photo in view.

<LightboxGridItem image={{
    src: lake,
    alt: "Mountains reflected in a lake",
    thumbnail: { class: "aspect-square object-top" },
}} />

Use image.content when the enlarged view needs its own Astro Image props, such as different widths or quality. If you use Astro to crop the thumbnail file itself, set content: {} to zoom the original source. Captioned items render the enlarged view in a dialog.

A local import supplies its dimensions. For a string source, set width and height on image. For an allowed remote URL, Astro can read the dimensions with inferSize: true.

Default Grid

The default grid keeps images in source order and gives each row the same height. At three columns, seven images form rows of three, two, and two. At two columns, a final image on its own fills the row. The grid chooses the tile widths automatically.

Bento Grid

Set layout="bento" to make the root a CSS grid. Its default column template uses repeat(auto-fill, minmax(...)). Each item occupies one cell unless you give it a span. Use class on the grid to set your own column template and class on an item to set its row or column span.

<LightboxGrid layout="bento" class="grid-cols-[repeat(3,minmax(0,1fr))]">
    <LightboxGridItem image={lead} class="col-span-2 row-span-2" />
    {images.map((image) => <LightboxGridItem image={image} />)}
</LightboxGrid>

The example below uses the named span options.

The thumbnail fills its bento cell. Set image.thumbnail.class to change the crop position.

Masonry Grid

Use the reusable Masonry Grid with LightboxGridItem, as this example shows. LightboxGrid layout="masonry" also uses that same component. Each item goes into the shortest column. span, rowHeight, and minRows have no effect on masonry.

Masonry thumbnails use their natural aspect ratio. This example sets different thumbnail crops and opens the full source image on zoom.

Drag the example’s right handle, or focus it and use the arrow keys.

CSS columns

For a gallery that reads down each column, place the items in a CSS multi-column container. The browser balances the columns. Each tile keeps its natural height, and break-inside-avoid keeps the tile in one column.

<div class="columns-1 gap-3 sm:columns-2 lg:columns-3">
    {images.map((image) => (
        <LightboxGridItem image={image} class="mb-3 break-inside-avoid" />
    ))}
</div>

API

LightboxGrid accepts native div attributes and these options:

LightboxGridProps

PropType
layout?'grid' | 'bento' | 'masonry'

Full rows of equal-height tiles, mixed bento spans, or natural-height masonry items.

Type'grid' | 'bento' | 'masonry'
Default'grid'
minCols?number

Minimum column count. Use a positive integer.

Typenumber
Default1
minRows?number

Minimum row count for grid and bento. Use a positive integer.

Typenumber
Default1
rowHeight?string

The height of each row in the grid as a CSS value

Typestring
Default'14rem' // 224px
gap?string

A valid CSS gap value

Typestring
Default'0.5rem'

With minCols={1}, the regular grid uses one column below 24rem, two from 24rem, and three from 32rem. Bento fits tracks to the available width. Masonry switches to two columns at 30rem and four at 48rem. Each breakpoint uses the container width. minCols sets a lower limit.

Both counts accept positive integers. Set minCols={6} and minRows={5} to reserve five rows and fit six images in a full row. More images create additional rows. The last row shares its width among the remaining images. Bento uses the requested number of columns for its tile spans. Masonry uses the column count and sizes its height from the items.

Set --lightbox-grid-gap and --lightbox-grid-row-height in style to override the corresponding props. rowHeight sizes both explicit and automatic rows in grid and bento layouts.

LightboxGridItemProps

PropType
imageLightboxItemImage

Shared image source and description, with optional Astro Image props for each view.

TypeLightboxItemImage
caption?string

Show this text below the image in a dialog. Omit it for image zoom.

Typestring
span?'auto' | 'square' | 'wide' | 'tall' | 'large'

Number of cells the tile occupies in a bento grid.

Type'auto' | 'square' | 'wide' | 'tall' | 'large'
hoverZoom?boolean

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

Typeboolean
Defaulttrue

In bento, wide spans two columns, tall spans two rows, and large spans both from a container width of 24rem. auto and square occupy one cell. Use item classes for other spans or breakpoints. Bento fills empty cells, so visual order can differ from source order.

The registry source includes both parts.

Items without a caption use LightboxImage. Press Enter or Space to zoom in. Click again or press Escape to close. A captioned item opens a dialog with the image description as its title. See Lightbox.

When to use Lightbox Grid

Use LightboxGrid to arrange related images before opening their detail views. Choose the layout and minimum column count from the supported props. Set row height and gap to suit the image proportions.

Keyboard and behavior

Each image trigger needs a clear name and visible focus. Keep the DOM order meaningful even when the grid has varied spans. The visual layout should not be used to change the reading order of a story.

Theme and layout

Container queries adapt the grid to its available width. Check it inside narrow cards as well as full-page sections. Supply image dimensions and intentional crops; the grid cannot restore detail removed by a crop.