Skip to content
Commerce block

Variant & Add-to-Cart Box

Price, variant radiogroup, quantity stepper and add-to-bag — with sold-out sizes disabled and visible rather than hidden.

347 lineslucide-reactAdded 8 Aug 2026Updated 10 Sept 2026
  • buy box
  • variants
  • add to cart
  • quantity
  • product

What's included

  • components/product-buy-box.tsx
  • components/product-grid.tsx
  • Needs lucide-react

Works with

  • React
  • Next.js
  • Tailwind CSS
  • TypeScript

npx hoverlab add product-buy-box

Or over MCP, from your editor's agent — no account needed.

License

Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.

Was this useful?

Start a page with this section — add more, order them, and leave with the page source.

Preview

Merino Crew Sweater

£89.00£120.00Save £31.00
SizeSize guide

Quantity

1

  • Free delivery over £50.00
  • Free returns within 30 days

Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.

Source

components/product-buy-box.tsx
'use client'

/**
 * <ProductBuyBox> — price, variants, quantity and add-to-cart.
 *
 * The part people get wrong: **out-of-stock variants stay visible and
 * disabled**, they are not hidden. A size that vanishes from the picker
 * reads as "this shop does not make a Large", and the customer leaves. A
 * greyed-out Large with "out of stock" on it reads as "come back", which is
 * a different outcome entirely.
 *
 * Variant selection is a radiogroup, so arrow keys move between options and
 * the group is announced with its label. The quantity stepper's buttons
 * have real accessible names — "Increase quantity", not "+".
 *
 * Prices are integer minor units. See `product-grid.tsx` for why.
 */

import * as React from 'react'
import { Minus, Plus, ShoppingBag, Check, Truck, RotateCcw } from 'lucide-react'
import { formatPrice } from './product-grid'

export interface Variant {
  value: string
  label: string
  inStock: boolean
}

export interface ProductBuyBoxProps {
  name?: string
  price?: number
  compareAt?: number
  currency?: string
  locale?: string
  variantLabel?: string
  variants?: Variant[]
  maxQuantity?: number
  onAddToCart?: (value: { variant: string; quantity: number }) => void
  className?: string
}

const DEFAULT_VARIANTS: Variant[] = [
  { value: 'xs', label: 'XS', inStock: false },
  { value: 's', label: 'S', inStock: true },
  { value: 'm', label: 'M', inStock: true },
  { value: 'l', label: 'L', inStock: true },
  { value: 'xl', label: 'XL', inStock: false },
]

