Expanding image accordion
Five tall photo panels side by side; the one you hover or focus stretches wide and reveals its title, while the rest shrink to slivers.
"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import "./image-accordion.css";
type Panel = {
title: string;
place: string;
nights: string;
image: string;
tone: string;
};
const PANELS: Panel[] = [
{
title: "Cliffs & coves",
place: "Algarve, Portugal",
nights: "5 nights",
image: "/images/components-image-accordion-coast-01.jpg",
tone: "#e07b4f",
},
{
title: "Red sand silence",
place: "Wadi Rum, Jordan",
nights: "4 nights",
image: "/images/components-image-accordion-desert-02.jpg",
tone: "#c4612f",
},
{
title: "Cedar and rain",
place: "Yakushima, Japan",
nights: "6 nights",
image: "/images/components-image-accordion-forest-03.jpg",
tone: "#4f6b4a",
},
{
title: "Neon after dark",
place: "Seoul, South Korea",
nights: "4 nights",
image: "/images/components-image-accordion-city-04.jpg",
tone: "#3b3f8f",
},
{
title: "First tracks",
place: "Lofoten, Norway",
nights: "7 nights",
image: "/images/components-image-accordion-snow-05.jpg",
tone: "#8ea4b8",
},
];
function Photo({ src, tone }: { src: string; tone: 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-iacc__photo"
style={{ "--tone": tone } as CSSProperties}
>
{!missing && (
<img ref={img} src={src} alt="" onError={() => setMissing(true)} />
)}
</span>
);
}
/**
* ImageAccordion
* - Panels are flex items; the open one gets flex-grow 6, the others 1. Flex-grow animates smoothly.
* - Each panel is a button with aria-expanded, so it works with keyboard and touch.
*/
export function ImageAccordion({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [open, setOpen] = useState(0);
return (
<div className="gu-iacc" data-theme={theme}>
<div className="gu-iacc__track">
{PANELS.map((p, i) => (
<button
key={p.title}
type="button"
className="gu-iacc__panel"
aria-expanded={open === i}
aria-label={`${p.title}, ${p.place}, ${p.nights}`}
onClick={() => setOpen(i)}
onMouseEnter={() => setOpen(i)}
onFocus={() => setOpen(i)}
>
<Photo src={p.image} tone={p.tone} />
<span className="gu-iacc__num" aria-hidden="true">
0{i + 1}
</span>
<span className="gu-iacc__info" aria-hidden="true">
<small>
{p.place} · {p.nights}
</small>
<b>{p.title}</b>
</span>
</button>
))}
</div>
</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: Expanding image accordion
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 accordion: five tall rounded photo panels in a row. The hovered, focused or tapped panel stretches wide, its photo un-zooms and its title rises in; the others shrink to slim slivers with just a number.
## Layout and look
- Row 920px max, 440px tall, 8px gaps, panels radius 22px.
- Each panel: full-bleed photo (object-fit cover), a bottom dark gradient, "01" number at the top (centered when closed, top-left when open).
- Open panel info: "Algarve, Portugal · 5 nights" (small, 85% opacity) and a big title (clamp 26–42px, weight 900, tracking -0.04em) in Cream.
## Behavior and motion
1. Panels are flex: 1 1 0; the open one flex-grow 6, animated over 700ms cubic-bezier(0.16, 1, 0.3, 1).
2. Photos scale from 1.15 to 1 when opened (1.2s).
3. Info fades up 16px with a 250ms delay.
4. Container under 560px: vertical stack (620px tall) growing in height.
## Images
Five travel photos (portrait 3:4 works best). Until they exist, each panel shows a striped tone block in the photo's main color.
## Touch
Tap to open; vertical stack on phones.
## Accessibility
- Each panel is a button with aria-expanded and a full label (title, place, nights). Focus opens it, so keyboard users can tab through.
- prefers-reduced-motion: instant size changes.
## Props
theme. Edit PANELS.
## 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.
### ImageAccordion.tsx
```tsx
"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import "./image-accordion.css";
type Panel = {
title: string;
place: string;
nights: string;
image: string;
tone: string;
};
const PANELS: Panel[] = [
{
title: "Cliffs & coves",
place: "Algarve, Portugal",
nights: "5 nights",
image: "/images/components-image-accordion-coast-01.jpg",
tone: "#e07b4f",
},
{
title: "Red sand silence",
place: "Wadi Rum, Jordan",
nights: "4 nights",
image: "/images/components-image-accordion-desert-02.jpg",
tone: "#c4612f",
},
{
title: "Cedar and rain",
place: "Yakushima, Japan",
nights: "6 nights",
image: "/images/components-image-accordion-forest-03.jpg",
tone: "#4f6b4a",
},
{
title: "Neon after dark",
place: "Seoul, South Korea",
nights: "4 nights",
image: "/images/components-image-accordion-city-04.jpg",
tone: "#3b3f8f",
},
{
title: "First tracks",
place: "Lofoten, Norway",
nights: "7 nights",
image: "/images/components-image-accordion-snow-05.jpg",
tone: "#8ea4b8",
},
];
function Photo({ src, tone }: { src: string; tone: 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-iacc__photo"
style={{ "--tone": tone } as CSSProperties}
>
{!missing && (
<img ref={img} src={src} alt="" onError={() => setMissing(true)} />
)}
</span>
);
}
/**
* ImageAccordion
* - Panels are flex items; the open one gets flex-grow 6, the others 1. Flex-grow animates smoothly.
* - Each panel is a button with aria-expanded, so it works with keyboard and touch.
*/
export function ImageAccordion({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [open, setOpen] = useState(0);
return (
<div className="gu-iacc" data-theme={theme}>
<div className="gu-iacc__track">
{PANELS.map((p, i) => (
<button
key={p.title}
type="button"
className="gu-iacc__panel"
aria-expanded={open === i}
aria-label={`${p.title}, ${p.place}, ${p.nights}`}
onClick={() => setOpen(i)}
onMouseEnter={() => setOpen(i)}
onFocus={() => setOpen(i)}
>
<Photo src={p.image} tone={p.tone} />
<span className="gu-iacc__num" aria-hidden="true">
0{i + 1}
</span>
<span className="gu-iacc__info" aria-hidden="true">
<small>
{p.place} · {p.nights}
</small>
<b>{p.title}</b>
</span>
</button>
))}
</div>
</div>
);
}
```
### image-accordion.css
```css
/* ImageAccordion: change these colors to match your brand. */
.gu-iacc {
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
container-type: inline-size;
width: min(100%, 920px);
font-family: system-ui, sans-serif;
}
/* The track holds the layout so it can respond to the width of .gu-iacc */
.gu-iacc__track {
display: flex;
gap: 8px;
height: 440px;
}
.gu-iacc[data-theme="dark"] {
--focus: #d7ff3a;
}
.gu-iacc__panel {
position: relative;
flex: 1 1 0;
min-width: 0;
padding: 0;
overflow: hidden;
border: 0;
border-radius: 22px;
background: none;
color: #f3eee3;
font: inherit;
text-align: left;
cursor: pointer;
transition: flex-grow 0.7s var(--ease);
}
.gu-iacc__panel[aria-expanded="true"] {
flex-grow: 6;
}
.gu-iacc__panel:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 3px;
}
.gu-iacc__photo {
position: absolute;
inset: 0;
background: var(--tone)
repeating-linear-gradient(
135deg,
rgb(255 255 255 / 0.14) 0 2px,
transparent 2px 18px
);
}
.gu-iacc__photo img {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
transform: scale(1.15);
transition: transform 1.2s var(--ease);
}
.gu-iacc__panel[aria-expanded="true"] img {
transform: none;
}
/* Dark fade at the bottom so the title stays readable on any photo */
.gu-iacc__panel::after {
content: "";
position: absolute;
inset: 40% 0 0;
background: linear-gradient(transparent, rgb(14 14 16 / 0.75));
}
.gu-iacc__num {
position: absolute;
top: 16px;
left: 50%;
z-index: 1;
translate: -50% 0;
font-size: 0.8rem;
font-weight: 900;
transition:
left 0.6s var(--ease),
translate 0.6s var(--ease);
}
.gu-iacc__panel[aria-expanded="true"] .gu-iacc__num {
left: 20px;
translate: 0 0;
}
.gu-iacc__info {
position: absolute;
left: 22px;
right: 22px;
bottom: 20px;
z-index: 1;
display: grid;
gap: 4px;
min-width: 300px;
opacity: 0;
transform: translateY(16px);
transition:
opacity 0.3s,
transform 0.5s var(--ease);
}
.gu-iacc__panel[aria-expanded="true"] .gu-iacc__info {
opacity: 1;
transform: none;
transition-delay: 0.25s;
}
.gu-iacc__info small {
font-size: 0.85rem;
font-weight: 600;
opacity: 0.85;
}
.gu-iacc__info b {
font-size: clamp(1.6rem, 4.5cqi, 2.6rem);
font-weight: 900;
letter-spacing: -0.04em;
line-height: 1;
}
/* Narrow containers: stack vertically and grow in height */
@container (max-width: 560px) {
.gu-iacc__track {
flex-direction: column;
height: 620px;
}
.gu-iacc__info {
min-width: 0;
}
.gu-iacc__num {
top: 50%;
left: 20px;
translate: 0 -50%;
}
.gu-iacc__panel[aria-expanded="true"] .gu-iacc__num {
top: 16px;
translate: 0 0;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-iacc__panel,
.gu-iacc__photo img,
.gu-iacc__num,
.gu-iacc__info {
transition: none !important;
}
}
```
You might also want
Next piece: Feature tabs with autoplayProduct card with quick view
Components, intermediate
3D tilt card
Components, intermediate
Spotlight cards
Components, beginner