Product Grid
FreeA filterable product list with animated reflow, a save toggle and an add-to-bag button that announces the bag count.
"use client";
import { useState } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { Check, Heart, Plus } from "lucide-react";
type Kind = "Mugs" | "Vases" | "Bowls";
type Product = { id: string; name: string; kind: Kind; price: number; glaze: string; tint: string; clay: string; isNew?: boolean };
const products: Product[] = [
{ id: "m1", name: "Morning mug", kind: "Mugs", price: 28, glaze: "Oat", tint: "bg-[#efe6d8] dark:bg-[#2f2a23]", clay: "#c9b08d", isNew: true },
{ id: "v1", name: "Bud vase", kind: "Vases", price: 42, glaze: "Moss", tint: "bg-[#e1e8dc] dark:bg-[#252b24]", clay: "#7f9575" },
{ id: "b1", name: "Ramen bowl", kind: "Bowls", price: 36, glaze: "Ink", tint: "bg-[#dfe3ea] dark:bg-[#23272e]", clay: "#56657d" },
{ id: "m2", name: "Espresso cup", kind: "Mugs", price: 22, glaze: "Rust", tint: "bg-[#f0dfd5] dark:bg-[#30241f]", clay: "#b5694b" },
{ id: "v2", name: "Tall vase", kind: "Vases", price: 68, glaze: "Chalk", tint: "bg-[#ecebe6] dark:bg-[#2a2a27]", clay: "#b9b6aa", isNew: true },
{ id: "b2", name: "Nesting bowl", kind: "Bowls", price: 31, glaze: "Oat", tint: "bg-[#efe6d8] dark:bg-[#2f2a23]", clay: "#c9b08d" },
];
const kinds = ["All", "Mugs", "Vases", "Bowls"] as const;
const money = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 0 });
/** A small drawn stand-in for product photography. Swap it for an image. */
function Piece({ kind, clay }: { kind: Kind; clay: string }) {
return (
<svg viewBox="0 0 120 120" className="size-full" aria-hidden>
<ellipse cx="60" cy="100" rx="34" ry="4" fill="#000" opacity="0.12" />
{kind === "Mugs" ? (
<>
<path d="M82 52h6a12 12 0 0 1 0 24h-6" fill="none" stroke={clay} strokeWidth="6" strokeLinecap="round" />
<path d="M36 40h48v46a12 12 0 0 1-12 12H48a12 12 0 0 1-12-12z" fill={clay} />
</>
) : kind === "Vases" ? (
<path d="M52 22h16v14c0 8 14 14 14 32 0 16-9 30-22 30S38 84 38 68c0-18 14-24 14-32z" fill={clay} />
) : (
<path d="M24 56h72c0 24-14 42-36 42S24 80 24 56z" fill={clay} />
)}
</svg>
);
}
export default function ProductGrid() {
const reduce = useReducedMotion();
const [kind, setKind] = useState<(typeof kinds)[number]>("All");
const [bag, setBag] = useState<string[]>([]);
const [saved, setSaved] = useState<string[]>([]);
const [announce, setAnnounce] = useState("");
const shown = products.filter((product) => kind === "All" || product.kind === kind);
function add(product: Product) {
setBag((items) => [...items, product.id]);
setAnnounce(`${product.name} added to bag. ${bag.length + 1} in bag.`);
}
return (
<section className="mx-auto w-full max-w-6xl px-5 py-12 sm:px-8 sm:py-16">
<div className="flex flex-wrap items-end justify-between gap-4">
<div>
<h2 className="text-3xl font-semibold tracking-[-0.035em] sm:text-4xl">Tableware</h2>
<p className="mt-1.5 text-sm text-muted-foreground">Thrown and glazed by hand in small batches.</p>
</div>
<p className="font-mono text-sm tabular-nums text-muted-foreground">Bag ({bag.length})</p>
</div>
<div role="group" aria-label="Filter by type" className="mt-6 flex gap-1.5 overflow-x-auto pb-1 [scrollbar-width:none]">
{kinds.map((option) => (
<button
key={option}
type="button"
aria-pressed={kind === option}
onClick={() => setKind(option)}
className="h-9 shrink-0 rounded-md border border-border px-3.5 text-sm text-muted-foreground outline-none transition-colors duration-150 hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring aria-pressed:border-foreground aria-pressed:bg-foreground aria-pressed:text-background"
>
{option}
</button>
))}
</div>
<ul className="mt-6 grid grid-cols-2 gap-x-4 gap-y-8 lg:grid-cols-3">
<AnimatePresence mode="popLayout" initial={false}>
{shown.map((product) => {
const inBag = bag.includes(product.id);
const isSaved = saved.includes(product.id);
return (
<motion.li
key={product.id}
layout={!reduce}
initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.96 }}
animate={{ opacity: 1, scale: 1 }}
exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.96 }}
transition={{ type: "spring", duration: 0.45, bounce: 0 }}
className="group relative min-w-0"
>
<div className={`relative aspect-square overflow-hidden rounded-xl ${product.tint}`}>
<div className="absolute inset-[14%] transition-transform duration-500 ease-[cubic-bezier(0.215,0.61,0.355,1)] group-hover:scale-[1.04] motion-reduce:transition-none">
<Piece kind={product.kind} clay={product.clay} />
</div>
{product.isNew ? <span className="absolute left-3 top-3 rounded-[5px] bg-background px-2 py-0.5 text-[11px] font-medium">New</span> : null}
<button
type="button"
aria-pressed={isSaved}
aria-label={`Save ${product.name}`}
onClick={() => setSaved((items) => (isSaved ? items.filter((id) => id !== product.id) : [...items, product.id]))}
className="absolute right-2 top-2 grid size-9 place-items-center rounded-full bg-background/80 text-foreground outline-none backdrop-blur transition-[scale,background-color] duration-150 hover:bg-background active:scale-[0.96] focus-visible:ring-2 focus-visible:ring-ring motion-reduce:transition-none"
>
<Heart className={`size-4 transition-[fill,color,scale] duration-200 ${isSaved ? "scale-110 fill-[#d2452d] text-[#d2452d]" : ""}`} aria-hidden />
</button>
</div>
<div className="mt-3 flex items-start justify-between gap-3">
<div className="min-w-0">
<h3 className="truncate font-medium">{product.name}</h3>
<p className="text-sm text-muted-foreground">{product.glaze} glaze</p>
</div>
<p className="font-medium tabular-nums">{money.format(product.price)}</p>
</div>
<button
type="button"
onClick={() => add(product)}
className="mt-3 inline-flex h-10 w-full items-center justify-center gap-2 rounded-lg border border-border text-sm font-medium outline-none transition-[background-color,border-color,scale] duration-150 hover:border-foreground/30 hover:bg-accent active:scale-[0.96] focus-visible:ring-2 focus-visible:ring-ring motion-reduce:transition-none"
>
{inBag ? <Check className="size-4" aria-hidden /> : <Plus className="size-4" aria-hidden />}
{inBag ? "Add another" : "Add to bag"}
<span className="sr-only">: {product.name}</span>
</button>
</motion.li>
);
})}
</AnimatePresence>
</ul>
<p role="status" className="sr-only">{announce}</p>
</section>
);
}