nook
Browse the docs

Components

Cart

Native

Editable cart rows, undoable removal, promo codes and an order summary.

See the prompt
Loading your cart

Installation

npx nook-ui@latest add cart

Prompt

Paste this into your AI coding assistant. It adds this exact component, the same way the CLI does, and tells the assistant how to use it.

Short. For assistants that can run commands in your project.

Plain text
Add the Nook "Cart" component to this project, exactly as Nook ships it.

Editable cart rows, undoable removal, promo codes and an order summary.

Steps
1. Check for `nook.json` in the project root. If it is missing, run `npx nook-ui@latest init` first (React or Next.js with Tailwind CSS v4). It adds the Nook tokens, the theme and `cn()`.
2. Run `npx nook-ui@latest add cart`. It writes `components/ui/cart.tsx` together with everything the component needs. Do not write the component from memory.
3. If you cannot run commands, fetch http://localhost:3000/r/cart.json, write every entry of `files` to the path it names, install the packages in `dependencies`, and repeat for each name in `registryDependencies`.
4. Use it where I ask. Start from this example:

```tsx
import { Skeleton } from "@/components/ui/skeleton";

export default function CartLoading() {
  return (
    <div className="grid w-full max-w-md gap-4" aria-busy="true" role="status">
      <span className="sr-only">Loading your cart</span>
      {[0, 1].map((row) => (
        <div key={row} className="flex gap-3">
          <Skeleton className="size-20 shrink-0" />
          <div className="grid flex-1 content-start gap-2">
            <Skeleton className="h-4 w-40" />
            <Skeleton className="h-3 w-24" />
            <Skeleton className="h-8 w-28" />
          </div>
        </div>
      ))}
      <Skeleton className="h-10 w-full" />
    </div>
  );
}
```

API
- `Cart`: A controlled or local cart with derived subtotal and an empty state. Compose with Sheet for a side panel.
  - `value / defaultValue / onValueChange` (`CartLine[] / callback`): Rows: id, name, optional image/options, price, quantity and maxQuantity.
  - `summary` (`Omit<OrderSummaryProps, 'subtotal' | 'currency'>`): Shipping, tax, discounts and promo handlers for the summary.
  - `currency / empty / children` (`string / ReactNode / ReactNode`): Currency, custom empty content and actions such as checkout.
- `CartItem`: A row for custom cart layouts.
  - `item / currency` (`CartLine / string`): Product information and amount formatting.
  - `onQuantityChange / onRemove` (`callback`): Handlers for the quantity and removal controls.
- `QuantityStepper`: A NumberField adapter with a default quantity range of 1–99.
  - `value / defaultValue / onValueChange / min / max` (`NumberFieldProps`): The same controlled state and keyboard contract as NumberField. Give it an aria-label.
- `OrderSummary`: Subtotal, shipping, tax, absolute discount and clamped total, plus an optional promo form.
  - `subtotal / shipping / tax / discount` (`number`): Amounts in major currency units.
  - `onApplyPromo` (`(code) => PromoResult | Promise<PromoResult>`): Return { discount, label? } or { error }. No request is made by the component.
  - `onRemovePromo / onDiscountChange` (`callback`): Tell the app when the promo is removed or its discount changes.

Rules
- Colors, radii, shadows and motion come from Nook tokens and classes such as `bg-primary`, `text-muted-foreground`, `border-border`, `rounded-lg` and `h-control`. Never hard-code them.
- Do not restyle or simplify the file. Change how it looks through `className`, its props and the theme.
- It must keep working with the keyboard, in light and dark mode, and in the sharp, soft and bold styles.
- Accessibility: Quantity entry reuses NumberField for bounds, arrow keys and hold-to-repeat buttons. Removal moves focus to Undo and announces the removed item. Promo errors are linked to the field and announced.

Done when the component renders like the example in light and dark mode and the project type-checks.

Usage

import { Skeleton } from "@/components/ui/skeleton";

export default function CartLoading() {
  return (
    <div className="grid w-full max-w-md gap-4" aria-busy="true" role="status">
      <span className="sr-only">Loading your cart</span>
      {[0, 1].map((row) => (
        <div key={row} className="flex gap-3">
          <Skeleton className="size-20 shrink-0" />
          <div className="grid flex-1 content-start gap-2">
            <Skeleton className="h-4 w-40" />
            <Skeleton className="h-3 w-24" />
            <Skeleton className="h-8 w-28" />
          </div>
        </div>
      ))}
      <Skeleton className="h-10 w-full" />
    </div>
  );
}

Examples

Long product names

  • Handmade stoneware dinner plate set with speckled glaze, 4 pieces

    Colour: Oat, Size: 27 cm, Gift wrapped

    $96.00 each

    $96.00
  • Linen napkins

    Pack of 6

    $28.00 each

    $56.00

Order summary

Subtotal
$152.00
Shipping
$0.00
Tax
$0.00
Total
$152.00

A cart in a side panel

An order on its way

Order NK-2048

On its way
  1. Order confirmed

    Everyday notebook × 2 and Morning cup × 1

  2. Packed with care

    Your order left our Brooklyn studio.

  3. Out for delivery

    Arriving today, before 6 pm.

  4. Delivered

    We will let you know when it arrives.

Empty cart

Your cart is empty. Add a notebook or a cup to get started.

With promo code

  • Forest notebook

    Everyday notebook

    Forest / A5

    $18.00 each

    $36.00
  • Sand cup

    Morning cup

    Sand

    $32.00 each

    $32.00

Order summary

Subtotal
$68.00
Shipping
$6.00
Tax
$4.20
Total
$78.20

Props

Cart

A controlled or local cart with derived subtotal and an empty state. Compose with Sheet for a side panel.

PropTypeDefaultDescription
value / defaultValue / onValueChangeCartLine[] / callback–Rows: id, name, optional image/options, price, quantity and maxQuantity.
summaryOmit<OrderSummaryProps, 'subtotal' | 'currency'>–Shipping, tax, discounts and promo handlers for the summary.
currency / empty / childrenstring / ReactNode / ReactNode–Currency, custom empty content and actions such as checkout.

CartItem

A row for custom cart layouts.

PropTypeDefaultDescription
item / currencyCartLine / string–Product information and amount formatting.
onQuantityChange / onRemovecallback–Handlers for the quantity and removal controls.

QuantityStepper

A NumberField adapter with a default quantity range of 1–99.

PropTypeDefaultDescription
value / defaultValue / onValueChange / min / maxNumberFieldProps–The same controlled state and keyboard contract as NumberField. Give it an aria-label.

OrderSummary

Subtotal, shipping, tax, absolute discount and clamped total, plus an optional promo form.

PropTypeDefaultDescription
subtotal / shipping / tax / discountnumber–Amounts in major currency units.
onApplyPromo(code) => PromoResult | Promise<PromoResult>–Return { discount, label? } or { error }. No request is made by the component.
onRemovePromo / onDiscountChangecallback–Tell the app when the promo is removed or its discount changes.

Accessibility

Quantity entry reuses NumberField for bounds, arrow keys and hold-to-repeat buttons. Removal moves focus to Undo and announces the removed item. Promo errors are linked to the field and announced.

Checked against WCAG 2.1 AA with automated axe tests and keyboard tests on every release.