Skip to content
Product block

Faceted Refinement Panel

Faceted search with result counts on every option, zero-count options kept visible and disabled so the list stays stable, and a note explaining why the counts do not sum.

276 lineslucide-reactAdded 31 Aug 2026Updated 13 Sept 2026
  • search
  • facets
  • filters
  • refinement
  • sidebar

What's included

  • components/search-facet-panel.tsx
  • Needs lucide-react

Works with

  • React
  • Next.js
  • Tailwind CSS
  • TypeScript

npx hoverlab add search-facet-panel

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

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

Source

components/search-facet-panel.tsx
'use client'

/**
 * <SearchFacetPanel> — refinement with counts, including the zeroes.
 *
 * The catalog has a product filter sidebar and an applied-filters bar. This
 * is the third piece and the one that makes faceted search usable rather
 * than merely present: the panel that shows how many results each option
 * would leave, before it is clicked.
 *
 * THE ZERO-COUNT DECISION
 *
 * Every faceted search has to choose what to do with an option that would
 * return nothing. Hiding it makes the list shorter and makes the facet
 * *look different every time you refine*, which is disorienting — options
 * vanish and reappear as you click around. Showing it disabled keeps the
 * list stable and tells you the option exists but is excluded by something
 * else. This shows them, greyed and unclickable, and that is a deliberate
 * choice rather than an oversight.
 *
 * MULTI-SELECT WITHIN A FACET, AND WHY THE COUNTS LOOK ODD
 *
 * Within one facet the options are OR-ed — picking Blue and Green shows
 * both — while separate facets are AND-ed. That is what every good search
 * does and what makes the counts within a facet not sum to the result
 * total. A note says so, because the alternative is a support ticket.
 *
 * COLLAPSING REMEMBERS. A facet the user collapsed stays collapsed as they
 * refine; nothing here springs back open on re-render.
 *
 * ACCESSIBILITY: each facet is a `<fieldset>` with a `<legend>` inside a
 * disclosure whose button carries both `aria-expanded` and `aria-controls`
 * — the audit flags the first without the second. Counts are inside each
 * checkbox's label so they are announced with the option.
 */

import * as React from 'react'
import { ChevronDown, RotateCcw } from 'lucide-react'

export interface FacetOption {
  id: string
  label: string
  /** Results remaining if this option were selected. Zero renders disabled. */
  count: number
}

export interface Facet {
  id: string
  label: string
  options: FacetOption[]
  /** Options beyond this are behind a "Show all". */
  visibleLimit?: number
}

export interface SearchFacetPanelProps {
  facets?: Facet[]
  resultCount?: number
  className?: string
}

/*
  Deliberately NOT this catalog's own numbers.

  These facets used to read Effects 973, Blocks 194, Pages 21 — Hoverlab's
  counts at the moment the block was written, baked into demo data that
  ships into other people's repositories. Two things were wrong with that.
  They went stale immediately, and nothing checks them because a
  component's example data is not a claim anyone thought to verify; and a
  reader who recognised the numbers would reasonably read the block as
  hard-wired to this catalog rather than as a generic facet panel.

  A neutral domain avoids both. Nothing here can go out of date, because
  nothing here is a fact about anything.
*/
const DEFAULT_FACETS: Facet[] = [
  {
    id: 'type',
    label: 'Type',
    options: [
      { id: 'doc', label: 'Documents', count: 1240 },
      { id: 'spreadsheet', label: 'Spreadsheets', count: 318 },
      { id: 'slide', label: 'Presentations', count: 96 },
      { id: 'archive', label: 'Archived', count: 44 },
    ],
  },
  {
    id: 'owner',
    label: 'Owner',
    options: [
      { id: 'me', label: 'Me', count: 212 },
      { id: 'team', label: 'My team', count: 884 },
      { id: 'shared', label: 'Shared with me', count: 402 },
      { id: 'external', label: 'External', count: 61 },
      { id: 'unassigned', label: 'Unassigned', count: 0 },
    ],
  },
  {
    id: 'category',
    label: 'Category',
    visibleLimit: 4,
    options: [
      { id: 'contracts', label: 'Contracts', count: 59 },
      { id: 'invoices', label: 'Invoices', count: 39 },
      { id: 'reports', label: 'Reports', count: 36 },
      { id: 'notes', label: 'Meeting notes', count: 38 },
      { id: 'specs', label: 'Specifications', count: 37 },
      { id: 'policies', label: 'Policies', count: 30 },
      { id: 'research', label: 'Research', count: 31 },
    ],
  },
  {
    id: 'licence',
    label: 'Licence',
    options: [
      { id: 'free', label: 'Free', count: 1_188 },
      { id: 'pro', label: 'Pro', count: 6 },
    ],
  },
]

