Overview

Filter chips with two modes. In multi-select, a chip turns dark and grows a check mark drawn in as you pick it, its count rolls to the new number, and a Clear chip pops in at the end while anything is selected. In single-select, one dark pill slides between options, with a soft hover wash following the pointer. Pair it with a list that animates its rows, as in the demo, and the whole filter feels connected.


Installation

Use the CLI to install the component automatically:

$ pnpm dlx shadcn@latest add https://ui.sanjid.in/r/filter-bar.json

Install dependencies:

$ pnpm add motion

Add the utility function for class merging:

lib/utils.ts
tsx
import { ClassValue, clsx } from "clsx";import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {  return twMerge(clsx(inputs));}

Copy the component code into your project:

components/ui/filter-bar.tsx
tsx
"use client";
import React, { useCallback, useId, useState } from "react";import { AnimatePresence, LayoutGroup, motion, useReducedMotion } from "motion/react";import { X } from "lucide-react";import { cn } from "@/lib/utils";
export type FilterBarOption = {  id: string;  label: string;  /** Result count shown after the label. */  count?: number;  icon?: React.ReactNode;};
export type FilterBarProps = {  options: FilterBarOption[];  value?: string[];  defaultValue?: string[];  onChange?: (value: string[]) => void;  /** Multi-select toggles chips; single-select slides one pill between them. */  multi?: boolean;  /** Show a Clear chip while anything is selected (multi only). */  clearable?: boolean;  className?: string;  "aria-label"?: string;};
const SPRING = { type: "spring" as const, bounce: 0.18, duration: 0.42 };
function Count({ value, on }: { value: number; on: boolean }) {  return (    <span      className={cn(        "relative inline-flex min-w-[1.25rem] justify-center overflow-hidden rounded-full px-1.5 text-[11px] leading-[18px] font-medium tabular-nums transition-colors duration-200",        on ? "bg-(--color-paper)/15 text-(--color-paper)" : "bg-(--color-paper-2) text-(--color-neutral)",      )}    >      <AnimatePresence initial={false} mode="popLayout">        <motion.span          key={value}          initial={{ y: 8, opacity: 0 }}          animate={{ y: 0, opacity: 1 }}          exit={{ y: -8, opacity: 0 }}          transition={{ type: "spring", bounce: 0, duration: 0.3 }}        >          {value}        </motion.span>      </AnimatePresence>    </span>  );}
export function FilterBar({  options,  value: valueProp,  defaultValue = [],  onChange,  multi = true,  clearable = true,  className,  "aria-label": ariaLabel = "Filters",}: FilterBarProps) {  const reduce = !!useReducedMotion();  const uid = useId();  const isControlled = valueProp !== undefined;  const [uncontrolled, setUncontrolled] = useState(defaultValue);  const selected = isControlled ? valueProp : uncontrolled;  const [hovered, setHovered] = useState<string | null>(null);  const spring = reduce ? { duration: 0 } : SPRING;
  const setSelected = useCallback(    (next: string[]) => {      if (!isControlled) setUncontrolled(next);      onChange?.(next);    },    [isControlled, onChange],  );
  const toggle = (id: string) => {    if (multi) {      // Keep the options' order, not the click order.      const next = selected.includes(id) ? selected.filter((x) => x !== id) : [...selected, id];      setSelected(options.map((o) => o.id).filter((o) => next.includes(o)));    } else {      setSelected(selected.includes(id) ? [] : [id]);    }  };
  /* ---------------------------- single select ---------------------------- */
  if (!multi) {    return (      <LayoutGroup id={uid}>        <div          role="group"          aria-label={ariaLabel}          onPointerLeave={() => setHovered(null)}          className={cn(            "relative isolate inline-flex w-fit max-w-full flex-wrap items-center gap-0.5 rounded-full p-1",            "bg-(--color-paper-2) shadow-[inset_0_0_0_1px_var(--color-rule)]",            className,          )}        >          {options.map((opt) => {            const on = selected.includes(opt.id);            return (              <button                key={opt.id}                type="button"                aria-pressed={on}                onClick={() => toggle(opt.id)}                onPointerEnter={() => setHovered(opt.id)}                className={cn(                  "relative inline-flex h-8 items-center gap-1.5 rounded-full px-3.5 text-[13px] font-medium whitespace-nowrap outline-none",                  "transition-colors duration-200 active:scale-[0.97]",                  "focus-visible:ring-2 focus-visible:ring-(--color-focus) focus-visible:ring-offset-1 focus-visible:ring-offset-(--color-paper-2)",                  on ? "text-(--color-paper)" : "text-(--color-neutral) hover:text-(--color-ink)",                )}              >                {hovered === opt.id && !on ? (                  <motion.span                    layoutId="hover"                    aria-hidden                    transition={spring}                    className="absolute inset-0 -z-10 rounded-full bg-(--color-ink)/[0.05]"                  />                ) : null}                {on ? (                  <motion.span                    layoutId="pill"                    aria-hidden                    transition={spring}                    className="absolute inset-0 -z-10 rounded-full bg-(--color-ink) shadow-[inset_0_1px_0_rgba(255,255,255,0.14),0_1px_2px_rgba(0,0,0,0.15)]"                  />                ) : null}                {opt.icon ? <span aria-hidden className="flex size-3.5 [&_svg]:size-full [&_svg]:stroke-[1.75]">{opt.icon}</span> : null}                {opt.label}                {opt.count !== undefined ? <Count value={opt.count} on={on} /> : null}              </button>            );          })}        </div>      </LayoutGroup>    );  }
  /* ----------------------------- multi select ---------------------------- */
  return (    <LayoutGroup id={uid}>      <div role="group" aria-label={ariaLabel} className={cn("flex flex-wrap items-center gap-2", className)}>        {options.map((opt) => {          const on = selected.includes(opt.id);          return (            <motion.button              key={opt.id}              type="button"              aria-pressed={on}              onClick={() => toggle(opt.id)}              layout={reduce ? false : true}              transition={spring}              whileTap={reduce ? undefined : { scale: 0.95 }}              style={{ borderRadius: 999 }}              className={cn(                "relative inline-flex h-9 items-center overflow-hidden pr-3.5 text-[13px] font-medium whitespace-nowrap outline-none",                "transition-[background-color,color,box-shadow] duration-200",                "focus-visible:ring-2 focus-visible:ring-(--color-focus) focus-visible:ring-offset-2 focus-visible:ring-offset-(--color-paper)",                on                  ? "bg-(--color-ink) pl-2.5 text-(--color-paper) shadow-[inset_0_1px_0_rgba(255,255,255,0.14),0_1px_2px_rgba(0,0,0,0.15)]"                  : "bg-(--color-paper) pl-3.5 text-(--color-ink-2) shadow-[inset_0_0_0_1px_var(--color-rule)] hover:bg-(--color-paper-2) hover:text-(--color-ink)",              )}            >              <motion.span layout="position" transition={spring} className="inline-flex items-center gap-1.5">                <AnimatePresence initial={false}>                  {on ? (                    <motion.svg                      key="check"                      viewBox="0 0 16 16"                      aria-hidden                      fill="none"                      stroke="currentColor"                      strokeWidth="2.4"                      strokeLinecap="round"                      strokeLinejoin="round"                      initial={{ width: 0, opacity: 0, marginRight: -6 }}                      animate={{ width: 14, opacity: 1, marginRight: 0 }}                      exit={{ width: 0, opacity: 0, marginRight: -6 }}                      transition={spring}                      className="h-3.5 shrink-0"                    >                      <motion.path                        d="M3.5 8.5l3 3 6-7"                        initial={{ pathLength: reduce ? 1 : 0 }}                        animate={{ pathLength: 1 }}                        transition={{ duration: 0.28, delay: 0.06 }}                      />                    </motion.svg>                  ) : null}                </AnimatePresence>                {opt.icon ? <span aria-hidden className="flex size-3.5 [&_svg]:size-full [&_svg]:stroke-[1.75]">{opt.icon}</span> : null}                <span>{opt.label}</span>                {opt.count !== undefined ? <Count value={opt.count} on={on} /> : null}              </motion.span>            </motion.button>          );        })}
        <AnimatePresence initial={false} mode="popLayout">          {clearable && selected.length > 0 ? (            <motion.button              key="clear"              type="button"              layout={reduce ? false : "position"}              onClick={() => setSelected([])}              initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.8, filter: "blur(4px)" }}              animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}              exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.8, filter: "blur(4px)" }}              transition={spring}              aria-label={`Clear ${selected.length} ${selected.length === 1 ? "filter" : "filters"}`}              className={cn(                "inline-flex h-9 items-center gap-1 rounded-full px-3 text-[13px] font-medium text-(--color-neutral) outline-none",                "transition-colors duration-150 hover:bg-(--color-paper-2) hover:text-(--color-ink) active:scale-[0.96]",                "focus-visible:ring-2 focus-visible:ring-(--color-focus)",              )}            >              <X className="size-3.5" strokeWidth={2} aria-hidden />              Clear            </motion.button>          ) : null}        </AnimatePresence>      </div>    </LayoutGroup>  );}

