Skip to Content

Product Filters

Product filter components for storefronts: checkbox facet accordions with live counts, a sort select, removable filter chips, a phone filter Sheet and paged results in a product grid. State comes from the useProductFilters hook.

Installation

pnpm dlx shadcn@latest add @01orbital/product-filters

Usage

"use client"

import {
  ProductFacets,
  ProductFilterChips,
  ProductFilterResults,
  ProductFiltersSheet,
  ProductSort,
} from "@/components/store/product-filters"
import {
  ProductTile,
  ProductTileDescription,
  ProductTileMedia,
  ProductTilePrice,
  ProductTileTitle,
} from "@/components/store/product-tile"
import { useProductFilters } from "@/hooks/use-product-filters"

// The copy: change text here, not in the markup.
const COPY = {
  filters: "Filters",
  filterAndSort: "Filter and sort",
  sort: "Sort",
  selected: "selected",
  remove: "Remove",
  clearAll: "Clear all",
  show: "Show",
  result: "print",
  results: "prints",
  showing: "Showing",
  of: "of",
  more: "Load more",
  emptyTitle: "No prints match these filters",
  emptyBody: "Remove a filter or two to see more prints.",
  clear: "Clear filters",
} as const

// Each facet's id names the product field it filters.
const FILTERS = [
  {
    id: "colors",
    name: "Color",
    options: [
      { value: "black", label: "Black" },
      { value: "bone", label: "Bone" },
      { value: "blue", label: "Blue" },
      { value: "green", label: "Green" },
      { value: "pink", label: "Pink" },
    ],
  },
  {
    id: "price",
    name: "Price",
    options: [
      { value: "under-50", label: "Under $50", max: 50 },
      { value: "50-100", label: "$50 to $100", min: 50, max: 100 },
      { value: "100-up", label: "$100 and up", min: 100 },
    ],
  },
] as const

const SORTS = [
  {
    value: "popular",
    label: "Most popular",
    key: "popularity",
    direction: "desc",
  },
  {
    value: "price-asc",
    label: "Price: low to high",
    key: "price",
    direction: "asc",
  },
  {
    value: "price-desc",
    label: "Price: high to low",
    key: "price",
    direction: "desc",
  },
] as const

const PRODUCTS = [
  {
    id: "navy-field",
    name: "Navy Field",
    description: "Navy and bone",
    href: "#",
    imageUrl: "https://01orbital.dev/art/kit/newman-blue.png",
    imageAlt: "Print of a deep navy field split by two pale vertical lines.",
    price: 38,
    colors: ["blue", "bone"],
    popularity: 96,
  },
  {
    id: "sage-line",
    name: "Sage Line",
    description: "Sage with a black edge",
    href: "#",
    imageUrl: "https://01orbital.dev/art/kit/newman-line-sage.png",
    imageAlt: "Print of a gray sage field with a black band at its edge.",
    price: 56,
    colors: ["green", "black"],
    popularity: 64,
  },
  {
    id: "ink-rings",
    name: "Ink Rings",
    description: "Black on white",
    href: "#",
    imageUrl: "https://01orbital.dev/art/kit/thomas-v2-596378.png",
    imageAlt: "Print of concentric rings of heavy black dashes on white.",
    price: 72,
    colors: ["black", "bone"],
    popularity: 88,
  },
  {
    id: "indigo-rings",
    name: "Indigo Rings",
    description: "Indigo on white",
    href: "#",
    imageUrl: "https://01orbital.dev/art/kit/thomas-v2-252538.png",
    imageAlt: "Print of concentric rings of small indigo dashes on white.",
    price: 68,
    colors: ["blue"],
    popularity: 79,
  },
  {
    id: "dawn-rings",
    name: "Dawn Rings",
    description: "Pink into pale yellow",
    href: "#",
    imageUrl: "https://01orbital.dev/art/kit/thomas-v2-86369.png",
    imageAlt:
      "Print of concentric rings of dashes fading from pink to pale yellow.",
    price: 88,
    colors: ["pink"],
    popularity: 52,
  },
  {
    id: "fern",
    name: "Fern",
    description: "Sage on black",
    href: "#",
    imageUrl: "https://01orbital.dev/art/kit/fern.png",
    imageAlt: "Print of branching sage lines spreading across a black ground.",
    price: 135,
    colors: ["green", "black"],
    popularity: 92,
  },
  {
    id: "tide",
    name: "Tide",
    description: "Blue on black",
    href: "#",
    imageUrl: "https://01orbital.dev/art/kit/tide.png",
    imageAlt: "Print of pale blue veins winding across a black ground.",
    price: 195,
    colors: ["blue", "black"],
    popularity: 84,
  },
  {
    id: "bone-plume",
    name: "Bone Plume",
    description: "Copper on bone",
    href: "#",
    imageUrl: "https://01orbital.dev/art/kit/bone-plume.png",
    imageAlt: "Print of copper threads branching across a bone ground.",
    price: 115,
    colors: ["bone"],
    popularity: 47,
  },
] as const

