Number 5: Card that grows into its detail
Specification
- Catalogue
- No. 5 of 110
- Section
- Cards and accordions
- Interaction
- Layout Animation
- Element
- Card, Overlay
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
A class, service or case study opens out of its own row and closes back into it.
The visitor sees the detail come from the thing they pressed, and returns to the same place in the list. It is a real dialog, so Escape and focus behave.
Panel grows from the row 420ms cubic-bezier(.16, 1, .3, 1), shrinks back 300ms. Content fades in after 140ms at its final width, so text never reflows mid-grow. Native dialog; focus returns to the row; no blurred backdrop.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useId, useLayoutEffect, useRef, useState } from "react";
import { growBetween } from "@/components/pieces/motion-helpers";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import "@/components/pieces/styles/form-fields.css";
import "./expandable-card.css";
const S = "/samples";
const CLASSES = [
{ id: "taster", title: "Taster evening", meta: "2 hours · £45", img: "studio-cups.svg", body: "Try the wheel with a potter beside you. You throw two pieces and choose one to be glazed and fired.", includes: ["Clay, glazes and firing", "Collect your piece two weeks later", "Aprons provided"] },
{ id: "course", title: "Six-week wheel course", meta: "Thursdays, 7pm · £240", img: "studio-bowls.svg", body: "From centring to trimming over six evenings, ending with a set of bowls glazed in colours you pick.", includes: ["12.5kg of clay", "All firings", "Studio time on Saturdays"] },
{ id: "hand", title: "Hand-building weekend", meta: "Sat and Sun · £160", img: "studio-shelf.svg", body: "Pinch, coil and slab. No wheel, so it suits anyone who wants to start slowly.", includes: ["Lunch both days", "Two fired pieces"] },
{ id: "private", title: "Private session", meta: "By arrangement", img: "studio-jug.svg", body: "One to one, or with a friend. Bring a piece you want to make and we plan the session around it.", includes: ["Planned with you by email"] },
];
function ExpandableCardDemo() {
const dialog = useRef<HTMLDialogElement>(null);
const panel = useRef<HTMLDivElement>(null);
const rows = useRef<(HTMLButtonElement | null)[]>([]);
const [open, setOpen] = useState<number | null>(null);
const [closing, setClosing] = useState(false);
const [width, setWidth] = useState(480);
const uid = useId();
const show = (i: number) => {
const d = dialog.current;
const row = rows.current[i];
if (!d || !row) return;
setWidth(Math.min(480, window.innerWidth - 32));
setOpen(i);
setClosing(false);
d.showModal();
};
// After the panel renders at its final size, grow it there from the row.
useLayoutEffect(() => {
if (open === null || closing) return;
const p = panel.current;
const row = rows.current[open];
if (!p || !row) return;
// The panel is centred by CSS; pin that box in place so the grow can run on left/top/size.
const to = p.getBoundingClientRect();
Object.assign(p.style, { left: `${to.left}px`, top: `${to.top}px`, height: `${to.height}px`, translate: "none" });
void growBetween(p, row.getBoundingClientRect(), to, 420, ["14px", "18px"]);
// Runs on open only: closing has its own animation in hide().
// eslint-disable-next-line react-hooks/exhaustive-deps -- reading `closing` here must not re-run the grow
}, [open]);
const hide = async () => {
const d = dialog.current;
const p = panel.current;
if (!d || open === null || closing) return;
const row = rows.current[open];
setClosing(true);
if (p && row) await growBetween(p, p.getBoundingClientRect(), row.getBoundingClientRect(), 300, ["18px", "14px"]);
d.close();
setOpen(null);
setClosing(false);
row?.focus();
};
const c = open === null ? null : CLASSES[open];
return (
<div className="dm">
<ul className="xc-list" aria-label="Sample classes">
{CLASSES.map((k, i) => (
<li key={k.id}>
<button
ref={(el) => {
rows.current[i] = el;
}}
type="button"
className="xc-row"
data-hidden={open === i || undefined}
aria-haspopup="dialog"
onClick={() => show(i)}
>
<img src={`${S}/${k.img}`} alt="" />
<span className="xc-text">
<strong>{k.title}</strong>
<span>{k.meta}</span>
</span>
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
<path d="M12 5v14M5 12h14" />
</svg>
</button>
</li>
))}
</ul>
<dialog
ref={dialog}
className="xc"
aria-labelledby={`${uid}-t`}
data-closing={closing || undefined}
onCancel={(e) => {
e.preventDefault();
void hide();
}}
onClick={(e) => {
if (e.target === e.currentTarget) void hide();
}}
>
{c && (
<div ref={panel} className="xc-panel" style={{ width }}>
<div className="xc-inner" style={{ width }}>
<img src={`${S}/${c.img}`} alt="" />
<div className="xc-body">
<h3 id={`${uid}-t`}>{c.title}</h3>
<p className="xc-meta">{c.meta}</p>
<p>{c.body}</p>
<ul>
{c.includes.map((x) => (
<li key={x}>{x}</li>
))}
</ul>
<div className="qz-nav">
<button type="button" className="dm-btn" onClick={() => void hide()}>
Close
</button>
<a className="dm-btn" data-variant="solid" href="#">
Book a place
</a>
</div>
</div>
</div>
</div>
)}
</dialog>
</div>
);
}
export default function ExpandableCardEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Card that grows into its detail" sample>
<ExpandableCardDemo />
</PieceStage>
);
}