Adjust import paths according to your folder structure.


Basic Usage

tsx
import { FilterBar } from "@/components/ui/filter-bar";
// Multi-select with counts<FilterBar  aria-label="Ticket status"  options={[    { id: "open", label: "Open", count: 12 },    { id: "review", label: "In review", count: 4 },    { id: "shipped", label: "Shipped", count: 31 },  ]}  value={status}  onChange={setStatus}/>
// Single-select<FilterBar  multi={false}  aria-label="Owner"  options={[{ id: "all", label: "Everyone" }, { id: "mine", label: "Assigned to me" }]}  value={owner}  onChange={setOwner}/>

Props

PropTypeRequiredDefaultDescription
optionsFilterBarOption[]Yes—Options with id, label, and optional count and icon.
value / defaultValuestring[]No—Controlled or initial selected ids. Multi-select keeps them in the options' order.
onChange(value: string[]) => voidNo—Fires with the new selection.
multibooleanNotrueMulti-select chips, or a single sliding pill. Clicking the selected pill clears it.
clearablebooleanNotrueShow a Clear chip while anything is selected (multi-select only).
aria-label / classNamestringNo—Accessible name of the group, and classes for the wrapper.

Best Practices

  1. Show counts so people can see what each filter will leave before they pick it.
  2. Each chip is a toggle button with aria-pressed, so screen readers hear the state.
  3. Use single-select for mutually exclusive scopes like owner or time range; use the Segmented Control when it should never be empty.
  4. Animate the filtered results too (the demo uses AnimatePresence with popLayout), so the change is easy to follow.