Back to elements
ElementForm & InputFree

Variant Swatches React component

Product option pickers for colour and size: round colour swatches with a ring that glides to the pick, size chips with a sliding fill, the chosen value named in the label, sold-out options struck through, and a low-stock warning when only a few are left.

When to use the Variant Swatches component

Use Variant Swatches on product pages and quick-add dialogs wherever shoppers pick a colour, size or other option. Seeing sold-out and low-stock options up front saves a disappointed add-to-cart.

Keep the option names short (they appear in the label and tooltip) and update the product image when the colour changes.

Installation

Add the Variant Swatches component to your project with the shadcn CLI. It lands in components/variant-swatches.tsx as plain React + Tailwind CSS source you own and can edit.

Install
Copied!
npx shadcn@latest add https://reactframe.com/r/variant-swatches.json

Or copy the source by hand and install its dependencies:

Install
Copied!
npm install motion clsx tailwind-merge

Props

<VariantSwatches /> accepts 8 props, all optional.

  • groups
    { name: string; type: "color" | "size"; options: SwatchOption[] }[]
    Default: DEFAULT_GROUPS
  • value

    Selected value per group name.

    Record<string, string>
  • defaultValue
    Record<string, string>
    Default: { Color: "black", Size: "s" }
  • onValueChange
    (value: Record<string, string>) => void
  • lowStock

    Show "Only n left" when stock is at or below this.

    number
    Default: 3
  • size
    "sm" | "md" | "lg"
    Default: "md"
  • theme
    "dark" | "light"
    Default: "dark"
  • className
    string

FAQ

How are sold-out options shown?
Colour swatches fade with a diagonal strike and size chips get struck through; both stay visible but can't be selected, and screen readers hear 'sold out'.
When does the low-stock warning appear?
When the selected option's stock is at or below lowStock (3 by default). Size chips with low stock also carry a small dot.
Can I add more option types?
Add as many groups as you need; use type size for any text options (material, length, pack size).

Related components

Content Switch
Free
Floating Label Input
Free
Input Group
Free
Blockquote
Free
Basic Table
Free
List Group
Free