npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@haus-storefront-react/product-variant-selector

v0.0.50

Published

A headless, flexible product variant selector component for e-commerce storefronts. Supports multiple UI patterns (pills, dropdowns, radio buttons), option availability tracking, and variant selection. Built with TypeScript, accessibility-first design, an

Readme

Product Variant Selector Component

A headless, flexible product variant selector component for e-commerce storefronts. Supports multiple UI patterns (pills, dropdowns, radio buttons), option availability tracking, and variant selection. Built with TypeScript, accessibility-first design, and platform-agnostic architecture.

Installation

npm install @haus-storefront-react/product-variant-selector
# or
yarn add @haus-storefront-react/product-variant-selector

Usage Example (Pills)

import { ProductVariantSelector } from '@haus-storefront-react/product-variant-selector'

const productId = '123'
const initialVariantId = 'variant-456'

<ProductVariantSelector.Root
  productId={productId}
  initialVariantId={initialVariantId}
>
  {({ optionGroups }) => (
    <div>
      {/* Render option groups */}
      {optionGroups.map((optionGroup) => (
        <ProductVariantSelector.OptionGroup
          key={optionGroup.code}
          optionGroup={optionGroup}
        >
          {({ options, selectOption, selectedOptionId }) => (
            <div>
              <label>{optionGroup.name}</label>

              {/* Pill-style options */}
              <div style={{ display: 'flex', gap: '8px' }}>
                {options.map((option) => (
                  <ProductVariantSelector.Option
                    key={option.id}
                    optionId={option.id}
                  >
                    {({ option: optionData, selectOption, isSelected, isAvailable }) => (
                      <button
                        onClick={() => selectOption(optionData.id)}
                        disabled={!isAvailable}
                        style={{
                          padding: '8px 16px',
                          borderRadius: '21px',
                          border: isSelected ? '2px solid #2c5282' : '1px solid #ccc',
                          backgroundColor: isSelected ? '#2c5282' : 'white',
                          color: isSelected ? 'white' : '#333',
                          opacity: isAvailable ? 1 : 0.5,
                          cursor: isAvailable ? 'pointer' : 'not-allowed',
                        }}
                      >
                        {optionData.name}
                      </button>
                    )}
                  </ProductVariantSelector.Option>
                ))}
              </div>
            </div>
          )}
        </ProductVariantSelector.OptionGroup>
      ))}
    </div>
  )}
</ProductVariantSelector.Root>

Dropdown Example

<ProductVariantSelector.Root productId={productId} initialVariantId={initialVariantId}>
  {({ optionGroups, selectedVariant }) => (
    <div>
      {optionGroups.map((optionGroup) => (
        <ProductVariantSelector.OptionGroup key={optionGroup.code} optionGroup={optionGroup}>
          {({ options, selectOption, selectedOptionId }) => (
            <div>
              <label>{optionGroup.name}</label>

              <select
                onChange={(e) => {
                  if (e.target.value) {
                    selectOption(e.target.value)
                  }
                }}
                value={selectedOptionId || ''}
              >
                <option value="">Select {optionGroup.name}</option>
                {options.map((option) => (
                  <ProductVariantSelector.Option key={option.id} optionId={option.id} asChild>
                    {({ option: optionData, isSelected, isAvailable }) => (
                      <option value={optionData.id} disabled={!isAvailable} selected={isSelected}>
                        {optionData.name}
                      </option>
                    )}
                  </ProductVariantSelector.Option>
                ))}
              </select>
            </div>
          )}
        </ProductVariantSelector.OptionGroup>
      ))}
    </div>
  )}
</ProductVariantSelector.Root>

Features

  • 🎯 Product variant selection that can be used with multiple UI patterns
  • ♿ Accessibility-first, platform-agnostic
  • 🔄 Real-time option availability tracking
  • 🎨 Headless, fully customizable
  • ⚡ TypeScript support
  • 🔗 Compound component pattern
  • 📱 Responsive design support
  • 🎛️ Flexible option rendering (pills, dropdowns, radio buttons)

API Reference

<ProductVariantSelector.Root>

Context provider for product variant selection functionality.

Props:

  • productId?: string – Product ID for fetching variant data
  • productSlug?: string – Product slug (alternative to productId)
  • initialVariantId?: string – Initial selected variant ID
  • autoSelectOnInvalidOption?: boolean – Auto-select first available variant when current selection becomes invalid
  • hideUnavailableOptions?: boolean – Hide options that are not available with current selection
  • children: (context) => ReactNode – Render prop with variant selector context

Context:

{
  optionGroups: ProductOptionGroup[]
  selectedVariant: ProductVariant | undefined
}

<ProductVariantSelector.OptionGroup>

Wraps a group of related options (e.g., "Size", "Color").

Props:

  • optionGroup: ProductOptionGroup – The option group object
  • asChild?: boolean – Render as child component
  • children: (context) => ReactNode – Render prop with option group context

Context:

{
  options: ProductOption[]
  selectOption: (optionId: string) => void
  selectedOptionId?: string
}

<ProductVariantSelector.Option>

Represents a single option within an option group.

Props:

  • optionId: string – The option ID
  • asChild?: boolean – Render as child component (useful for native HTML elements like <option>)
  • children: (context) => ReactNode – Render prop with option context

Context:

{
  option: ProductOption
  selectOption: (optionId: string) => void
  isSelected: boolean
  isAvailable: boolean
}

UI Patterns

Pill/Button Pattern

Use for visual, interactive option selection:

<ProductVariantSelector.Option optionId={option.id}>
  {({ option, selectOption, isSelected, isAvailable }) => (
    <button
      onClick={() => selectOption(option.id)}
      disabled={!isAvailable}
      className={isSelected ? 'selected' : isAvailable ? 'available' : 'disabled'}
    >
      {option.name}
    </button>
  )}
</ProductVariantSelector.Option>

Dropdown Pattern

Use with native <select> elements:

<ProductVariantSelector.Option optionId={option.id} asChild>
  {({ option, isSelected, isAvailable }) => (
    <option value={option.id} disabled={!isAvailable} selected={isSelected}>
      {option.name}
    </option>
  )}
</ProductVariantSelector.Option>

Radio Button Pattern

Use for form-like selection:

<ProductVariantSelector.Option optionId={option.id}>
  {({ option, selectOption, isSelected, isAvailable }) => (
    <label>
      <input
        type="radio"
        checked={isSelected}
        disabled={!isAvailable}
        onChange={() => selectOption(option.id)}
      />
      {option.name}
    </label>
  )}
</ProductVariantSelector.Option>

State Management

The component automatically manages:

  • Selected options per option group
  • Available options based on current selection
  • Variant matching to find the selected variant
  • Option availability tracking

Option Availability

Options become unavailable when:

  • They don't exist in any variant with the current selection

Selection Logic

  • Selecting an option updates all option groups
  • Unavailable options that does not exsist in a available product variant are automatically filtered (if hideUnavailableOptions is true)
  • Invalid selections are automatically corrected (if autoSelectOnInvalidOption is true)