Add to Cart
An Add to cart button that morphs into a − qty + stepper after the first tap, and back to the button at zero.
$
View as Markdownnpx shadcn@latest add https://sundaeui.dev/r/add-to-cart.json'use client'
import { useRef, useState } from 'react'
import './add-to-cart.css'
export interface AddToCartProps {
name?: string
price?: string
defaultQuantity?: number
max?: number
onQuantityChange?: (n: number) => void
variant?: 'solid' | 'outline'
fill?: 'dark' | 'light'
className?: string
}
/** Add-to-cart button that becomes a quantity stepper. */
export function AddToCart({ name = 'Cold brew, 1L', price = '$8.50', defaultQuantity = 0, max = 10, onQuantityChange, variant = 'solid', fill = 'dark', className }: AddToCartProps) {
const [q, setQ] = useState(defaultQuantity)
const plus = useRef<HTMLButtonElement>(null)
const addBtn = useRef<HTMLButtonElement>(null)
const set = (n: number) => {
const c = Math.max(0, Math.min(max, n))
setQ(c); onQuantityChange?.(c)
requestAnimationFrame(() => (c === 0 ? addBtn.current : plus.current)?.focus())
}
return (
<div className={['sd-atc', className].filter(Boolean).join(' ')} data-fill={fill} data-style={variant} data-on={q > 0 || undefined}>
<div className="sd-atc__img" aria-hidden>{name[0]}{q > 0 && <span key={q} className="sd-atc__badge">{q}</span>}</div>
<div className="sd-atc__n">{name}</div>
<div className="sd-atc__p">{price}</div>
<div className="sd-atc__ctl">
<button ref={addBtn} type="button" className="sd-atc__add" tabIndex={q > 0 ? -1 : 0} aria-hidden={q > 0} onClick={() => set(1)}>
<svg viewBox="0 0 16 16" aria-hidden><path d="M2 3h2l1.5 7.5h7L14 5H5" /><circle cx="6.5" cy="13" r="1" /><circle cx="12" cy="13" r="1" /></svg>Add to cart
</button>
<div className="sd-atc__step" aria-hidden={q === 0}>
<button type="button" tabIndex={q > 0 ? 0 : -1} aria-label={q === 1 ? `Remove ${name} from cart` : `Decrease ${name}`} onClick={() => set(q - 1)}>−</button>
<span className="sd-atc__q" aria-live="polite"><span key={q}>{q}</span><span className="sr"> in cart</span></span>
<button ref={plus} type="button" tabIndex={q > 0 ? 0 : -1} aria-label={`Increase ${name}`} disabled={q >= max} onClick={() => set(q + 1)}>+</button>
</div>
</div>
</div>
)
}.sd-atc { --bg: #1c1c1c; --fg: #f6f6f6; --mute: #8e8e8e; --line: rgb(255 255 255 / 0.1); --soft: rgb(255 255 255 / 0.07); --strong: #fafafa; --on: #111;
color: var(--fg); font-size: 14px; line-height: 1.4; -webkit-tap-highlight-color: transparent; }
.sd-atc[data-fill='light'] { --bg: #fff; --fg: #121212; --mute: #6f6f6f; --line: rgb(0 0 0 / 0.09); --soft: rgb(0 0 0 / 0.05); --strong: #111; --on: #fff; }
.sd-atc button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
.sd-atc :focus-visible { outline: 2px solid var(--fg); outline-offset: 2px; }
.sd-atc .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.sd-atc { width: min(100%, 240px); padding: 12px; border-radius: 20px; background: var(--bg); box-shadow: inset 0 0 0 1px var(--line); }
.sd-atc__img { position: relative; height: 130px; border-radius: 12px; background: var(--soft); display: grid; place-items: center; font: 600 34px/1 ui-sans-serif, system-ui; color: var(--mute); }
.sd-atc__badge { position: absolute; top: 8px; right: 8px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 99px; background: var(--strong); color: var(--on); font-size: 11.5px; font-weight: 700; display: grid; place-items: center; font-variant-numeric: tabular-nums; animation: sd-atc-bump 320ms cubic-bezier(0.3, 1.8, 0.4, 1); }
@keyframes sd-atc-bump { 40% { transform: scale(1.3); } }
.sd-atc__n { margin-top: 10px; font-size: 14px; font-weight: 600; }
.sd-atc__p { font-size: 13px; color: var(--mute); margin-bottom: 10px; }
.sd-atc__ctl { position: relative; height: 42px; border-radius: 12px; background: var(--strong); color: var(--on); overflow: hidden; transition: background-color 220ms, box-shadow 220ms; }
.sd-atc[data-style='outline'] .sd-atc__ctl { background: transparent; color: var(--fg); box-shadow: inset 0 0 0 1.5px var(--fg); }
.sd-atc__add { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 8px; font-weight: 600; font-size: 14px; color: inherit !important; transition: opacity 200ms, transform 260ms cubic-bezier(0.3, 1.3, 0.4, 1); }
.sd-atc__add svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sd-atc__step { position: absolute; inset: 0; display: grid; grid-template-columns: 42px 1fr 42px; align-items: center; opacity: 0; transform: scale(0.9); pointer-events: none; transition: opacity 200ms, transform 260ms cubic-bezier(0.3, 1.3, 0.4, 1); }
.sd-atc[data-on] .sd-atc__add { opacity: 0; transform: scale(1.1); pointer-events: none; }
.sd-atc[data-on] .sd-atc__step { opacity: 1; transform: none; pointer-events: auto; }
.sd-atc__step button { height: 42px; font-size: 18px; color: inherit !important; }
.sd-atc__step button:hover { background: color-mix(in srgb, currentColor 10%, transparent) !important; }
.sd-atc__step button:disabled { opacity: 0.35; }
.sd-atc__q { text-align: center; font-weight: 700; font-size: 14px; font-variant-numeric: tabular-nums; }
.sd-atc__q span { display: inline-block; animation: sd-atc-roll 220ms ease; }
@keyframes sd-atc-roll { from { transform: translateY(6px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sd-atc *, .sd-atc *::before, .sd-atc *::after { animation: none !important; transition: none !important; } }Overview
First tap adds one and the button reshapes into a stepper in place, keeping its width so the layout never jumps. Going down to zero morphs it back.
A small cart badge elsewhere on the card bumps with each change.
When to use
- Product cards, menus, grocery lists.
When not to use
- Products needing options (size, colour) before adding.
Installation
$
pnpm dlx shadcn@latest add https://sundaeui.dev/r/add-to-cart.json$
npx shadcn@latest add https://sundaeui.dev/r/add-to-cart.json$
yarn dlx shadcn@latest add https://sundaeui.dev/r/add-to-cart.json$
bunx --bun shadcn@latest add https://sundaeui.dev/r/add-to-cart.jsonWorks in any React project. Files are written to components/sundae/. Read the source in the stage’s Code view.
- No dependencies beyond React 18 or later.
- Copy add-to-cart.tsx and add-to-cart.css into components/sundae/.
- The CSS file is imported by the component, so no extra configuration is needed.
- Import the component and use it as shown below.
Usage
import { AddToCart } from '@/components/sundae/add-to-cart'
<AddToCart />Props
| Prop | Type | Default |
|---|---|---|
quantity / defaultQuantityIn-cart quantity. | number | 0 |
maxMax per order. | number | 10 |
onQuantityChangeCalled on change. | (n: number) => void | — |
fillSurface: dark or light. | 'dark' | 'light' | 'dark' |
Accessibility
- Idle: a button named “Add to cart”. Active: decrease / increase buttons and a live quantity.
- Focus moves to the plus button after adding so keyboard users can keep going.
- Reduced motion removes the movement and keeps every state change.
Customization
- Colours are CSS custom properties on .sd-atc; override them in your own stylesheet.
- fill="light" switches the built-in light palette.
FAQ
- Is Add to Cart free?
- Yes. Add to Cart is free for personal and commercial projects, with full source on this page.
- How do I install Add to Cart?
- Run npx shadcn@latest add https://sundaeui.dev/r/add-to-cart.json. The files are written to components/sundae/. It has no dependencies beyond React 18 or later.
- Does Add to Cart support keyboard and screen readers?
- Idle: a button named “Add to cart”. Active: decrease / increase buttons and a live quantity. Focus moves to the plus button after adding so keyboard users can keep going.
- Does Add to Cart respect reduced motion?
- Reduced motion removes the movement and keeps every state change.
More like this
All components →Hold to Confirm
Components · RefinedFollow Button
Components · RefinedDownload Button
Components · RefinedBookmark Button
Components · RefinedSplit Button
Components · RefinedSubmit Button
Components · Refined