export function SearchFacetPanel({
  facets = DEFAULT_FACETS,
  resultCount = 1_195,
  className = '',
}: SearchFacetPanelProps) {
  // 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 [selected, setSelected] = React.useState<Record<string, string[]>>({
    type: ['block'],
  })
  const [collapsed, setCollapsed] = React.useState<string[]>([])
  const [expandedLists, setExpandedLists] = React.useState<string[]>([])

  const totalSelected = Object.values(selected).flat().length

  function toggleOption(facetId: string, optionId: string) {
    setSelected((current) => {
      const existing = current[facetId] ?? []
      const next = existing.includes(optionId)
        ? existing.filter((id) => id !== optionId)
        : [...existing, optionId]

      // An empty array and an absent key mean the same thing; keeping only
      // one of the two representations stops `totalSelected` drifting.
      const copy = { ...current }
      if (next.length === 0) delete copy[facetId]
      else copy[facetId] = next
      return copy
    })
  }

  return (
    <aside
      className={`w-full max-w-xs rounded-2xl border border-border bg-card text-card-foreground ${className}`}
    >
      <header className="flex items-center justify-between gap-3 border-b border-border p-4">
        <div>
          <h2 className="text-sm font-semibold">Refine</h2>
          <p aria-live="polite" className="text-xs text-muted-foreground">
            {resultCount.toLocaleString('en-US')} results
          </p>
        </div>
        {totalSelected > 0 ? (
          <button
            type="button"
            onClick={() => setSelected({})}
            className="inline-flex items-center gap-1 rounded-lg px-2 py-1 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
          >
            <RotateCcw aria-hidden className="h-3 w-3" />
            Clear {totalSelected}
          </button>
        ) : null}
      </header>

      <div className="divide-y divide-border">
        {facets.map((facet) => {
          const open = !collapsed.includes(facet.id)
          const listId = `${uid}-facet-${facet.id}`
          const limit = facet.visibleLimit ?? facet.options.length
          const showingAll = expandedLists.includes(facet.id)
          const options = showingAll ? facet.options : facet.options.slice(0, limit)
          const chosen = selected[facet.id] ?? []

          return (
            <fieldset key={facet.id} className="p-4">
              <legend className="sr-only">{facet.label}</legend>

              <button
                type="button"
                aria-expanded={open}
                aria-controls={listId}
                onClick={() =>
                  setCollapsed((current) =>
                    current.includes(facet.id)
                      ? current.filter((id) => id !== facet.id)
                      : [...current, facet.id],
                  )
                }
                className="flex w-full items-center justify-between gap-2 text-sm font-medium transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
              >
                <span aria-hidden>{facet.label}</span>
                <span className="sr-only">
                  {facet.label}
                  {chosen.length > 0 ? `, ${chosen.length} selected` : ''}
                </span>
                <span className="flex items-center gap-1.5">
                  {chosen.length > 0 ? (
                    <span className="rounded-full bg-primary/10 px-1.5 text-[11px] font-semibold text-primary">
                      {chosen.length}
                    </span>
                  ) : null}
                  <ChevronDown
                    aria-hidden
                    className={`h-4 w-4 text-muted-foreground transition-transform ${open ? '' : '-rotate-90'}`}
                  />
                </span>
              </button>

              <div id={listId} hidden={!open} className="mt-3 space-y-1.5">
                {options.map((option) => {
                  const empty = option.count === 0
                  return (
                    <label
                      key={option.id}
                      className={`flex items-center gap-2.5 text-sm ${
                        empty ? 'cursor-not-allowed opacity-45' : 'cursor-pointer'
                      }`}
                    >
                      <input
                        type="checkbox"
                        disabled={empty}
                        checked={chosen.includes(option.id)}
                        onChange={() => toggleOption(facet.id, option.id)}
                        className="h-3.5 w-3.5 accent-primary"
                      />
                      <span className="flex-1">{option.label}</span>
                      <span className="tabular-nums text-xs text-muted-foreground">
                        {option.count.toLocaleString('en-US')}
                      </span>
                    </label>
                  )
                })}

                {facet.options.length > limit ? (
                  <button
                    type="button"
                    onClick={() =>
                      setExpandedLists((current) =>
                        current.includes(facet.id)
                          ? current.filter((id) => id !== facet.id)
                          : [...current, facet.id],
                      )
                    }
                    className="pt-1 text-xs font-medium text-primary underline-offset-4 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
                  >
                    {showingAll ? 'Show fewer' : `Show all ${facet.options.length}`}
                  </button>
                ) : null}
              </div>
            </fieldset>
          )
        })}
      </div>

      {/* The sentence that prevents the "these numbers do not add up" ticket. */}
      <p className="border-t border-border p-4 text-xs text-muted-foreground">
        Options within one group are combined with OR; separate groups are combined
        with AND. Counts show what each option alone would leave, so they do not sum
        to the total.
      </p>
    </aside>
  )
}

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/search-facet-panel.tsx and import it where you need the section:

