Skip to content
ProLifetime access for $199, paid once, every future release included
Truffle UI

Chip

FreeNew

A compact token that can be a toggle, carry an icon, and have a remove button with its own name.

Installation

pnpm dlx shadcn@latest add @viewport/chip

First time? Add the @viewport registry to components.json. Setup takes one minute.

Usage

The code behind the preview above.

chip-demo.tsx
"use client";

import { useState } from "react";
import { Building2, MapPin, Tag } from "lucide-react";
import { Chip } from "@/components/viewport/chip";

const all = ["Enterprise", "Mid-market", "Startup", "Agency", "Non-profit"];
const start = [
  { id: "city", label: "City: Stockholm", icon: <MapPin aria-hidden /> },
  { id: "size", label: "Size: 50 to 200", icon: <Building2 aria-hidden /> },
  { id: "tag", label: "Tag: Renewal", icon: <Tag aria-hidden /> },
];

export default function ChipDemo() {
  const [segments, setSegments] = useState(["Enterprise", "Agency"]);
  const [filters, setFilters] = useState(start);

  return (
    <div className="flex w-full max-w-sm flex-col gap-5">
      <div>
        <p id="chip-segments" className="mb-2 text-xs font-medium text-muted-foreground">
          Segments
        </p>
        <div role="group" aria-labelledby="chip-segments" className="flex flex-wrap gap-1.5">
          {all.map((segment) => (
            <Chip key={segment} selected={segments.includes(segment)} onSelectedChange={(on) => setSegments((list) => (on ? [...list, segment] : list.filter((s) => s !== segment)))}>
              {segment}
            </Chip>
          ))}
        </div>
      </div>
      <div>
        <p className="mb-2 text-xs font-medium text-muted-foreground">Active filters</p>
        <div className="flex min-h-7 flex-wrap items-center gap-1.5">
          {filters.map((filter) => (
            <Chip key={filter.id} icon={filter.icon} onRemove={() => setFilters((list) => list.filter((f) => f.id !== filter.id))} removeLabel={`Remove filter ${filter.label}`}>
              {filter.label}
            </Chip>
          ))}
          {filters.length === 0 ? (
            <button type="button" onClick={() => setFilters(start)} className="rounded-sm text-sm text-muted-foreground underline underline-offset-4 outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring">
              Restore filters
            </button>
          ) : null}
        </div>
      </div>
    </div>
  );
}

Props

PropTypeDefault
iconReact.ReactNode

Shown before the label.

—
selectedboolean

Makes the chip a toggle. Pass the current state.

—
onSelectedChange(selected: boolean) => void

Runs when a toggle chip is pressed.

—
onRemove() => void

Adds a remove button.

—
removeLabelstring

The remove button's accessible name. Defaults to "Remove" followed by the label when it is plain text.

—
disabledboolean—

Accessibility

  • A toggle chip is a button with aria-pressed.
  • The remove button is named after the chip it removes.
  • Respects prefers-reduced-motion.