Cart Drawer
A slide-over cart with empty state, free-shipping progress, lines, totals and checkout links.
npx shadcn@latest add https://ui.develop-better-solutions.com/r/store-cart-drawer.jsonDependencies:
lucide-reactUsage
store-cart-drawer-example.tsx
"use client";
import { useState } from "react";
import { CartDrawer, CartLineRow, ShippingProgress, type CartLineRowLine } from "@dbs/ui";
import { IMG, IMG2, StoreScope, fmt, triggerClass } from "./_store";
const BASE: CartLineRowLine[] = [
{ key: "a", image: IMG, colorLabel: "Sand", lineTotal: 390, product: { id: "p1", name: "Atlas Canvas Tote" }, line: { quantity: 1, size: "One size" } },
{ key: "b", image: IMG2, colorLabel: "Indigo", lineTotal: 640, product: { id: "p2", name: "Souk Linen Shirt" }, line: { quantity: 1, size: "M" } },
];
const UNIT: Record<string, number> = { p1: 390, p2: 640 };
export default function StoreCartDrawerExample() {
const [open, setOpen] = useState(false);
const [lines, setLines] = useState(BASE);
const subtotal = lines.reduce((s, l) => s + l.lineTotal, 0);
const setQuantity = (key: string, q: number) =>
setLines((ls) =>
ls.map((l) => (l.key === key ? { ...l, line: { ...l.line, quantity: q }, lineTotal: (UNIT[l.product.id] ?? 0) * q } : l)),
);
return (
<StoreScope minHeight={640}>
<button type="button" className={triggerClass} onClick={() => setOpen(true)}>
Open cart ({lines.length})
</button>
<CartDrawer
open={open}
onClose={() => setOpen(false)}
labels={{
title: "Your bag",
close: "Close cart",
emptyTitle: "Your bag is empty",
emptyBody: "Add a few things and they will wait for you here.",
shop: "Keep shopping",
}}
count={lines.reduce((n, l) => n + l.line.quantity, 0)}
lines={lines}
renderLine={(l) => (
<CartLineRow
line={l}
compact
href="#"
formatPrice={fmt}
unitPrice={UNIT[l.product.id] ?? 0}
maxQuantity={5}
onQuantity={setQuantity}
onRemove={(key) => setLines((ls) => ls.filter((x) => x.key !== key))}
imageWidth={64}
imageHeight={80}
imageClassName="h-24 w-20 rounded-lg object-cover"
imageCompactClassName="h-20 w-16 rounded-lg bg-[var(--store-tile)] object-cover"
/>
)}
totals={{ subtotal, discount: 0, freeShipping: subtotal >= 1000 }}
formatPrice={fmt}
progress={<ShippingProgress remaining={Math.max(0, 1000 - subtotal)} threshold={1000} formatPrice={fmt} />}
hrefs={{ products: "#", cart: "#", checkout: "#" }}
/>
</StoreScope>
);
}