cart button.md

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

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

// ...
// 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 (

// Additional UI for checkout...
); }