export function ProductBuyBox({
  name = 'Merino Crew Sweater',
  price = 8900,
  compareAt = 12000,
  currency = 'GBP',
  locale = 'en-GB',
  variantLabel = 'Size',
  variants = DEFAULT_VARIANTS,
  maxQuantity = 10,
  onAddToCart,
  className = '',
}: ProductBuyBoxProps) {
  // Per-instance ids. A literal id in a reusable component is a
  // collision waiting for the second copy on the page — and a <label>
  // then resolves to whichever input rendered first.
  const uid = React.useId()
  const firstAvailable = variants.find((v) => v.inStock)?.value ?? ''
  const [variant, setVariant] = React.useState(firstAvailable)
  const [quantity, setQuantity] = React.useState(1)
  const [added, setAdded] = React.useState(false)

  const saving = compareAt ? compareAt - price : 0

  function add() {
    if (!variant) return
    onAddToCart?.({ variant, quantity })
    setAdded(true)
    window.setTimeout(() => setAdded(false), 2000)
  }

  return (
    <div className={`flex flex-col ${className}`}>
      <h1 className="text-2xl leading-snug font-bold tracking-tight sm:text-3xl sm:leading-[1.3]">{name}</h1>

      <div className="mt-3 flex flex-wrap items-baseline gap-3">
        <span className="text-2xl font-extrabold tracking-tight">
          {formatPrice(price, currency, locale)}
        </span>
        {compareAt ? (
          <>
            <span className="text-base text-muted-foreground line-through">
              {formatPrice(compareAt, currency, locale)}
            </span>
            <span className="rounded-full bg-emerald-500/15 px-2 py-0.5 text-xs font-semibold text-emerald-600 dark:text-emerald-400">
              Save {formatPrice(saving, currency, locale)}
            </span>
          </>
        ) : null}
      </div>

      {/* Variants — sold-out options stay visible, disabled. */}
      <fieldset className="mt-6">
        <legend className="mb-2.5 flex w-full items-baseline justify-between text-sm font-medium">
          {variantLabel}
          <a href="#size-guide" className="text-xs font-normal text-muted-foreground hover:text-foreground hover:underline">
            Size guide
          </a>
        </legend>

        <div role="radiogroup" aria-label={variantLabel} className="flex flex-wrap gap-2">
          {variants.map((v) => {
            const selected = v.value === variant
            return (
              <button
                key={v.value}
                type="button"
                role="radio"
                aria-checked={selected}
                disabled={!v.inStock}
                onClick={() => setVariant(v.value)}
                title={v.inStock ? undefined : 'Out of stock'}
                className={`relative min-w-12 rounded-xl border px-3.5 py-2 text-sm font-medium transition-colors ${
                  selected
                    ? 'border-primary bg-primary/10 text-foreground'
                    : 'border-border/60 hover:border-foreground/30'
                } ${
                  !v.inStock
                    ? 'cursor-not-allowed text-muted-foreground/50 line-through'
                    : ''
                }`}
              >
                {v.label}
                {!v.inStock ? <span className="sr-only"> — out of stock</span> : null}
              </button>
            )
          })}
        </div>
      </fieldset>

      {/* Quantity */}
      <div className="mt-6">
        <p id={`${uid}-qty-label`} className="mb-2.5 text-sm font-medium">
          Quantity
        </p>
        <div className="inline-flex items-center rounded-xl border border-border/60">
          <button
            type="button"
            onClick={() => setQuantity((q) => Math.max(1, q - 1))}
            disabled={quantity <= 1}
            aria-label="Decrease quantity"
            className="rounded-s-xl p-2.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40"
          >
            <Minus aria-hidden className="h-4 w-4" />
          </button>

          <output
            aria-labelledby={`${uid}-qty-label`}
            className="w-12 text-center text-sm font-medium tabular-nums"
          >
            {quantity}
          </output>

          <button
            type="button"
            onClick={() => setQuantity((q) => Math.min(maxQuantity, q + 1))}
            disabled={quantity >= maxQuantity}
            aria-label="Increase quantity"
            className="rounded-e-xl p-2.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40"
          >
            <Plus aria-hidden className="h-4 w-4" />
          </button>
        </div>
      </div>

      <button
        type="button"
        onClick={add}
        disabled={!variant}
        className="mt-6 inline-flex items-center justify-center gap-2 rounded-xl bg-primary px-6 py-3.5 text-sm font-semibold text-primary-foreground transition-colors hover:bg-primary/90 disabled:opacity-50"
      >
        {added ? (
          <Check aria-hidden className="h-4 w-4" />
        ) : (
          <ShoppingBag aria-hidden className="h-4 w-4" />
        )}
        {added ? 'Added to bag' : 'Add to bag'}
      </button>

      <p aria-live="polite" className="sr-only">
        {added ? `${quantity} × ${name}, size ${variant}, added to bag` : ''}
      </p>

      <ul className="mt-6 space-y-2 border-t border-border/60 pt-5 text-sm text-muted-foreground">
        <li className="flex items-center gap-2.5">
          <Truck aria-hidden className="h-4 w-4 shrink-0" />
          Free delivery over {formatPrice(5000, currency, locale)}
        </li>
        <li className="flex items-center gap-2.5">
          <RotateCcw aria-hidden className="h-4 w-4 shrink-0" />
          Free returns within 30 days
        </li>
      </ul>
    </div>
  )
}

