The Gallery component displays images in a responsive grid layout with PhotoSwipe lightbox functionality. It automatically loads images from a gallery/ subfolder and supports custom captions, sorting options, and responsive column configuration.
Key Features
- 📸 PhotoSwipe Integration — full-screen lightbox with zoom, pan, and swipe gestures
- 🔄 Auto-loading — automatically discovers images in a
gallery/subfolder - 📝 Custom Captions — via
captions.jsonor auto-generated from filenames - 🎨 Responsive Grid — configurable columns that adapt to screen size
- ⌨️ Keyboard Navigation — arrow keys, ESC, and full accessibility support
Live Examples
Default usage with auto-loading
In MDX files, use import.meta.glob to load images automatically. The GalleryMdx component processes the raw output and applies custom captions if available.
This demo loads images from ./gallery/*.jpg (and other formats) and captions from ./gallery/captions.json.
// At the top of your MDX file:import { GalleryMdx, processGalleryImages } from '@core/components/Gallery';
export const galleryImages = import.meta.glob( './gallery/*.{jpg,jpeg,png,gif,webp,avif}', { eager: true });export const captionsData = import.meta.glob('./gallery/captions.json', { eager: true });export const captions = Object.values(captionsData)[0]?.default;
// In your content:<GalleryMdx images={processGalleryImages(galleryImages, { captions })} client:idle/>📄 View captions.json structure
Place this file in your gallery/ folder to provide custom captions:
{ "photo1.jpg": "A short caption for this image", "photo2.jpg": "Another caption"}Tip: keys can be filenames with or without extensions. Missing captions fall back to auto-generated ones from the filename.
Images by Pixabay
With reverse sorting
Images are sorted alphabetically by filename by default. Use the reverseSorting option to reverse this:
<GalleryMdx images={processGalleryImages(galleryImages, { captions, reverseSorting: true })} client:idle/>Custom column configuration
Control the responsive grid by specifying minimum item width and maximum columns. The grid adapts between these values based on screen width:
<GalleryMdx images={processGalleryImages(galleryImages, { captions })} columns={{ min: 200, max: 3 }} client:idle/>Without custom captions (filename-based)
When no captions.json is provided, the component automatically generates readable captions from filenames:
<GalleryMdx images={processGalleryImages(galleryImages)} client:idle/>Usage in Astro Files
For .astro files, use the Gallery Astro wrapper which handles image loading automatically:
---import Gallery from '@core/components/Gallery/Gallery.astro';---
<Gallery />The wrapper automatically:
- Detects the current page path
- Loads images from a
./gallery/subfolder (jpg, jpeg, png, gif, webp, avif) - Loads captions from
./gallery/captions.jsonif present - Optimizes images at build time via Astro
Folder Structure
Place your images next to your MDX or Astro page file:
your-page.mdx ← or your-page/index.mdxgallery/ photo1.jpg photo2.jpg captions.json ← optionalThe import.meta.glob path must be a static string literal relative to the MDX file — variables are not supported.
Props API
GalleryMdx Props
| Prop | Type | Default | Description |
|---|---|---|---|
images | GalleryImage[] or Record<string,any> | — | Processed image array or raw import.meta.glob output |
columns | { min?: number, max?: number } | { min: 200, max: 1 } | min = minimum item width in px; max = maximum column count |
className | string | — | Additional CSS classes |
processGalleryImages Options
| Option | Type | Default | Description |
|---|---|---|---|
captions | Record<string,string> | — | Custom captions keyed by filename |
reverseSorting | boolean | false | Reverse the default alphabetical sort |
GalleryImage Type
interface GalleryImage { src: string; // Full-size image URL thumb: string; // Thumbnail URL width: number; height: number; alt: string; caption?: string;}