import { SearchFacetPanel } from '@/components/search-facet-panel'

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
facetsFacet[]DEFAULT_FACETS
resultCountnumber1_195
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.

search-facet-panel.html
<!--
  Faceted Refinement Panel — 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.
-->
<aside class="w-full max-w-xs rounded-2xl border border-border bg-card text-card-foreground ">
  <header class="flex items-center justify-between gap-3 border-b border-border p-4">
    <div>
      <h2 class="text-sm font-semibold">Refine</h2>
      <p aria-live="polite" class="text-xs text-muted-foreground">1,195 results</p>
    </div>
    <button type="button" class="inline-flex items-center gap-1 rounded-lg px-2 py-1 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"><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-3 w-3" 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>Clear 1</button>
  </header>
  <div class="divide-y divide-border">
    <fieldset class="p-4">
      <legend class="sr-only">Type</legend>
      <button type="button" aria-expanded="true" aria-controls="_R_0_-facet-type" class="flex w-full items-center justify-between gap-2 text-sm font-medium transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
        <span aria-hidden="true">Type</span>
        <span class="sr-only">Type, 1 selected</span>
        <span class="flex items-center gap-1.5">
          <span class="rounded-full bg-primary/10 px-1.5 text-[11px] font-semibold text-primary">1</span>
          <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-chevron-down h-4 w-4 text-muted-foreground transition-transform" aria-hidden="true">
            <path d="m6 9 6 6 6-6"></path>
          </svg>
        </span>
      </button>
      <div id="_R_0_-facet-type" class="mt-3 space-y-1.5">
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Documents</span>
          <span class="tabular-nums text-xs text-muted-foreground">1,240</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Spreadsheets</span>
          <span class="tabular-nums text-xs text-muted-foreground">318</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Presentations</span>
          <span class="tabular-nums text-xs text-muted-foreground">96</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Archived</span>
          <span class="tabular-nums text-xs text-muted-foreground">44</span>
        </label>
      </div>
    </fieldset>
    <fieldset class="p-4">
      <legend class="sr-only">Owner</legend>
      <button type="button" aria-expanded="true" aria-controls="_R_0_-facet-owner" class="flex w-full items-center justify-between gap-2 text-sm font-medium transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
        <span aria-hidden="true">Owner</span>
        <span class="sr-only">Owner</span>
        <span class="flex items-center gap-1.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-chevron-down h-4 w-4 text-muted-foreground transition-transform" aria-hidden="true">
            <path d="m6 9 6 6 6-6"></path>
          </svg>
        </span>
      </button>
      <div id="_R_0_-facet-owner" class="mt-3 space-y-1.5">
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Me</span>
          <span class="tabular-nums text-xs text-muted-foreground">212</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">My team</span>
          <span class="tabular-nums text-xs text-muted-foreground">884</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Shared with me</span>
          <span class="tabular-nums text-xs text-muted-foreground">402</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">External</span>
          <span class="tabular-nums text-xs text-muted-foreground">61</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-not-allowed opacity-45">
          <input type="checkbox" disabled="" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Unassigned</span>
          <span class="tabular-nums text-xs text-muted-foreground">0</span>
        </label>
      </div>
    </fieldset>
    <fieldset class="p-4">
      <legend class="sr-only">Category</legend>
      <button type="button" aria-expanded="true" aria-controls="_R_0_-facet-category" class="flex w-full items-center justify-between gap-2 text-sm font-medium transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
        <span aria-hidden="true">Category</span>
        <span class="sr-only">Category</span>
        <span class="flex items-center gap-1.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-chevron-down h-4 w-4 text-muted-foreground transition-transform" aria-hidden="true">
            <path d="m6 9 6 6 6-6"></path>
          </svg>
        </span>
      </button>
      <div id="_R_0_-facet-category" class="mt-3 space-y-1.5">
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Contracts</span>
          <span class="tabular-nums text-xs text-muted-foreground">59</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Invoices</span>
          <span class="tabular-nums text-xs text-muted-foreground">39</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Reports</span>
          <span class="tabular-nums text-xs text-muted-foreground">36</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Meeting notes</span>
          <span class="tabular-nums text-xs text-muted-foreground">38</span>
        </label>
        <button type="button" class="pt-1 text-xs font-medium text-primary underline-offset-4 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">Show all 7</button>
      </div>
    </fieldset>
    <fieldset class="p-4">
      <legend class="sr-only">Licence</legend>
      <button type="button" aria-expanded="true" aria-controls="_R_0_-facet-licence" class="flex w-full items-center justify-between gap-2 text-sm font-medium transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
        <span aria-hidden="true">Licence</span>
        <span class="sr-only">Licence</span>
        <span class="flex items-center gap-1.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-chevron-down h-4 w-4 text-muted-foreground transition-transform" aria-hidden="true">
            <path d="m6 9 6 6 6-6"></path>
          </svg>
        </span>
      </button>
      <div id="_R_0_-facet-licence" class="mt-3 space-y-1.5">
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Free</span>
          <span class="tabular-nums text-xs text-muted-foreground">1,188</span>
        </label>
        <label class="flex items-center gap-2.5 text-sm cursor-pointer">
          <input type="checkbox" class="h-3.5 w-3.5 accent-primary" />
          <span class="flex-1">Pro</span>
          <span class="tabular-nums text-xs text-muted-foreground">6</span>
        </label>
      </div>
    </fieldset>
  </div>
  <p class="border-t border-border p-4 text-xs text-muted-foreground">Options within one group are combined with OR; separate groups are combined with AND. Counts show what each option alone would leave, so they do not sum to the total.</p>
