Small rounded filters, links and tags, each removable with its own ×.
- 1.6 kB JS 3.1 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 9 tests
Applied
- Status: Open
- Assignee: Ada Okafor
- Label: Billing
- Priority: Urgent or High
Quick filters
Filters on: 5
This example also uses Color Dot. Install it too:
npx shadcn@latest add checkfu/uicontext/color-dotimport { 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 toggleexport 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}`), ),})When to use
Small rounded filters, links and tags, each removable with its own ×. Use Tag Input instead when people type the tags themselves.
Usage
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
Links and tags
With href a chip is a link; with nothing to do it's a plain tag.
Shop by material
Care
- Machine wash cold
- Line dry
- Do not bleach
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 tagsexport 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
ChipView function
Options
| Name | Type | Description |
|---|---|---|
labelrequired | 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 | — |
ChipListView function
Options
| Name | Type | Description |
|---|---|---|
labelrequired | string | What the chips are: "Active filters", "Collections". |
style | Style | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/chip