# Cart Button
> An Add button that opens into a quantity stepper in place, with a bin at one and press-and-hold to repeat.

## Installation

```bash
npx shadcn@latest add @kokonutui/cart-button
```

## Props

| Prop                      | Type                  | Default      | Description                                   |
|---------------------------|-----------------------|--------------|-----------------------------------------------|
| `items?`                 | `CartItem[]`         | `built-in`   |                                               |
| `defaultQuantities?`     | `Record<string, number>` | `{ croissant: 2 }` | Starting quantities, by item id.             |
| `currency?`              | `string`              | `"$"`      |                                               |
| `onCheckout?`            | `(quantities: Record<string, number>) => void` |  |                                               |
| `className?`             | `string`              |              | Extra classes for the root element.          |

### CartItem

| Field     | Type    | Description                                           |
|-----------|---------|-------------------------------------------------------|
| `id`     | `string` |                                                       |
| `name`   | `string` |                                                       |
| `detail` | `string` | Pack size or weight, shown under the name.          |
| `price`  | `number` |                                                       |
| `icon`   | `LucideIcon` |                                                   |
| `tint`   | `string` | Tailwind classes for the icon tile.                  |
| `max?`   | `number` |                                                       |

## Source

```tsx
// ...
// Cart Button implementation
// ...
```

export interface CartItem {
  id: string;
  name: string;
  /** Pack size or weight, shown under the name. */
  detail: string;
  price: number;
  icon: LucideIcon;
  /** Tailwind classes for the icon tile. */
  tint: string;
  max?: number;
}

export interface CartButtonProps {
  items?: CartItem[];
  /** Starting quantities, by item id. */
  defaultQuantities?: Record<string, number>;
  currency?: string;
  onCheckout?: (quantities: Record<string, number>) => void;
  className?: string;
}

const DEFAULT_ITEMS: CartItem[] = [
  {
    id: "oat-milk",
    name: "Oat milk",
    detail: "1 L · Barista",
    price: 3.2,
    icon: Milk,
    tint: "bg-sky-50 text-sky-600 dark:bg-sky-500/10 dark:text-sky-300",
    max: 6,
  },
  {
    id: "croissant",
    name: "Butter croissant",
    detail: "Baked this morning",
    price: 2.6,
    icon: Croissant,
    tint: "bg-orange-50 text-orange-600 dark:bg-orange-500/10 dark:text-orange-300",
    max: 8,
  },
  {
    id: "cherries",
    name: "Cherries",
    detail: "500 g punnet",
    price: 5.8,
    icon: Cherry,
    tint: "bg-rose-50 text-rose-600 dark:bg-rose-500/10 dark:text-rose-300",
    max: 3,
  },
];

const money = (value: number, currency: string) =>
  `${currency}${value.toFixed(2)}`;

export default function CartButton({
  items = DEFAULT_ITEMS,
  defaultQuantities = { croissant: 2 },
  currency = "$",
  onCheckout,
  className,
}: CartButtonProps) {
  const [quantities, setQuantities] = useState(defaultQuantities);
  const reduceMotion = Boolean(useReducedMotion());

const count = items.reduce(
    (sum, item) => sum + (quantities[item.id] ?? 0),
    0
  );
  const total = items.reduce(
    (sum, item) => sum + (quantities[item.id] ?? 0) * item.price,
    0
  );

let summary = "Your cart is empty";
  if (count > 0) {
    summary = `Subtotal · ${count} ${count === 1 ? "item" : "items"}`;
  }

// Component return statement continues...
  
  return (
    <div className={cn(...)}>
      <ul className={cn(...)}>
        {items.map((item) => {
          const Icon = item.icon;
          return (
            <li className="flex items-center gap-3 py-3" key={item.id}>
              <span className={cn(...)>
                <Icon aria-hidden className="size-5" strokeWidth={1.75} />
              </span>
              <span className="min-w-0 flex-1">
                <span className="block truncate font-medium text-sm text-zinc-900 dark:text-zinc-100">
                  {item.name}
                </span>
                <span className="block truncate text-xs text-zinc-500 dark:text-zinc-400">
                  <span className="text-zinc-700 tabular-nums dark:text-zinc-300">
                    {money(item.price, currency)}
                  </span>
                  {` · ${item.detail}`}
                </span>
              </span>
              <AddToCartButton
                max={item.max}
                name={item.name}
                onQuantityChange={(next) =>
                  setQuantities((current) => ({ ...current, [item.id]: next }))
                }
                quantity={quantities[item.id] ?? 0}
              />
            </li>
          );
        })}
      </ul>
      // Additional UI for checkout...
    </div>
  );
}