</aside>
  • 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.

9 more blocks in Command & Search

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 Command & Search

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 Command & Search blocks

View category
Open the full page for this block

Command Palette

The ⌘K switcher done properly: focus stays in the input while aria-activedescendant moves, arrows wrap, and the highlight resets on every query change.

Command & Search280 lines1 dep

Grouped Search Results

Results grouped by section with visible breadcrumb paths and <mark>-highlighted matches — users recognise where a result lives faster than they read its snippet.

Command & Search194 lines1 dep
Open the full page for this block

Keyboard Shortcuts Sheet

The "?" overlay, with chords written once in one notation and rendered per platform — mod becomes Command on Apple and Ctrl everywhere else, decided after mount so the markup never mismatches.

Command & Search252 lines1 dep
Open the full page for this block

Applied Filters Bar

The strip above a result set that answers "why am I only seeing four things" — every active constraint as a removable chip, with the excluded count beside it.

Command & Search238 lines1 dep
Open the full page for this block

Header Search with Suggestions

The field in the header, not the palette over it: Enter searches for what you typed unless you arrowed to a suggestion, late responses are dropped by sequence number, and recent searches are removable one at a time.

Command & Search357 lines1 dep

Search Scope Switcher

One query counted across every scope it could have matched, as a real tablist with arrow-key movement — so an answer that is nowhere is visible without opening four tabs.

Command & Search247 lines1 dep
Open the full page for this block

Recent Search List

The empty state of a search box, filled with the searches that are worth repeating and the ones worth saving.

Command & Search150 linesNo deps
Open the full page for this block

Faceted Search Results

The whole faceted search assembled - rail, chips, live count and sort - with the intersection actually applied and relevance offered only when there is a query.

Command & Search388 lines1 dep
Open the full page for this block

Range Facet with Histogram

The facet that is a number: a distribution over a dual-thumb slider built from two real range inputs, with typed bounds clamped on blur rather than per keystroke.

Command & Search272 lines1 dep