Before you paste

  • Styling is Tailwind utility classes on semantic tokens (bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.
  • Install: npm i lucide-react
  • Every prop has a default, so it renders standalone before you wire it up.

Where it goes

Drop it at components/product-buy-box.tsx and import it where you need the section:

import { ProductBuyBox } from '@/components/product-buy-box'

Customize

7 of this block’s props are simple enough to drive from here. Change them and the block below re-renders — it is the same component whose source is above, not a mock of it. Everything else it accepts is in the table underneath.

Props

Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.

PropTypeDefault
namestring'Merino Crew Sweater'
pricenumber8900
compareAtnumber12000
currencystring'GBP'
localestring'en-GB'
variantLabelstring'Size'
variantsVariant[]DEFAULT_VARIANTS
maxQuantitynumber10
onAddToCart(value: { variant: string; quantity: number }) => void—
classNamestring''

Not using React?

The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.

This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.

product-buy-box.html
<!--
  Variant & Add-to-Cart Box — markup from the Hoverlab catalog.

  This is the block rendered once to HTML and wrapped as a component
  file. It is not a port of the React source: the Tailwind classes carry
  the design, which is the part that took the work, and they are the same
  in every framework.

  This block is interactive in React and the handlers are NOT here.
  Buttons, toggles and menus render in their initial state and do
  nothing until you wire them up.
-->
<div class="flex flex-col ">
  <h1 class="text-2xl leading-snug font-bold tracking-tight sm:text-3xl sm:leading-[1.3]">Merino Crew Sweater</h1>
  <div class="mt-3 flex flex-wrap items-baseline gap-3">
    <span class="text-2xl font-extrabold tracking-tight">£89.00</span>
    <span class="text-base text-muted-foreground line-through">£120.00</span>
    <span class="rounded-full bg-emerald-500/15 px-2 py-0.5 text-xs font-semibold text-emerald-600 dark:text-emerald-400">Save £31.00</span>
  </div>
  <fieldset class="mt-6">
    <legend class="mb-2.5 flex w-full items-baseline justify-between text-sm font-medium">Size<a href="#size-guide" class="text-xs font-normal text-muted-foreground hover:text-foreground hover:underline">Size guide</a></legend>
    <div role="radiogroup" aria-label="Size" class="flex flex-wrap gap-2">
      <button type="button" role="radio" aria-checked="false" disabled="" title="Out of stock" class="relative min-w-12 rounded-xl border px-3.5 py-2 text-sm font-medium transition-colors border-border/60 hover:border-foreground/30 cursor-not-allowed text-muted-foreground/50 line-through">XS<span class="sr-only"> — out of stock</span></button>
      <button type="button" role="radio" aria-checked="true" class="relative min-w-12 rounded-xl border px-3.5 py-2 text-sm font-medium transition-colors border-primary bg-primary/10 text-foreground ">S</button>
      <button type="button" role="radio" aria-checked="false" class="relative min-w-12 rounded-xl border px-3.5 py-2 text-sm font-medium transition-colors border-border/60 hover:border-foreground/30 ">M</button>
      <button type="button" role="radio" aria-checked="false" class="relative min-w-12 rounded-xl border px-3.5 py-2 text-sm font-medium transition-colors border-border/60 hover:border-foreground/30 ">L</button>
      <button type="button" role="radio" aria-checked="false" disabled="" title="Out of stock" class="relative min-w-12 rounded-xl border px-3.5 py-2 text-sm font-medium transition-colors border-border/60 hover:border-foreground/30 cursor-not-allowed text-muted-foreground/50 line-through">XL<span class="sr-only"> — out of stock</span></button>
    </div>
  </fieldset>
  <div class="mt-6">
    <p id="_R_0_-qty-label" class="mb-2.5 text-sm font-medium">Quantity</p>
    <div class="inline-flex items-center rounded-xl border border-border/60">
      <button type="button" disabled="" aria-label="Decrease quantity" class="rounded-s-xl p-2.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus h-4 w-4" aria-hidden="true">
          <path d="M5 12h14"></path>
        </svg>
      </button>
      <output aria-labelledby="_R_0_-qty-label" class="w-12 text-center text-sm font-medium tabular-nums">1</output>
      <button type="button" aria-label="Increase quantity" class="rounded-e-xl p-2.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus h-4 w-4" aria-hidden="true">
          <path d="M5 12h14"></path>
          <path d="M12 5v14"></path>
        </svg>
      </button>
    </div>
  </div>
  <button type="button" class="mt-6 inline-flex items-center justify-center gap-2 rounded-xl bg-primary px-6 py-3.5 text-sm font-semibold text-primary-foreground transition-colors hover:bg-primary/90 disabled:opacity-50"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-shopping-bag h-4 w-4" aria-hidden="true"><path d="M16 10a4 4 0 0 1-8 0"></path><path d="M3.103 6.034h17.794"></path><path d="M3.4 5.467a2 2 0 0 0-.4 1.2V20a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6.667a2 2 0 0 0-.4-1.2l-2-2.667A2 2 0 0 0 17 2H7a2 2 0 0 0-1.6.8z"></path></svg>Add to bag</button>
  <p aria-live="polite" class="sr-only"></p>
  <ul class="mt-6 space-y-2 border-t border-border/60 pt-5 text-sm text-muted-foreground">
    <li class="flex items-center gap-2.5"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-truck h-4 w-4 shrink-0" aria-hidden="true"><path d="M14 18V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v11a1 1 0 0 0 1 1h2"></path><path d="M15 18H9"></path><path d="M19 18h2a1 1 0 0 0 1-1v-3.65a1 1 0 0 0-.22-.624l-3.48-4.35A1 1 0 0 0 17.52 8H14"></path><circle cx="17" cy="18" r="2"></circle><circle cx="7" cy="18" r="2"></circle></svg>Free delivery over £50.00</li>
    <li class="flex items-center gap-2.5"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-rotate-ccw h-4 w-4 shrink-0" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"></path><path d="M3 3v5h5"></path></svg>Free returns within 30 days</li>
  </ul>
</div>
  • This is rendered HTML, not a translation of the React source. The Tailwind classes carry the design and work in any framework.
  • It is one frame: the component in its initial state, with no props applied beyond the defaults.
  • This block is interactive in React — toggles, menus or form state. None of that survives here; the markup is the closed/default state and the handlers are gone. Re-wire them in your own framework.
  • Requires Tailwind, and the design tokens the classes reference (bg-card, text-muted-foreground, and so on). The template ZIPs ship a globals.css that defines them.

What each framework gets across the whole catalog — effects convert properly; this rung is markup.

For AI

The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.

See the prompt

Used in these pages

Want the whole screen instead of this one section? Open a page and copy it entire.

6 more blocks in Product Detail

All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.

Browse Product Detail

Shipping one commercially

Copying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.

  • A commercial licence for everything in the catalog
  • Unlimited bundle exports, in Vue, Svelte and Tailwind
  • One payment — no subscription, nothing to renew
Pro — $79 once

More Product Detail blocks

View category
Open the full page for this block

Product Details Accordion

Description, materials, delivery and returns in named <details> elements — no JavaScript, and the copy stays indexable while collapsed.

Product Detail156 lines1 dep
Open the full page for this block

Rating Distribution Summary

Average rating with a clickable histogram — because a 4.3 of forties and fives is a different product from a 4.3 of ones and fives.

Product Detail126 lines1 dep
Open the full page for this block

Size Guide

The panel that decides whether the parcel comes back: what the model is wearing and what previous buyers did, above a table whose inches are converted rather than maintained twice.

Product Detail227 lines1 dep
Open the full page for this block

Product Spec Split

The specification table beside the product image, with the two or three figures that decide the purchase pulled out of it.

Product Detail159 linesNo deps
Open the full page for this block

Back In Stock Form

The capture that replaces a dead sold-out button, with the honest caveat that a notification is not a reservation.

Product Detail177 linesNo deps