# Chip

> Small rounded filters, links and tags, each removable with its own ×.

Page: https://uicontext.com/docs/components/chip

Group: Primitives.

## When to use

Small rounded filters, links and tags, each removable with its own ×. Use [Tag Input](https://uicontext.com/docs/components/tag-input.md) instead when people type the tags themselves.

## Usage

```ts
import { Chip, ChipList } from "./ui/uicontext/components/Chip.ts"

const Message = UiSchema.TaggedEnum({ ClearedStatus: {}, ToggledRemote: {} })

export const Filters = (h: Html.H<typeof Message.Type>, remote: boolean) =>
	ChipList(h, { label: "Filters" }, [
		Chip(h, { label: "Status: Open", onRemove: Message.ClearedStatus() }),
		Chip(h, { label: "Remote", selected: remote, onPress: Message.ToggledRemote() }),
	])
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Filters

The example the page opens with: Chip in “Filters”.

This example also uses Color Dot. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/color-dot
```

```ts title="src/demo.ts" source="www/src/examples/chip/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Chip, ChipList } from "./ui/uicontext/components/Chip.ts"
import { ColorDot } from "./ui/uicontext/components/ColorDot.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { display: "grid", gap: space.md, inlineSize: "min(100%, 34rem)" },
	group: { display: "grid", gap: space.xs },
	label: { fontSize: font.xs, fontWeight: 500, color: color.muted },
	said: { margin: 0, fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" },
})

const Filter = Schema.Struct({ id: Schema.String, label: Schema.String, color: Schema.optional(Schema.String) })
const allFilters = [
	{ id: "status", label: "Status: Open" },
	{ id: "assignee", label: "Assignee: Ada Okafor" },
	{ id: "label", label: "Label: Billing", color: "#ea580c" },
	{ id: "priority", label: "Priority: Urgent or High" },
]
const quick = ["Unassigned", "Created by me", "Has attachments", "Waiting on customer"]

const Message = UiSchema.TaggedEnum({ Cleared: { id: Schema.String }, ClearedAll: {}, Toggled: { name: Schema.String } })

// an issue list's filters: what's applied, each removable, and quick filters that toggle
export const IssueFilters = Html.component({
	name: "IssueFilters",
	model: Schema.Struct({ filters: Schema.Array(Filter), quick: Schema.Array(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ filters: allFilters, quick: ["Unassigned"] }),
	update: {
		Cleared: ($, { id }) => $.evolve({ filters: (f) => f.filter((x) => x.id !== id) }),
		ClearedAll: ($) => $.evolve({ filters: [] }),
		Toggled: ($, { name }) => $.evolve({ quick: (q) => (q.includes(name) ? q.filter((x) => x !== name) : [...q, name]) }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			h.div.attrs(sx(styles.group)).children(
				h.span.attrs(sx(styles.label)).children("Applied"),
				model.filters.length === 0
					? h.p.attrs(sx(styles.said)).children("No filters. Showing all 1,284 issues.")
					: ChipList(h, { label: "Applied filters" }, [
						...model.filters.map((f) => Chip(h, { label: f.label, ...(f.color === undefined ? {} : { icon: ColorDot(h, { color: f.color }) }), onRemove: Message.Cleared({ id: f.id }) })),
						Chip(h, { label: "Clear all", onPress: Message.ClearedAll() }),
					]),
			),
			h.div.attrs(sx(styles.group)).children(
				h.span.attrs(sx(styles.label)).children("Quick filters"),
				ChipList(h, { label: "Quick filters" }, quick.map((name) => Chip(h, { label: name, selected: model.quick.includes(name), onPress: Message.Toggled({ name }) }))),
			),
			h.p.attrs({ role: "status", ...sx(styles.said) }).children(`Filters on: ${model.filters.length + model.quick.length}`),
		),
})
```

### Links and tags

With `href` a chip is a link; with nothing to do it's a plain tag.

```ts title="src/links.ts" source="www/src/examples/chip/links.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Chip, ChipList } from "./ui/uicontext/components/Chip.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { display: "grid", gap: space.sm, inlineSize: "min(100%, 30rem)" },
	title: { margin: 0, fontSize: font.sm, fontWeight: 600, color: color.fg },
})

const collections = ["Linen", "Merino wool", "Organic cotton", "Waxed canvas", "Recycled down", "Made in Portugal"]

// a shop's related collections, each a link, and a product's read-only tags
export const RelatedCollections = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		h.h3.attrs(sx(styles.title)).children("Shop by material"),
		ChipList(h, { label: "Collections" }, collections.map((c) => Chip(h, { label: c, href: `#${c.toLowerCase().replaceAll(" ", "-")}` }))),
		h.h3.attrs(sx(styles.title)).children("Care"),
		ChipList(h, { label: "Care" }, ["Machine wash cold", "Line dry", "Do not bleach"].map((c) => Chip(h, { label: c }))),
	)
```

## API reference

### Chip (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | — |
| `icon` | `View.Content` | A small icon or a ColorDot before the label. |
| `onPress` | `Message` | Makes the chip a button; with `selected`, a toggle that says whether it's on. |
| `selected` | `boolean` | — |
| `href` | `string` | Makes the chip a link. |
| `onRemove` | `Message` | Adds a × button that removes it, named "Remove \<label>". |
| `removeLabel` | `string` | What the × says, when "Remove \<label>" isn't right: "Clear the status filter". |
| `style` | `Style` | — |

### ChipList (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | What the chips are: "Active filters", "Collections". |
| `style` | `Style` | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/chip
```

With npm: `npx shadcn@latest add checkfu/uicontext/chip`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/chip`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/chip`.

It writes these files:

- `src/ui/uicontext/components/Chip.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Chip.ts](https://github.com/checkfu/uicontext/blob/main/src/Chip.ts)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
