# Cart

- Page: <https://kznlabs.github.io/labsUI/components/cart>

## Cart (`cart`, block)

CartList and CartLine (photo, name, line total, quantity, remove) and OrderSummary (rows, a total screen readers hear change, your checkout action), usually in a Drawer. UI only: state, totals, payment and delivery stay in your app. To open the cart on add, call `show()` from `useDisclosure()` in ProductCard's `onAddToCart`, or show a toast instead.

- Install: `npx @kznlabs/labsui add cart`
- Docs: <https://kznlabs.github.io/labsUI/components/cart>
- Brings: aspect-ratio, button, button-variants, field, focus, form-value, input, input-group, label, link, number-input, use-controllable-state, utils
- npm: clsx@^2.1.1, lucide-react@1.63.0, motion@14.7.1, tailwind-merge@^3.7.0

### API

- `<CartList>` (+ <ul> props): The list of CartLines.
- `<CartLine>` (+ <li> props): One product in the cart: photo, name, line total, quantity field and remove button.
  - `name: string`: Product name.
  - `price: number`: Price of one item in major units; the line shows price × quantity.
  - `currency?: string`: ISO 4217 code for the price.
  - `details?: ReactNode`: Muted line under the name, e.g. "Size M, black".
  - `href?: string`: Product page link for the name.
  - `locale?: string`: BCP 47 locale for number formatting.
  - `max?: number`: Highest quantity, e.g. what is in stock.
  - `media?: ReactNode`: Square photo (`<img>`).
  - `onQuantityChange?: (quantity: number) => void`: Shows the quantity field; called with the new quantity, at least 1.
  - `onRemove?: () => void`: Shows the remove button; called when it is pressed. Focus moves to the next line once this one is gone.
  - `quantity?: number` = `1`: How many. Without `onQuantityChange` it shows as text, when above 1.
  - `quantityLabel?: string` = `"Quantity of"`: Names the quantity field, before the product name.
  - `removeLabel?: string` = `"Remove"`: Names the remove button, before the product name.
- `<OrderSummary>` (+ <div> props): Subtotal, shipping and the total as a description list. It adds nothing up: the numbers are yours.
  - `total: number`: What to pay. Your app computes it, and screen readers hear it change.
  - `action?: ReactNode`: Under everything, e.g. a checkout Button.
  - `currency?: string`: ISO 4217 code for the amounts.
  - `lines?: OrderSummaryLine[]` = `[]`: Rows above the total, in order.
  - `locale?: string`: BCP 47 locale for number formatting.
  - `note?: ReactNode`: Small print under the total, e.g. "Taxes calculated at checkout".
  - `totalLabel?: string` = `"Total"`: Label of the total row.

- `type OrderSummaryLine = { label: string; amount: number | ReactNode; }`

### Usage

```tsx
<CartList><CartLine name="Clip camera" price={279} quantity={qty} onQuantityChange={setQty} onRemove={remove} /></CartList><OrderSummary lines={[{ label: "Shipping", amount: "Free" }]} total={279 * qty} action={<Button className="w-full">Checkout</Button>} />
```

### Example: shop.tsx

```tsx
"use client";

import { useState } from "react";
import { CartLine, CartList, OrderSummary } from "@/components/blocks/cart";
import { ProductCard } from "@/components/blocks/product-card";
import { Button } from "@/components/ui/button";
import {
  Drawer,
  DrawerBody,
  DrawerContent,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
} from "@/components/ui/drawer";
import { useDisclosure } from "@/lib/use-disclosure";

type Product = { id: string; name: string; price: number; image: string };

export function Shop({ products, onCheckout }: { products: Product[]; onCheckout: () => void }) {
  // One Drawer for the page. Adding opens it; call toast() there instead to stay on the page.
  const cart = useDisclosure();
  const [lines, setLines] = useState<Record<string, number>>({});
  const inCart = products.filter((product) => lines[product.id]);
  const total = inCart.reduce((sum, product) => sum + product.price * (lines[product.id] ?? 0), 0);

  const setQuantity = (id: string, quantity: number) =>
    setLines((current) => ({ ...current, [id]: quantity }));

  return (
    <>
      <div className="grid gap-4 sm:grid-cols-3">
        {products.map((product) => (
          <ProductCard
            key={product.id}
            name={product.name}
            price={product.price}
            media={[<img key={product.image} src={product.image} alt="" />]}
            onAddToCart={() => {
              setQuantity(product.id, (lines[product.id] ?? 0) + 1);
              cart.show();
            }}
          />
        ))}
      </div>

      <Drawer {...cart.props}>
        <DrawerContent>
          <DrawerHeader>
            <DrawerTitle>Your cart</DrawerTitle>
          </DrawerHeader>

          <DrawerBody>
            <CartList>
              {inCart.map((product) => (
                <CartLine
                  key={product.id}
                  name={product.name}
                  price={product.price}
                  quantity={lines[product.id]}
                  media={<img src={product.image} alt="" />}
                  onQuantityChange={(quantity) => setQuantity(product.id, quantity)}
                  onRemove={() => setQuantity(product.id, 0)}
                />
              ))}
            </CartList>
          </DrawerBody>

          <DrawerFooter className="border-t">
            <OrderSummary
              className="w-full"
              lines={[{ label: "Shipping", amount: "Free" }]}
              total={total}
              // Payment and delivery plug in here: your app owns them.
              action={<Button className="w-full" onClick={onCheckout}>Checkout</Button>}
            />
          </DrawerFooter>
        </DrawerContent>
      </Drawer>
    </>
  );
}
```
