DDBS UI
commerce

Cart View

The cart page: lines, promo form, shipping progress and an order summary, with an empty state. Includes SummaryRows.

npx shadcn@latest add https://ui.develop-better-solutions.com/r/store-cart-view.json
Dependencies:lucide-react
Fullscreen

Your cart (3)

Usage

store-cart-view-example.tsx
"use client";

import { useState } from "react";
import { CartLineRow, CartView, PromoForm, ShippingProgress, type CartLineRowLine } from "@dbs/ui";
import { IMG, IMG2, StoreScope, fmt } from "./_store";

const BASE: CartLineRowLine[] = [
  { key: "a", image: IMG, colorLabel: "Sand", lineTotal: 780, product: { id: "p1", name: "Atlas Canvas Tote" }, line: { quantity: 2, 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 StoreCartViewExample() {
  const [lines, setLines] = useState(BASE);
  const [promo, setPromo] = useState<string | null>(null);
  const subtotal = lines.reduce((s, l) => s + l.lineTotal, 0);
  const discount = promo ? Math.round(subtotal * 0.1) : 0;
  const net = subtotal - discount;
  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={0} className="rounded-none border-0">
      <CartView
        hydrated
        lines={lines}
        count={lines.reduce((n, l) => n + l.line.quantity, 0)}
        totals={{ subtotal, discount, tax: Math.round(net - net / 1.2), total: net, shipping: 0, freeShipping: net >= 1000 }}
        copy={{
          title: "Your cart",
          crumb: "Cart",
          notice: "Demo store. No real orders are placed.",
          emptyTitle: "Your cart is empty",
          emptyBody: "Browse the collection and add something you like.",
          shop: "Continue shopping",
          taxLabel: "VAT (20%)",
        }}
        hrefs={{ home: "#", products: "#", checkout: "#" }}
        formatPrice={fmt}
        taxIncluded
        renderLine={(l) => (
          <CartLineRow
            line={l}
            href="#"
            formatPrice={fmt}
            unitPrice={UNIT[l.product.id] ?? 0}
            maxQuantity={5}
            onQuantity={setQuantity}
            onRemove={(key) => setLines((ls) => ls.filter((x) => x.key !== key))}
            imageWidth={96}
            imageHeight={120}
            imageClassName="h-28 w-24 rounded-lg bg-[var(--store-tile)] object-cover"
            imageCompactClassName="h-20 w-16 rounded-lg object-cover"
          />
        )}
        progress={<ShippingProgress remaining={Math.max(0, 1000 - net)} threshold={1000} formatPrice={fmt} />}
        promo={
          <PromoForm
            promo={promo}
            applyPromo={(c) => (c.trim().toUpperCase() === "WELCOME10" ? (setPromo("WELCOME10"), null) : "Try WELCOME10.")}
            clearPromo={() => setPromo(null)}
            describe={() => "10% off"}
          />
        }
      />
    </StoreScope>
  );
}