Swatch
<x-wirekit:: shows a value that is a color, a pattern or a short name: the color of a product variant, a fabric, a finish, a size. It draws a circle or a square filled with the color or the picture, or a pill with the name, and it carries the value's name, because a color says nothing to a reader who cannot see it.
To let the reader choose among swatches, use the swatch group: it puts each swatch in a native radio button or checkbox, which posts with a form and binds with wire:model.
Usage
Give the swatch a color and a label. The label is the swatch's name for assistive technology:
Add show-label to show the name as text beside the swatch:
A light color keeps its edge on a light page: every swatch has a border in the color the form fields use for theirs, which stands at 3:1 against the page.
A Pattern Instead of a Color
image fills the swatch with a picture, for a value no single color describes: a fabric, a wood grain, a leather. The picture covers the swatch, and the swatch's name names it:
image takes an http or https address, or a path on your site. An address with any other scheme draws no picture.
A Color Behind the Picture
Give a color with the image, and the color lies behind the picture: it shows through the picture's transparent parts, fills the swatch while the picture loads, and stands alone where the picture does not load. A color close to the picture's own keeps a row of patterns calm while they arrive:
A Name Instead of a Color
shape="pill" draws the value's name as the swatch's text, for a value its name says best, such as a size. A pill is as tall as a swatch of its size and at least as wide, and it is read as its text:
An unavailable pill strikes its name through and reads "unavailable" with it. A color or an image given with a pill is drawn as a dot before the name:
A pill draws its name, so it needs a label. Without one, WireKit reports it in development and draws a circle.
The Colors It Accepts
color takes a hex color of three, four, six or eight digits (#fff, #1e3a8a, #1e3a8a80), rgb(), hsl() or oklch(). The color is written into the swatch's style attribute, so a value in any other form is rejected: WireKit reports it in development, and the swatch draws no fill instead of writing the value into your page. A color that comes from your database is checked the same way as one written in a template.
An Unavailable Value
unavailable draws a line across the swatch and adds "unavailable" to its name, so the state is in the picture and in the words:
Sizes and Shapes
size is sm (24px), md (32px, the default) or lg (40px), the height of a pill as well. shape is circle, the default, square, whose corners take the theme's small radius, or pill:
The defaults come from config('wirekit..
Props
| Prop | Type | Default | Description |
|---|---|---|---|
color |
string|null |
null |
The value's color: a hex color, rgb(), hsl() or oklch(). Any other value is rejected and draws no fill. Given with an image, it lies behind the picture |
image |
string|null |
null |
A picture of the value, as an http or https address or a path. It covers the swatch, over a color given with it |
label |
string|null |
null |
The value's name. It names the swatch for assistive technology, stands beside it with showLabel, or is the text of a pill. Without one the swatch is decorative and hidden from assistive technology |
showLabel |
bool |
false |
Show the name as text beside the swatch. A pill shows it inside already |
unavailable |
bool |
false |
Draw a line across the swatch, or through a pill's name, and read "unavailable" with its name |
size |
string |
md |
sm (24px), md (32px) or lg (40px), the height of a pill |
shape |
string |
circle |
circle, square or pill, which draws the name as text |
scope |
string|null |
null |
Scoped personalization key |
Accessibility
- The name is always there. With
labelthe swatch is an image named by it; withshowLabelthe name is text beside it, and the swatch itself is hidden from assistive technology so the name is read once. A swatch without a label is decorative: give it one unless the value's name already stands next to it. - No state rests on color alone. An unavailable value has a line across it, or through a pill's name, and "unavailable" in its name.
- A pill is read as its text. The name it shows is the name a reader hears, and it is as tall as a swatch of its size, 24px at
sm, and at least as wide, so it is never a smaller target than a circle. - A light color keeps its edge. The border is the form fields' resting border, at 3:1 against the page, so white on white stays visible.
- The color survives a forced-colors theme. It is the content of the swatch, as a photo's colors are, so the fill opts out of the theme's color replacement, while the edge around it takes the theme's colors.
Keyboard Interaction
A swatch is not interactive and takes no focus. To choose among swatches with the keyboard, use the swatch group.
Design Tokens
| Token | Use |
|---|---|
--size-wk-xs / --size-wk-sm / --size-wk-md |
The swatch at sm, md and lg |
--radius-wk-sm |
The corners of a square swatch |
--border-wk-width + --color-wk-border-strong |
The edge that keeps a light color visible on a light page |
--color-wk-text + --color-wk-surface |
The line across an unavailable value, and the halo that keeps it readable on a dark color |
--gap-wk-sm + --text-wk-sm |
The name beside the swatch |
--padding-wk-x-xs / --padding-wk-x-sm / --padding-wk-x-md |
The sides of a pill at sm, md and lg |
--text-wk-xs / --text-wk-sm / --text-wk-md |
The name in a pill at sm, md and lg |
--color-wk-surface + --color-wk-text + --gap-wk-xs |
A pill's ground, its name, and the space after its dot |
Customization
Set the default size and shape of every swatch in config/:
'components' => [
'swatch' => ['size' => 'lg', 'shape' => 'square'],
],