expand in place.
A list row that grows into a full card with a FLIP animation, keeping its corners round and its text sharp the whole way.
live component, try it
what to expect.
how it works.
A row growing into a card is a FLIP: measure the row, let the card lay out at its real size, then transform it back onto the row and animate that to nothing. One layout, and every frame after it is a transform. The corner is what gives a bad one away. A radius under a non-uniform scale paints as an ellipse, so a 14px corner on a card squashed to a row comes out as a smear, and the only fix is to divide the radius by each axis every frame. The text has the same problem in reverse: scaled, it is resampled, so the content carries the inverse transform and is never scaled at all.
take it with you.
Three steps, and the stage above runs in your own project.
Install
Nothing to add. It runs on React alone, 18 or newer.
Copy the files
4 files, kept together in one folder,
expand-card/. The styles travel with them."use client"; import { useCallback, useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react"; import { useReducedMotion } from "./motion"; import { flipDelta, radiusCorrection, type Box, type Delta } from "./flip"; import "./expand-card.css"; export interface ExpandCardProps { open: boolean; onClose: () => void; // ms to grow out of the row; closing takes a touch less duration?: number; // the row's viewport rect at the moment it was tapped from: Box | null; children: ReactNode; } const RADIUS = 14; const MAX_STEP = 1000 / 30; const rect = (el: Element): Box => { const r = el.getBoundingClientRect(); return { x: r.x, y: r.y, width: r.width, height: r.height }; }; const easeOutQuart = (t: number) => 1 - Math.pow(1 - t, 4); const easeInOutCubic = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2); export default function ExpandCard({ open, onClose, duration = 320, from, children, }: ExpandCardProps) { const cardRef = useRef<HTMLDivElement>(null); const innerRef = useRef<HTMLDivElement>(null); const scrimRef = useRef<HTMLButtonElement>(null); const rafRef = useRef(0); const deltaRef = useRef<Delta | null>(null); const reduced = useReducedMotion(); const [playing, setPlaying] = useState(false); const [closing, setClosing] = useState(false); const write = useCallback( (progress: number, delta: Delta | null, isClosing = false) => { const card = cardRef.current; const inner = innerRef.current; const scrim = scrimRef.current; if (!card || !delta) return; const p = Math.max(0, Math.min(1, progress)); const k = 1 - p; const sx = 1 + (delta.sx - 1) * k; const sy = 1 + (delta.sy - 1) * k; const tx = delta.dx * k; const ty = delta.dy * k; if (p >= 1) { card.style.transform = "none"; card.style.borderRadius = `${RADIUS}px`; card.style.boxShadow = ""; card.style.opacity = "1"; if (inner) inner.style.transform = "none"; if (scrim) scrim.style.opacity = "1"; } else { card.style.transform = `translate(${tx}px, ${ty}px) scale(${sx}, ${sy})`; card.style.borderRadius = radiusCorrection(RADIUS, sx, sy); if (isClosing) { const shadowAlpha1 = (0.16 * p).toFixed(3); const shadowAlpha2 = (0.18 * p).toFixed(3); card.style.boxShadow = `0 4px 20px -2px rgba(27, 27, 25, ${shadowAlpha1}), 0 12px 36px -8px rgba(27, 27, 25, ${shadowAlpha2})`; card.style.opacity = p < 0.12 ? `${(p / 0.12).toFixed(3)}` : "1"; } else { card.style.boxShadow = ""; card.style.opacity = "1"; } if (inner) { inner.style.transform = `scale(${1 / sx}, ${1 / sy})`; } if (scrim) { scrim.style.opacity = `${p}`; } } }, [], ); const startClose = useCallback(() => { if (closing) return; if (reduced) { onClose(); return; } cancelAnimationFrame(rafRef.current); setPlaying(true); setClosing(true); const delta = deltaRef.current; if (!delta) { onClose(); return; } let prev: number | null = null; let elapsed = 0; const closeDuration = Math.round(Math.max(200, duration * 0.95)); const frame = (now: number) => { elapsed += prev === null ? 0 : Math.min(now - prev, MAX_STEP); prev = now; const t = Math.min(1, elapsed / closeDuration); const p = 1 - easeInOutCubic(t); write(p, delta, true); if (t < 1) { rafRef.current = requestAnimationFrame(frame); } else { setPlaying(false); setClosing(false); onClose(); } }; rafRef.current = requestAnimationFrame(frame); }, [closing, reduced, duration, write, onClose]); useLayoutEffect(() => { if (!open || !from) return; if (closing) return; if (reduced) { write(1, { dx: 0, dy: 0, sx: 1, sy: 1 }); return; } const card = cardRef.current; if (!card) return; card.style.transform = "none"; const last = rect(card); const delta = flipDelta(from, last); deltaRef.current = delta; write(0, delta); setPlaying(true); let prev: number | null = null; let elapsed = 0; const frame = (now: number) => { elapsed += prev === null ? 0 : Math.min(now - prev, MAX_STEP); prev = now; const t = Math.min(1, elapsed / duration); const p = easeOutQuart(t); write(p, delta); if (t < 1) { rafRef.current = requestAnimationFrame(frame); } else { setPlaying(false); write(1, delta); } }; rafRef.current = requestAnimationFrame(frame); return () => cancelAnimationFrame(rafRef.current); }, [open, from, duration, reduced, write]); useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") { e.preventDefault(); startClose(); } }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [open, startClose]); if (!open) return null; return ( <div className="xc-layer" data-closing={String(closing)}> <button type="button" ref={scrimRef} className="xc-scrim" aria-label="close" onClick={startClose} /> <div className="xc-card" ref={cardRef} role="dialog" aria-modal="true" aria-label="expanded" data-playing={String(playing)} data-closing={String(closing)} > <button type="button" className="xc-close" aria-label="dismiss card" onClick={startClose} > <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"> <line x1="18" y1="6" x2="6" y2="18" /> <line x1="6" y1="6" x2="18" y2="18" /> </svg> </button> <div className="xc-inner" ref={innerRef}> {children} </div> </div> </div> ); }Use it
The stage above, exactly as it runs here. The comments mark what to change.
"use client"; import { useRef, useState } from "react"; import ExpandCard from "./expand-card/ExpandCard"; import type { Box } from "./expand-card/flip"; export interface Row { id: string; title: string; description: string; image: string; } // swap these for your own rows and images const ROWS: Row[] = [ { id: "movievault", title: "MovieVault", description: "where to stream any film, series or anime, with whole franchises in release order.", image: "https://picsum.photos/seed/movievault/640/400", }, { id: "quizzme", title: "QuizzMe!", description: "trivia on 24 topics dealt as cards, with streaks, a scorecard and a replay of misses.", image: "https://picsum.photos/seed/quizzme/640/400", }, { id: "redline", title: "Redline", description: "selected changes across my projects, drawn like revisions on a drawing.", image: "https://picsum.photos/seed/redline/640/400", }, { id: "pasteup", title: "Pasteup", description: "pick a template or your own image, caption it, drag the words into place and send it.", image: "https://picsum.photos/seed/pasteup/640/400", }, ]; export default function ExpandCardExample({ rows = ROWS }: { rows?: Row[] }) { const [open, setOpen] = useState<number | null>(null); const [from, setFrom] = useState<Box | null>(null); const rowsRef = useRef<(HTMLButtonElement | null)[]>([]); const expand = (i: number) => { const r = rowsRef.current[i]?.getBoundingClientRect(); if (r) setFrom({ x: r.x, y: r.y, width: r.width, height: r.height }); setOpen(i); }; const row = open === null ? null : rows[open]; return ( <div className="xc-lab"> <ul className="xc-rows"> {rows.map((r, i) => ( <li key={r.id}> <button type="button" className="xc-row" ref={(el) => { rowsRef.current[i] = el; }} onClick={() => expand(i)} > <span className="xc-thumb"> <img src={r.image} alt="" decoding="async" /> </span> <span>{r.title}</span> </button> </li> ))} </ul> {/* escape, the scrim or the close button all fold it back into its row */} <ExpandCard open={row != null} onClose={() => setOpen(null)} duration={320} from={from}> {row && ( <> <span className="xc-cover"> <img src={row.image} alt="" decoding="async" /> </span> <h2>{row.title}</h2> <p>{row.description}</p> </> )} </ExpandCard> </div> ); }