Cart Card
Cart with quantity steppers, remove, promo code field and totals.
"use client"
import { useState } from "react"
import { MinusIcon, PlusIcon, TrashIcon } from "@phosphor-icons/react"
import { Button, buttonVariants } from "@/components/ui/button"
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Separator } from "@/components/ui/separator"
const COPY = {
title: "Your cart",
empty: "Your cart is empty.",
promoLabel: "Promo code",
promoPlaceholder: "Enter code",
apply: "Apply",
remove: "Remove",
promoApplied: (code: string, percent: number) => `${code}: ${percent}% off`,
promoInvalid: "That code isn't valid.",
discount: "Discount",
subtotal: "Subtotal",
delivery: "Delivery",
shippingValue: "Free",
total: "Total",
checkout: "Checkout",
} as const
const ITEMS = [
{
id: "mat",
name: "Orbcore Desk Mat",
variant: "Slate, large",
price: 48,
image: "https://01orbital.dev/art/kit/tide.png",
},
{
id: "tidy",
name: "Felt Cable Tidy",
variant: "Oat",
price: 12,
image: "https://01orbital.dev/art/kit/bone-plume.png",
},
{
id: "lamp",
name: "Arc Task Lamp",
variant: "Matte black",
price: 89,
image: "https://01orbital.dev/art/kit/newman-blue.png",
},
] as const
// Codes the sample store accepts, as a percentage off the subtotal.
const PROMOS: Record<string, number> = { ORBIT10: 10, FRIENDS20: 20 }
const money = (n: number) => `$${n.toFixed(2)}`
export default function StoreCart01() {
const [qty, setQty] = useState<Record<string, number>>({
mat: 1,
tidy: 2,
lamp: 1,
})
const [code, setCode] = useState("")
const [promo, setPromo] = useState<string | null>(null)
const [promoError, setPromoError] = useState(false)
const rows = ITEMS.filter((i) => qty[i.id] > 0)
const subtotal = rows.reduce((s, i) => s + i.price * qty[i.id], 0)
const percent = promo ? PROMOS[promo] : 0
const discount = (subtotal * percent) / 100
const change = (id: string, d: number) =>
setQty((q) => ({ ...q, [id]: Math.max(0, q[id] + d) }))
const applyPromo = () => {
const key = code.trim().toUpperCase()
if (key in PROMOS) {
setPromo(key)
setPromoError(false)
setCode("")
} else {
setPromoError(true)
}
}
return (
<Card className="mx-auto w-full max-w-xl">
<CardHeader>
<CardTitle className="text-lg">{COPY.title}</CardTitle>
<CardDescription>
{rows.length} {rows.length === 1 ? "item" : "items"}
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-4">
{rows.length === 0 && (
<p className="py-6 text-center text-muted-foreground">{COPY.empty}</p>
)}
{rows.map((i) => (
<div key={i.id} className="flex items-center gap-4">
<img
src={i.image}
alt=""
width={64}
height={64}
loading="lazy"
className="size-16 shrink-0 rounded-2xl object-cover"
/>
<div className="min-w-0 flex-1">
<p className="truncate font-medium">{i.name}</p>
<p className="text-muted-foreground">{i.variant}</p>
<div className="mt-2 flex items-center gap-1">
<Button
variant="outline"
size="icon-xs"
aria-label={`Decrease ${i.name}`}
onClick={() => change(i.id, -1)}
>
<MinusIcon />
</Button>
<span className="w-8 text-center tabular-nums">
{qty[i.id]}
</span>
<Button
variant="outline"
size="icon-xs"
aria-label={`Increase ${i.name}`}
onClick={() => change(i.id, 1)}
>
<PlusIcon />
</Button>
</div>
</div>
<div className="flex flex-col items-end gap-1">
<span className="font-medium tabular-nums">
{money(i.price * qty[i.id])}
</span>
<Button
variant="ghost"
size="icon-xs"
aria-label={`Remove ${i.name}`}
onClick={() => change(i.id, -qty[i.id])}
>
<TrashIcon />
</Button>
</div>
</div>
))}
<Separator />
{promo ? (
<div className="flex items-center justify-between gap-2 text-sm">
<span>{COPY.promoApplied(promo, percent)}</span>
<Button variant="ghost" size="sm" onClick={() => setPromo(null)}>
{COPY.remove}
</Button>
</div>
) : (
<form
className="grid gap-2"
onSubmit={(event) => {
event.preventDefault()
applyPromo()
}}
>
<div className="flex gap-2">
<Input
aria-label={COPY.promoLabel}
aria-invalid={promoError || undefined}
placeholder={COPY.promoPlaceholder}
value={code}
onChange={(event) => {
setCode(event.target.value)
setPromoError(false)
}}
/>
<Button type="submit" variant="outline" disabled={!code.trim()}>
{COPY.apply}
</Button>
</div>
{promoError && (
<p className="text-sm text-destructive" role="alert">
{COPY.promoInvalid}
</p>
)}
</form>
)}
<dl className="flex flex-col gap-2">
<div className="flex justify-between">
<dt className="text-muted-foreground">{COPY.subtotal}</dt>
<dd className="tabular-nums">{money(subtotal)}</dd>
</div>
{promo && (
<div className="flex justify-between">
<dt className="text-muted-foreground">{COPY.discount}</dt>
<dd className="tabular-nums">-{money(discount)}</dd>
</div>
)}
<div className="flex justify-between">
<dt className="text-muted-foreground">{COPY.delivery}</dt>
<dd>{COPY.shippingValue}</dd>
</div>
<div className="flex justify-between font-medium">
<dt>{COPY.total}</dt>
<dd className="tabular-nums">{money(subtotal - discount)}</dd>
</div>
</dl>
</CardContent>
<CardFooter>
{rows.length === 0 ? (
<Button size="lg" className="w-full" disabled>
{COPY.checkout}
</Button>
) : (
<a
href="#checkout"
data-slot="button"
className={buttonVariants({ size: "lg", className: "w-full" })}
>
{COPY.checkout}
</a>
)}
</CardFooter>
</Card>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/cart-card-01npx shadcn@latest add @01orbital/cart-card-01yarn dlx shadcn@latest add @01orbital/cart-card-01bunx --bun shadcn@latest add @01orbital/cart-card-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/store/cart-card-01/index.tsx: change them there, not in the markup. ITEMSandPROMOS, also at the top of the file, hold its lists and settings.
Built from
npm: @phosphor-icons/react. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on