Specification
- Catalogue
- No. 27 of 110
- Section
- Buttons and toggles
- Interaction
- Feedback, Layout Animation
- Element
- Button
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
After the first add, the button turns into a quantity stepper in the same spot. Both controls share a grid cell and swap with a clip-path inset, so the change looks like the button opening rather than being replaced.
The cart badge bumps each time the count changes. The count itself re-mounts on change so it can rise in, and the stepper's minus button removes the item when the count is one.
The hidden control is taken out of the tab order and hidden from assistive technology while the other one is showing.
Code
React and plain CSS"use client";
import { useEffect, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./add-to-cart.module.css";
const DEMO = [0, 1, 2, 3, 2, 1, 0];
const MAX = 9;
export default function AddToCart({ active, context }: LivePreviewProps) {
const [quantity, setQuantity] = useState(0);
const [step, setStep] = useState(0);
const demo = active && context === "card";
// Each time the demo starts, it starts from the first step.
const [demoRunning, setDemoRunning] = useState(demo);
if (demoRunning !== demo) {
setDemoRunning(demo);
setStep(0);
}
useEffect(() => {
if (!demo) return;
const id = window.setInterval(() => setStep((s) => s + 1), 750);
return () => window.clearInterval(id);
}, [demo]);
const count = context === "card" ? (demo ? (DEMO[step % DEMO.length] ?? 0) : 0) : quantity;
const inCart = count > 0;
const card = context === "card" ? -1 : 0;
return (
<div className={styles.root}>
<div className={styles.cart} aria-live="polite">
<svg width="22" height="22" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M5 8h14l-1.2 10.2a2 2 0 0 1-2 1.8H8.2a2 2 0 0 1-2-1.8L5 8Z" stroke="currentColor" strokeWidth="2" strokeLinejoin="round" />
<path d="M9 8V7a3 3 0 0 1 6 0v1" stroke="currentColor" strokeWidth="2" strokeLinecap="round" />
</svg>
<span key={count} className={styles.badge} data-visible={inCart || undefined}>
{count}
</span>
<span className="visually-hidden">{count === 1 ? "1 item in cart" : `${count} items in cart`}</span>
</div>
<div className={styles.product}>
<span className={styles.image} aria-hidden="true">
<svg width="44" height="44" viewBox="0 0 24 24" fill="none">
<path d="M5 9h14l-1 11H6L5 9Z" fill="currentColor" fillOpacity="0.25" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
<path d="M9 9V7a3 3 0 0 1 6 0v2" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" />
</svg>
</span>
<div className={styles.info}>
<p className={styles.name}>Canvas tote</p>
<p className={styles.price}>$38</p>
</div>
</div>
<div className={styles.action} data-in-cart={inCart || undefined}>
<button type="button" className={styles.add} tabIndex={inCart ? -1 : card} aria-hidden={inCart || undefined} onClick={() => setQuantity(1)}>
Add to cart
</button>
<div className={styles.stepper} aria-hidden={!inCart || undefined}>
<button
type="button"
className={styles.stepButton}
tabIndex={inCart ? card : -1}
aria-label={count === 1 ? "Remove from cart" : "Remove one"}
onClick={() => setQuantity((q) => Math.max(0, q - 1))}
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M6 12h12" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" />
</svg>
</button>
<span key={count} className={styles.count}>
{Math.max(count, 1)}
</span>
<button
type="button"
className={styles.stepButton}
tabIndex={inCart ? card : -1}
aria-label="Add one more"
disabled={count >= MAX}
onClick={() => setQuantity((q) => Math.min(MAX, q + 1))}
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M12 6v12M6 12h12" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" />
</svg>
</button>
</div>
</div>
</div>
);
}