labsUI
Accent color

Cart

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.

npx @kznlabs/labsui add cart

Clip camera

$279

Studio headphones

$129.99

"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>    </>  );}

<CartList>

The list of CartLines.

Also accepts every <ul> prop (none of its own).

<CartLine>

One product in the cart: photo, name, line total, quantity field and remove button.

  • name required
    string
    Product name.
  • price required
    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.
  • quantityDefault 1
    number
    How many. Without onQuantityChange it shows as text, when above 1.
  • quantityLabelDefault "Quantity of"
    string
    Names the quantity field, before the product name.
  • removeLabelDefault "Remove"
    string
    Names the remove button, before the product name.
PropTypeDefault
name required
Product name.
string—
price required
Price of one item in major units; the line shows price × quantity.
number—
currency
ISO 4217 code for the price.
string—
details
Muted line under the name, e.g. "Size M, black".
ReactNode—
href
Product page link for the name.
string—
locale
BCP 47 locale for number formatting.
string—
max
Highest quantity, e.g. what is in stock.
number—
media
Square photo (<img>).
ReactNode—
onQuantityChange
Shows the quantity field; called with the new quantity, at least 1.
(quantity: number) => void—
onRemove
Shows the remove button; called when it is pressed. Focus moves to the next line once this one is gone.
() => void—
quantity
How many. Without onQuantityChange it shows as text, when above 1.
number1
quantityLabel
Names the quantity field, before the product name.
string"Quantity of"
removeLabel
Names the remove button, before the product name.
string"Remove"

required

Also accepts every <li> prop.

<OrderSummary>

Subtotal, shipping and the total as a description list. It adds nothing up: the numbers are yours.

  • total required
    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.
  • linesDefault []
    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".
  • totalLabelDefault "Total"
    string
    Label of the total row.
PropTypeDefault
total required
What to pay. Your app computes it, and screen readers hear it change.
number—
action
Under everything, e.g. a checkout Button.
ReactNode—
currency
ISO 4217 code for the amounts.
string—
lines
Rows above the total, in order.
OrderSummaryLine[][]
locale
BCP 47 locale for number formatting.
string—
note
Small print under the total, e.g. "Taxes calculated at checkout".
ReactNode—
totalLabel
Label of the total row.
string"Total"

required

Also accepts every <div> prop.

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

Every component also takes className.