Quick View Modal
A product quick view with render-prop media, info and variant slots, a favourite button and an add button.
npx shadcn@latest add https://ui.develop-better-solutions.com/r/store-quick-view-modal.jsonDependencies:
lucide-reactUsage
store-quick-view-modal-example.tsx
"use client";
import { useState } from "react";
import { QuickViewModal, VariantPicker } from "@dbs/ui";
import { PRODUCTS, StoreScope, fmt, triggerClass, type DemoProduct } from "./_store";
const VARIANTS = {
colors: [
{ id: "sand", label: "Sand", swatch: "var(--color-gold)" },
{ id: "ink", label: "Ink", swatch: "var(--color-ink)" },
],
sizes: [{ label: "S" }, { label: "M" }, { label: "L" }],
};
export default function StoreQuickViewModalExample() {
const [product, setProduct] = useState<DemoProduct | null>(null);
const [fav, setFav] = useState(false);
const [added, setAdded] = useState<string | null>(null);
return (
<StoreScope minHeight={720}>
<div className="flex flex-wrap items-center gap-3">
<button type="button" className={triggerClass} onClick={() => setProduct(PRODUCTS[1]!)}>
Quick view
</button>
{added && <span className="text-sm text-[var(--store-soft)]">{added}</span>}
</div>
<QuickViewModal<DemoProduct>
product={product}
favorite={fav}
onClose={() => setProduct(null)}
onToggleFavorite={() => setFav((f) => !f)}
onAdd={(p, colorId, size) => {
setAdded(`Added ${p.name} (${colorId}, ${size})`);
setProduct(null);
}}
labelId="qv-title"
detailsHref="#"
initialColorId={() => "sand"}
needsSize={() => true}
sizeRequiredMessage="Choose a size first."
mediaClassName="min-h-[260px] bg-[var(--store-tile)] md:min-h-[420px]"
renderMedia={(p) => (
// eslint-disable-next-line @next/next/no-img-element
<img src={p.image} alt={p.name} className="h-full w-full object-cover" />
)}
renderInfo={(p) => (
<div>
<p className="text-xs uppercase tracking-[0.14em] text-[var(--store-muted)]">{p.category}</p>
<h2 id="qv-title" className="mt-1 text-2xl font-bold tracking-[-0.03em]">
{p.name}
</h2>
<p className="mt-2 text-lg">{fmt(p.price)}</p>
<p className="mt-3 text-sm text-[var(--store-soft)]">Woven in small batches and finished by hand.</p>
</div>
)}
renderVariants={(_, s) => (
<VariantPicker
product={VARIANTS}
idPrefix="qv"
colorId={s.colorId}
size={s.size}
onColor={s.setColorId}
onSize={(l) => {
s.setSize(l);
s.setError(null);
}}
sizeError={s.error}
sizeGroupRef={s.sizeRef}
/>
)}
addLabel="Add to bag"
addClassName="h-12 rounded-full bg-[var(--store-ink)] px-8 text-[var(--store-on-ink)]"
closeClassName="absolute right-3 top-3"
/>
</StoreScope>
);
}