Lightbox gallery
A masonry photo grid where any photo zooms out of its tile into a full-screen lightbox, with arrows, swipe, a thumbnail strip and keyboard support.
"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import "./lightbox-gallery.css";
type Photo = {
src: string;
alt: string;
caption: string;
ratio: string;
tone: string;
};
const PHOTOS: Photo[] = [
{
src: "/images/components-lightbox-gallery-photo-01.jpg",
alt: "Concrete stairs in hard afternoon light",
caption: "Stairwell, Porto",
ratio: "4 / 5",
tone: "#c9b8a0",
},
{
src: "/images/components-lightbox-gallery-photo-02.jpg",
alt: "A fishing boat on a calm blue sea",
caption: "Morning boat, Hydra",
ratio: "3 / 2",
tone: "#4f78a8",
},
{
src: "/images/components-lightbox-gallery-photo-03.jpg",
alt: "Neon shop signs reflected on a wet street",
caption: "After rain, Osaka",
ratio: "2 / 3",
tone: "#5b3f8f",
},
{
src: "/images/components-lightbox-gallery-photo-04.jpg",
alt: "Desert dunes with a single line of footprints",
caption: "Dunes, Erg Chebbi",
ratio: "3 / 2",
tone: "#d08a4f",
},
{
src: "/images/components-lightbox-gallery-photo-05.jpg",
alt: "A ceramic cup on a linen tablecloth",
caption: "Still life, studio",
ratio: "1 / 1",
tone: "#e6ddd0",
},
{
src: "/images/components-lightbox-gallery-photo-06.jpg",
alt: "Pine forest wrapped in fog",
caption: "Fog, Black Forest",
ratio: "4 / 5",
tone: "#5f7060",
},
{
src: "/images/components-lightbox-gallery-photo-07.jpg",
alt: "Yellow tram on a steep cobbled street",
caption: "Tram 28, Lisbon",
ratio: "3 / 4",
tone: "#e07b4f",
},
];
/** Image with a tone placeholder until the file exists. */
function Pic({ p, className }: { p: Photo; className?: string }) {
const img = useRef<HTMLImageElement>(null);
const [missing, setMissing] = useState(false);
useEffect(() => {
const el = img.current;
if (el && el.complete && el.naturalWidth === 0) setMissing(true);
}, []);
return (
<span
className={`gu-lb__pic ${className ?? ""}`}
style={{ "--tone": p.tone, aspectRatio: p.ratio } as CSSProperties}
>
{missing ? (
<span className="gu-lb__ph">{p.caption}</span>
) : (
<img
ref={img}
src={p.src}
alt={p.alt}
draggable={false}
onError={() => setMissing(true)}
/>
)}
</span>
);
}
/**
* LightboxGallery
* - The lightbox is a native <dialog> (focus trap, Escape, top layer).
* - On open, the image starts at the clicked tile's rectangle and animates to the center (a FLIP using
* CSS variables), so it feels like the photo grows out of the grid.
*/
export function LightboxGallery({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const dialog = useRef<HTMLDialogElement>(null);
const [index, setIndex] = useState<number | null>(null);
const [from, setFrom] = useState<CSSProperties>({});
const [dir, setDir] = useState(0);
const touch = useRef<{ x: number; y: number } | null>(null);
const n = PHOTOS.length;
const open = (i: number, el: HTMLElement) => {
const r = el.getBoundingClientRect();
// Offset of the tile center from the viewport center, and its scale vs. the final size
setFrom({
"--fx": `${r.left + r.width / 2 - innerWidth / 2}px`,
"--fy": `${r.top + r.height / 2 - innerHeight / 2}px`,
"--fs": Math.min(r.width / Math.min(innerWidth * 0.86, 1100), 1),
} as CSSProperties);
setDir(0);
setIndex(i);
dialog.current?.showModal();
};
const go = (d: number) => {
setDir(d);
setIndex((i) => (i === null ? i : (i + d + n) % n));
};
const close = () => dialog.current?.close();
return (
<div className="gu-lb" data-theme={theme}>
<ul className="gu-lb__grid">
{PHOTOS.map((p, i) => (
<li key={p.src}>
<button
type="button"
onClick={(e) => open(i, e.currentTarget)}
aria-label={`Open photo: ${p.caption}`}
>
<Pic p={p} />
</button>
</li>
))}
</ul>
<dialog
ref={dialog}
className="gu-lb__dialog"
aria-label="Photo viewer"
onClose={() => setIndex(null)}
onClick={(e) => e.target === e.currentTarget && close()}
onKeyDown={(e) => {
if (e.key === "ArrowRight") go(1);
if (e.key === "ArrowLeft") go(-1);
}}
onTouchStart={(e) =>
(touch.current = { x: e.touches[0].clientX, y: e.touches[0].clientY })
}
onTouchEnd={(e) => {
if (!touch.current) return;
const dx = e.changedTouches[0].clientX - touch.current.x;
const dy = e.changedTouches[0].clientY - touch.current.y;
if (dy > 90 && Math.abs(dy) > Math.abs(dx)) close();
else if (Math.abs(dx) > 50) go(dx < 0 ? 1 : -1);
touch.current = null;
}}
>
{index !== null && (
<>
<figure
className="gu-lb__stage"
style={from}
data-first={dir === 0 ? "" : undefined}
>
<div key={index} className="gu-lb__slide" data-dir={dir}>
<Pic p={PHOTOS[index]} className="gu-lb__big" />
</div>
<figcaption>
<b>{PHOTOS[index].caption}</b>
<span>
{index + 1} / {n}
</span>
</figcaption>
</figure>
<button
type="button"
className="gu-lb__nav gu-lb__nav--prev"
onClick={() => go(-1)}
aria-label="Previous photo"
>
‹
</button>
<button
type="button"
className="gu-lb__nav gu-lb__nav--next"
onClick={() => go(1)}
aria-label="Next photo"
>
›
</button>
<button
type="button"
className="gu-lb__close"
onClick={close}
aria-label="Close viewer"
autoFocus
>
×
</button>
<div className="gu-lb__thumbs" role="group" aria-label="All photos">
{PHOTOS.map((p, i) => (
<button
key={p.src}
type="button"
aria-current={i === index}
aria-label={p.caption}
onClick={() => go(i - index)}
style={{ "--tone": p.tone } as CSSProperties}
>
<Pic p={p} />
</button>
))}
</div>
</>
)}
</dialog>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Lightbox gallery
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A travel photo gallery: a masonry grid of rounded photos; clicking one opens a full-screen lightbox where the photo grows out of its tile to the center. Inside: arrows, a counter, a caption, a thumbnail strip, keyboard and swipe navigation, and swipe down to close.
## Layout and look
- Grid: CSS columns (3 × min 220px), 10px gaps, radius 16px tiles at their natural aspect ratios. Hover zooms 4% and brightens a little; zoom-in cursor.
- Lightbox: native <dialog> filling the screen, 94% Ink background. Photo up to 72dvh tall / 86vw wide, radius 14px. Caption "Tram 28, Lisbon" and "7 / 7" under it.
- 52px round translucent buttons for prev/next (sides) and close (top right), orange on hover. Thumbnails 54px, 45% opacity, the current one fully opaque with a Cream border.
## Behavior and motion
1. On open, measure the tile: set --fx/--fy (tile center minus viewport center) and --fs (tile width / final width). The slide animates from translate(--fx, --fy) scale(--fs) to its place (550ms cubic-bezier(0.16, 1, 0.3, 1)).
2. Next/prev: the new slide enters from ±60px with a fade.
3. Keys: ArrowLeft/Right; Escape closes (native). Touch: horizontal swipe > 50px changes, vertical down > 90px closes. Backdrop click closes.
4. Phones: no side arrows (swipe), larger image.
## Images
Seven photos with mixed aspect ratios (4:5, 3:2, 2:3, 1:1, 3:4). Until they exist, each tile shows a striped tone block with its caption.
## Accessibility
- Tiles are buttons "Open photo: …"; the dialog is labelled "Photo viewer"; thumbnails use aria-current; images have real alt text. Close gets focus on open.
- prefers-reduced-motion: simple fades.
## Props
theme. Edit PHOTOS.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### LightboxGallery.tsx
```tsx
"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import "./lightbox-gallery.css";
type Photo = {
src: string;
alt: string;
caption: string;
ratio: string;
tone: string;
};
const PHOTOS: Photo[] = [
{
src: "/images/components-lightbox-gallery-photo-01.jpg",
alt: "Concrete stairs in hard afternoon light",
caption: "Stairwell, Porto",
ratio: "4 / 5",
tone: "#c9b8a0",
},
{
src: "/images/components-lightbox-gallery-photo-02.jpg",
alt: "A fishing boat on a calm blue sea",
caption: "Morning boat, Hydra",
ratio: "3 / 2",
tone: "#4f78a8",
},
{
src: "/images/components-lightbox-gallery-photo-03.jpg",
alt: "Neon shop signs reflected on a wet street",
caption: "After rain, Osaka",
ratio: "2 / 3",
tone: "#5b3f8f",
},
{
src: "/images/components-lightbox-gallery-photo-04.jpg",
alt: "Desert dunes with a single line of footprints",
caption: "Dunes, Erg Chebbi",
ratio: "3 / 2",
tone: "#d08a4f",
},
{
src: "/images/components-lightbox-gallery-photo-05.jpg",
alt: "A ceramic cup on a linen tablecloth",
caption: "Still life, studio",
ratio: "1 / 1",
tone: "#e6ddd0",
},
{
src: "/images/components-lightbox-gallery-photo-06.jpg",
alt: "Pine forest wrapped in fog",
caption: "Fog, Black Forest",
ratio: "4 / 5",
tone: "#5f7060",
},
{
src: "/images/components-lightbox-gallery-photo-07.jpg",
alt: "Yellow tram on a steep cobbled street",
caption: "Tram 28, Lisbon",
ratio: "3 / 4",
tone: "#e07b4f",
},
];
/** Image with a tone placeholder until the file exists. */
function Pic({ p, className }: { p: Photo; className?: string }) {
const img = useRef<HTMLImageElement>(null);
const [missing, setMissing] = useState(false);
useEffect(() => {
const el = img.current;
if (el && el.complete && el.naturalWidth === 0) setMissing(true);
}, []);
return (
<span
className={`gu-lb__pic ${className ?? ""}`}
style={{ "--tone": p.tone, aspectRatio: p.ratio } as CSSProperties}
>
{missing ? (
<span className="gu-lb__ph">{p.caption}</span>
) : (
<img
ref={img}
src={p.src}
alt={p.alt}
draggable={false}
onError={() => setMissing(true)}
/>
)}
</span>
);
}
/**
* LightboxGallery
* - The lightbox is a native <dialog> (focus trap, Escape, top layer).
* - On open, the image starts at the clicked tile's rectangle and animates to the center (a FLIP using
* CSS variables), so it feels like the photo grows out of the grid.
*/
export function LightboxGallery({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const dialog = useRef<HTMLDialogElement>(null);
const [index, setIndex] = useState<number | null>(null);
const [from, setFrom] = useState<CSSProperties>({});
const [dir, setDir] = useState(0);
const touch = useRef<{ x: number; y: number } | null>(null);
const n = PHOTOS.length;
const open = (i: number, el: HTMLElement) => {
const r = el.getBoundingClientRect();
// Offset of the tile center from the viewport center, and its scale vs. the final size
setFrom({
"--fx": `${r.left + r.width / 2 - innerWidth / 2}px`,
"--fy": `${r.top + r.height / 2 - innerHeight / 2}px`,
"--fs": Math.min(r.width / Math.min(innerWidth * 0.86, 1100), 1),
} as CSSProperties);
setDir(0);
setIndex(i);
dialog.current?.showModal();
};
const go = (d: number) => {
setDir(d);
setIndex((i) => (i === null ? i : (i + d + n) % n));
};
const close = () => dialog.current?.close();
return (
<div className="gu-lb" data-theme={theme}>
<ul className="gu-lb__grid">
{PHOTOS.map((p, i) => (
<li key={p.src}>
<button
type="button"
onClick={(e) => open(i, e.currentTarget)}
aria-label={`Open photo: ${p.caption}`}
>
<Pic p={p} />
</button>
</li>
))}
</ul>
<dialog
ref={dialog}
className="gu-lb__dialog"
aria-label="Photo viewer"
onClose={() => setIndex(null)}
onClick={(e) => e.target === e.currentTarget && close()}
onKeyDown={(e) => {
if (e.key === "ArrowRight") go(1);
if (e.key === "ArrowLeft") go(-1);
}}
onTouchStart={(e) =>
(touch.current = { x: e.touches[0].clientX, y: e.touches[0].clientY })
}
onTouchEnd={(e) => {
if (!touch.current) return;
const dx = e.changedTouches[0].clientX - touch.current.x;
const dy = e.changedTouches[0].clientY - touch.current.y;
if (dy > 90 && Math.abs(dy) > Math.abs(dx)) close();
else if (Math.abs(dx) > 50) go(dx < 0 ? 1 : -1);
touch.current = null;
}}
>
{index !== null && (
<>
<figure
className="gu-lb__stage"
style={from}
data-first={dir === 0 ? "" : undefined}
>
<div key={index} className="gu-lb__slide" data-dir={dir}>
<Pic p={PHOTOS[index]} className="gu-lb__big" />
</div>
<figcaption>
<b>{PHOTOS[index].caption}</b>
<span>
{index + 1} / {n}
</span>
</figcaption>
</figure>
<button
type="button"
className="gu-lb__nav gu-lb__nav--prev"
onClick={() => go(-1)}
aria-label="Previous photo"
>
‹
</button>
<button
type="button"
className="gu-lb__nav gu-lb__nav--next"
onClick={() => go(1)}
aria-label="Next photo"
>
›
</button>
<button
type="button"
className="gu-lb__close"
onClick={close}
aria-label="Close viewer"
autoFocus
>
×
</button>
<div className="gu-lb__thumbs" role="group" aria-label="All photos">
{PHOTOS.map((p, i) => (
<button
key={p.src}
type="button"
aria-current={i === index}
aria-label={p.caption}
onClick={() => go(i - index)}
style={{ "--tone": p.tone } as CSSProperties}
>
<Pic p={p} />
</button>
))}
</div>
</>
)}
</dialog>
</div>
);
}
```
### lightbox-gallery.css
```css
/* LightboxGallery: change these colors to match your brand. */
.gu-lb {
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
width: min(100%, 900px);
font-family: system-ui, sans-serif;
}
.gu-lb[data-theme="dark"] {
--focus: #d7ff3a;
}
/* Masonry with CSS columns: simple and responsive */
.gu-lb__grid {
columns: 3 220px;
column-gap: 10px;
margin: 0;
padding: 0;
list-style: none;
}
.gu-lb__grid li {
margin-bottom: 10px;
break-inside: avoid;
}
.gu-lb__grid button {
display: block;
width: 100%;
padding: 0;
overflow: hidden;
border: 0;
border-radius: 16px;
background: none;
cursor: zoom-in;
}
.gu-lb__grid button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 3px;
}
.gu-lb__grid .gu-lb__pic {
transition:
transform 0.7s var(--ease),
filter 0.3s;
}
.gu-lb__grid button:hover .gu-lb__pic {
transform: scale(1.04);
filter: brightness(1.05);
}
.gu-lb__pic {
position: relative;
display: grid;
place-items: center;
width: 100%;
overflow: hidden;
background: var(--tone)
repeating-linear-gradient(
135deg,
rgb(255 255 255 / 0.14) 0 2px,
transparent 2px 16px
);
}
.gu-lb__pic img {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.gu-lb__ph {
padding: 8px;
color: rgb(14 14 16 / 0.55);
font-size: 0.85rem;
font-weight: 800;
text-align: center;
}
/* Lightbox */
.gu-lb__dialog {
width: 100vw;
max-width: none;
height: 100dvh;
max-height: none;
margin: 0;
padding: 0;
border: 0;
background: rgb(14 14 16 / 0.94);
color: #f3eee3;
font-family: system-ui, sans-serif;
overscroll-behavior: contain;
}
.gu-lb__dialog[open] {
display: grid;
grid-template-rows: 1fr auto;
animation: gu-lb-fade 0.3s;
}
.gu-lb__dialog::backdrop {
background: transparent;
}
@keyframes gu-lb-fade {
from {
background: rgb(14 14 16 / 0);
}
}
.gu-lb__stage {
display: grid;
place-content: center;
justify-items: center;
gap: 14px;
margin: 0;
padding: 56px 72px 12px;
pointer-events: none;
}
.gu-lb__stage > * {
pointer-events: auto;
}
/* First open: grow from the clicked tile */
.gu-lb__stage[data-first] .gu-lb__slide {
animation: gu-lb-grow 0.55s var(--ease);
}
@keyframes gu-lb-grow {
from {
transform: translate(var(--fx), var(--fy)) scale(var(--fs));
}
}
.gu-lb__slide[data-dir="1"] {
animation: gu-lb-next 0.45s var(--ease);
}
.gu-lb__slide[data-dir="-1"] {
animation: gu-lb-prev 0.45s var(--ease);
}
@keyframes gu-lb-next {
from {
opacity: 0;
transform: translateX(60px);
}
}
@keyframes gu-lb-prev {
from {
opacity: 0;
transform: translateX(-60px);
}
}
.gu-lb__big {
width: auto;
height: min(72dvh, 760px);
max-width: min(86vw, 1100px);
border-radius: 14px;
}
.gu-lb__stage figcaption {
display: flex;
gap: 14px;
font-size: 0.9rem;
}
.gu-lb__stage figcaption span {
opacity: 0.6;
font-variant-numeric: tabular-nums;
}
.gu-lb__dialog button {
font: inherit;
cursor: pointer;
}
.gu-lb__dialog button:focus-visible {
outline: 3px solid #d7ff3a;
outline-offset: 2px;
}
.gu-lb__nav,
.gu-lb__close {
position: fixed;
display: grid;
place-items: center;
width: 52px;
height: 52px;
border: 0;
border-radius: 50%;
background: rgb(243 238 227 / 0.12);
color: #f3eee3;
font-size: 1.8rem !important;
line-height: 1;
transition: background-color 0.2s;
}
.gu-lb__nav:hover,
.gu-lb__close:hover {
background: #ff5a1f;
color: #0e0e10;
}
.gu-lb__nav {
top: 50%;
translate: 0 -50%;
}
.gu-lb__nav--prev {
left: 14px;
}
.gu-lb__nav--next {
right: 14px;
}
.gu-lb__close {
top: 14px;
right: 14px;
}
.gu-lb__thumbs {
display: flex;
justify-content: center;
gap: 6px;
padding: 12px 16px 20px;
overflow-x: auto;
}
.gu-lb__thumbs button {
flex: none;
width: 54px;
height: 54px;
padding: 0;
overflow: hidden;
border: 2px solid transparent;
border-radius: 10px;
background: var(--tone);
opacity: 0.45;
transition:
opacity 0.2s,
border-color 0.2s;
}
.gu-lb__thumbs .gu-lb__pic {
height: 100%;
aspect-ratio: auto !important;
}
.gu-lb__thumbs .gu-lb__ph {
display: none;
}
.gu-lb__thumbs button[aria-current="true"] {
border-color: #f3eee3;
opacity: 1;
}
@media (max-width: 640px) {
.gu-lb__stage {
padding: 64px 12px 8px;
}
.gu-lb__big {
height: auto;
width: 94vw;
max-height: 66dvh;
}
.gu-lb__nav {
display: none; /* swipe instead */
}
}
@media (prefers-reduced-motion: reduce) {
.gu-lb__grid .gu-lb__pic {
transition: none;
}
.gu-lb__stage[data-first] .gu-lb__slide,
.gu-lb__slide[data-dir="1"],
.gu-lb__slide[data-dir="-1"] {
animation: gu-lb-plain 0.2s;
}
@keyframes gu-lb-plain {
from {
opacity: 0;
}
}
}
```
You might also want
Next piece: Loader setBefore and after slider
Components, beginner
Cart drawer
Components, advanced
Draggable bottom sheet
Components, advanced