const PRICE_FORMAT = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
  maximumFractionDigits: 0,
})

// A small working shop: facets beside the results from 40rem, a filter
// Sheet below it. Black opens chosen so the chips show from the start.
export default function ProductFiltersDemo() {
  const filters = useProductFilters({
    products: PRODUCTS,
    facets: FILTERS,
    sorts: SORTS,
    initial: { colors: ["black"] },
  })
  const results = `${filters.results.length} ${filters.results.length === 1 ? COPY.result : COPY.results}`

  return (
    <div className="grid w-full max-w-5xl min-w-0 gap-6 p-6 sm:grid-cols-[11rem_minmax(0,1fr)] sm:gap-x-8">
      <aside aria-label={COPY.filters} className="hidden content-start sm:grid">
        <ProductFacets
          filters={filters}
          facets={FILTERS}
          selectedLabel={COPY.selected}
        />
      </aside>
      <div className="grid min-w-0 content-start gap-6">
        <div className="flex min-h-9 flex-wrap items-center justify-between gap-x-4 gap-y-3">
          <p
            role="status"
            className="text-sm/5 text-muted-foreground tabular-nums"
          >
            {results}
          </p>
          <ProductSort
            filters={filters}
            sorts={SORTS}
            label={COPY.sort}
            className="hidden sm:flex"
          />
          <ProductFiltersSheet
            filters={filters}
            facets={FILTERS}
            sorts={SORTS}
            title={COPY.filterAndSort}
            sortLabel={COPY.sort}
            selectedLabel={COPY.selected}
            submitLabel={`${COPY.show} ${results}`}
            className="sm:hidden"
          />
        </div>
        <ProductFilterChips
          filters={filters}
          facets={FILTERS}
          removeLabel={COPY.remove}
          clearLabel={COPY.clearAll}
        />
        <ProductFilterResults
          filters={filters}
          cols={3}
          showingLabel={COPY.showing}
          ofLabel={COPY.of}
          moreLabel={COPY.more}
          emptyTitle={COPY.emptyTitle}
          emptyDescription={COPY.emptyBody}
          clearLabel={COPY.clear}
        >
          {(product, linkId) => (
            <ProductTile key={product.id}>
              <ProductTileMedia>
                <img src={product.imageUrl} alt={product.imageAlt} />
              </ProductTileMedia>
              <ProductTileTitle>
                <a id={linkId} href={product.href}>
                  {product.name}
                </a>
              </ProductTileTitle>
              <ProductTileDescription>
                {product.description}
              </ProductTileDescription>
              <ProductTilePrice>
                {PRICE_FORMAT.format(product.price)}
              </ProductTilePrice>
            </ProductTile>
          )}
        </ProductFilterResults>
      </div>
    </div>
  )
}

This example installs with everything it uses:

pnpm dlx shadcn@latest add @01orbital/product-filters-demo

Anatomy

Product Filters is built from 7 parts. In the example they nest like this:

ProductFacets
ProductSort
ProductFiltersSheet
ProductFilterChips
ProductFilterResults
  • ProductFacets: Every facet open; the stock trigger draws no focus ring, so add one.
  • ProductSort: A labelled select for the sort order.
  • ProductFiltersSheet: The phone layout: sort and every facet in a drawer whose button reads the live result count. Children go above the sort (a category list, say).
  • ProductFilterChips: A removable chip for each chosen option, and a link that clears them all. It renders nothing when no option is chosen; focus moves to a neighbouring chip as one is removed.
  • ProductFilterResults: The results in batches of two rows; a new filter or sort starts again at the first batch. children draws one product, giving its link linkId so Load more can move focus to the first new product.
  • ProductFilterCount: A small count badge, for how many filters are chosen. It renders nothing at zero.
  • ProductFacetOptions: One checkbox per option with the count it would leave; an option that would leave nothing is disabled unless it is already chosen.

API reference

ProductFacets

Every facet open; the stock trigger draws no focus ring, so add one.

Prop

Type

ProductSort

A labelled select for the sort order.

Prop

Type

ProductFiltersSheet

The phone layout: sort and every facet in a drawer whose button reads the live result count. Children go above the sort (a category list, say).

Prop

Type

ProductFilterChips

A removable chip for each chosen option, and a link that clears them all. It renders nothing when no option is chosen; focus moves to a neighbouring chip as one is removed.

Prop

Type

ProductFilterResults

The results in batches of two rows; a new filter or sort starts again at the first batch. children draws one product, giving its link linkId so Load more can move focus to the first new product.

Prop

Type

ProductFilterCount

A small count badge, for how many filters are chosen. It renders nothing at zero.

Prop

Type

ProductFacetOptions

One checkbox per option with the count it would leave; an option that would leave nothing is disabled unless it is already chosen.

Prop

Type

Dependencies

npm: @phosphor-icons/react, cn. The CLI installs them for you.

Requires these pieces. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:

The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.

Used in blocks

2 blocks are built with Product Filters:

Last updated on