# Chip

> Filter chip that toggles on in ink, alone or as a single-select row.

Docs: https://plumbago.ochotzas.com/components/chip

## Installation

```sh
npx plumbago add chip
```

This copies `components/ui/chip.tsx` into your project, along with `cn`, `scroll-fade`, and installs `@base-ui/react`.

## Usage

```tsx
import { Chip, ChipGroup } from "@/components/ui/chip"

<ChipGroup defaultValue={["Mains"]} aria-label="Course">
  <Chip value="Starters">Starters</Chip>
  <Chip value="Mains">Mains</Chip>
</ChipGroup>
```

## Examples

### Preview

```tsx
import { Chip, ChipGroup } from "@/components/ui/chip"
import { Island, ListRow, ListRowSubtitle, ListRowText, ListRowTitle, ListRowValue } from "@/components/ui/island"
import { useState } from "react"

const courses = ["All", "Starters", "Mains", "Desserts", "Drinks"]

const dishes = [
  { name: "Burrata with peaches", course: "Starters", note: "Basil, olive oil", price: "€12" },
  { name: "Charred aubergine", course: "Starters", note: "Tahini, pomegranate", price: "€11" },
  { name: "Slow-roasted lamb", course: "Mains", note: "Rosemary, white beans", price: "€26" },
  { name: "Mushroom risotto", course: "Mains", note: "Parmesan, thyme", price: "€19" },
  { name: "Fig tart", course: "Desserts", note: "Mascarpone", price: "€9" },
  { name: "Blood orange spritz", course: "Drinks", note: "Aperol, prosecco", price: "€10" },
]

export default function ChipDemo() {
  const [value, setValue] = useState(["Mains"])
  const course = value[0]
  const shown = course === "All" ? dishes : dishes.filter((dish) => dish.course === course)
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <ChipGroup scroll value={value} onValueChange={(next) => setValue(next.length > 0 ? next : value)} aria-label="Course">
        {courses.map((item) => (
          <Chip key={item} value={item}>
            {item}
          </Chip>
        ))}
      </ChipGroup>
      <Island variant="list" key={course}>
        {shown.map((dish, index) => (
          <ListRow
            key={dish.name}
            style={{ transitionDelay: `${index * 30}ms` }}
            className="transition-[opacity,filter,translate,scale,background-color] duration-spring ease-spring starting:translate-y-1.5 starting:scale-98 starting:opacity-0 starting:blur-[8px] motion-reduce:starting:translate-y-0 motion-reduce:starting:scale-100 motion-reduce:starting:blur-none"
          >
            <ListRowText>
              <ListRowTitle>{dish.name}</ListRowTitle>
              <ListRowSubtitle>{dish.note}</ListRowSubtitle>
            </ListRowText>
            <ListRowValue className="tabular-nums">{dish.price}</ListRowValue>
          </ListRow>
        ))}
      </Island>
    </div>
  )
}
```

### Toggles

```tsx
import { Chip } from "@/components/ui/chip"

export default function ChipToggles() {
  return (
    <div className="flex flex-wrap justify-center gap-2">
      <Chip defaultPressed>Vegetarian</Chip>
      <Chip>Spicy</Chip>
      <Chip disabled>Gluten free</Chip>
    </div>
  )
}
```

### With icon

```tsx
import { Chip } from "@/components/ui/chip"
import { Flame, Leaf } from "lucide-react"

export default function ChipWithIcon() {
  return (
    <div className="flex flex-wrap justify-center gap-2">
      <Chip defaultPressed>
        <Leaf strokeWidth={1.75} aria-hidden="true" />
        Vegetarian
      </Chip>
      <Chip>
        <Flame strokeWidth={1.75} aria-hidden="true" />
        Spicy
      </Chip>
    </div>
  )
}
```

### Inside an island

```tsx
import { Chip, ChipGroup } from "@/components/ui/chip"
import { Island } from "@/components/ui/island"

export default function ChipInsideAnIsland() {
  return (
    <Island className="flex w-full max-w-md flex-col gap-3">
      <div className="flex flex-col gap-1">
        <h3 className="text-headline font-semibold text-ink">Dietary needs</h3>
        <p className="text-footnote text-ink-2">Pick all that apply to this table.</p>
      </div>
      <ChipGroup scroll multiple defaultValue={["Vegan", "Nut free"]} aria-label="Dietary needs">
        <Chip value="Vegan">Vegan</Chip>
        <Chip value="Nut free">Nut free</Chip>
        <Chip value="Halal">Halal</Chip>
        <Chip value="Gluten free">Gluten free</Chip>
      </ChipGroup>
    </Island>
  )
}
```

## Built on Base UI

Chip wraps these Base UI parts and keeps their props:

- [Toggle](https://base-ui.com/react/components/toggle)
- [Toggle group](https://base-ui.com/react/components/toggle-group)

## Source

```tsx
import { Toggle } from "@base-ui/react/toggle"
import { ToggleGroup } from "@base-ui/react/toggle-group"
import { cn } from "@/lib/cn"
import { ScrollFade } from "@/components/ui/scroll-fade"
import type { ComponentProps } from "react"

export type ChipProps = Omit<ComponentProps<typeof Toggle>, "className"> & {
  className?: string
}

export function Chip({ className, ...props }: ChipProps) {
  return (
    <Toggle
      data-slot="chip"
      className={cn(
        "relative inline-flex h-8 shrink-0 cursor-pointer items-center gap-1.5 rounded-full bg-fill-secondary px-3.5 text-[15px] font-medium whitespace-nowrap text-ink select-none [-webkit-tap-highlight-color:transparent]",
        "transition-control active:not-data-disabled:scale-press active:not-data-disabled:duration-[90ms]",
        "after:absolute after:inset-x-0 after:-inset-y-1.5 after:content-['']",
        "hover:not-data-pressed:bg-[color-mix(in_srgb,var(--pb-fill-secondary),var(--pb-ink)_7%)]",
        "data-pressed:bg-ink data-pressed:text-on-ink",
        "data-disabled:cursor-default data-disabled:opacity-40",
        "[&_svg]:-ml-0.5 [&_svg]:size-4 [&_svg]:shrink-0 [&_svg]:text-ink-2 [&_svg]:transition-colors [&_svg]:duration-fast data-pressed:[&_svg]:text-on-ink",
        className,
      )}
      {...props}
    />
  )
}

export type ChipGroupProps = Omit<ComponentProps<typeof ToggleGroup>, "className"> & {
  scroll?: boolean
  className?: string
}

export function ChipGroup({ scroll = false, className, ...props }: ChipGroupProps) {
  if (scroll) {
    return (
      <ScrollFade orientation="horizontal" scrollbar={false} className="-my-2">
        <ToggleGroup data-slot="chip-group" className={cn("flex items-center gap-2 px-0.5 py-2", className)} {...props} />
      </ScrollFade>
    )
  }
  return (
    <ToggleGroup
      data-slot="chip-group"
      className={cn(
        "-my-2 flex items-center gap-2 overflow-x-auto py-2 [-ms-overflow-style:none] [scrollbar-width:none] sm:flex-wrap sm:overflow-visible [&::-webkit-scrollbar]:hidden",
        className,
      )}
      {...props}
    />
  )
}
```
