---
type: doc
title: Swatch
description: A color or pattern beside an option or filter value.
---

```astro live props={{ name: 'swatch' }}
---
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldGroup, FieldLegend, FieldSet } from "@/components/ui/field"
import { Label } from "@/components/ui/label"
import { Swatch } from "@/components/ui/swatch"

const colors = [
  { value: "navy", label: "Navy", color: "#1e3a5f" },
  { value: "sand", label: "Sand", color: "#d8c7a6" },
  { value: "white", label: "White", color: "#ffffff" },
]
---

<FieldSet class="w-full max-w-xs">
  <FieldLegend variant="label">Color</FieldLegend>
  <FieldGroup class="gap-3">
    {colors.map(({ value, label, color }) => (
      <Field orientation="horizontal">
        <Checkbox id={`color-${value}`} name="color" value={value} />
        <Label for={`color-${value}`}>
          <Swatch color={color} />
          {label}
        </Label>
      </Field>
    ))}
  </FieldGroup>
</FieldSet>
```

## Installation

```bash
npx shadcn@latest add @fulldev/swatch
```

## Usage

```ts
import { Swatch } from "@/components/ui/swatch"
```

```astro
<Swatch color="#1e3a5f" />
<Swatch image="/swatches/denim.webp" />
```

`color` sets the background color and `image` the URL of a pattern or photo,
such as a Shopify swatch. A swatch is decorative, so it renders with
`aria-hidden="true"`: keep the value's name in text, or give the control around
it an `aria-label`. A faint inner ring keeps white and black visible on any
background. Set its size with a `size-*` class.

## API Reference

| Prop    | Type     | Default     |
| ------- | -------- | ----------- |
| `color` | `string` | `undefined` |
| `image` | `string` | `undefined` |

Source: [`src/components/ui/swatch`](https://github.com/fulldotdev/ui/tree/main/src/components/ui/swatch)
