Skip to content

HySwatch

Usage examples on this page are written for Lit / plain HTML (<hy-swatch>). The same component ships as HySwatch in @whitespaceux/harmony-react (native React 19), with typed wrappers for Angular, Solid, Svelte, and Vue. The API reference below applies to all frameworks.

A single selectable cell inside an hy-swatch-group.

hy-swatch is content-agnostic — it renders a sample (a color fill via the color attribute, or arbitrary default-slot content such as a gradient, image, or a dimension preview) plus a label. It has no knowledge of color math, ramps, or tokens; the parent group owns selection state, ARIA roles, roving tabindex, and keyboard interaction. Selection is shown by the cell's ring (border + background tint), not by color alone.

Label placement

  • caption (default) — label sits below the sample. Serves dimension/size rungs.
  • inset — label sits centered on the fill, with an auto-contrast treatment (mix-blend) so it stays legible on any color. Serves color ramps.

Used on its own, a swatch is presentational. Selection behaviour only exists inside hy-swatch-group, and so does the interactive chrome: the pointer cursor and hover ring appear only on a cell the group has given a role (single / multiple mode). In selection-mode="none" and standalone the cell is display-only — no hover, no selection ring.

Intrinsic size

The sample has a size of its own (--hy-swatch-size, 2.5rem by default; the group sets it per size): a floor it never goes under, which is also its rendered size wherever nothing stretches it. Inside a numeric-columns group it stretches to fill the track. Without the floor a shrink-wrapped swatch — a centered flex item, inline-block, a table cell — collapsed to its borders.

Accessible naming

When a swatch carries only a color (no caption text), it MUST be given an accessible name — selection cannot rely on color alone (WCAG 1.4.1). Provide a label, a caption slot, or aria-label on the host (e.g. label="Brand 500" or aria-label="Sky blue, #4A90E2"). A dev-mode warning fires when a color-only swatch has no name.

Examples

Color cell (inset label)

html
<hy-swatch
  value="500"
  color="#4A90E2"
  label="500"
  label-placement="inset"
  aria-label="Brand 500"
></hy-swatch>

Non-color cell (dimension rung)

html
<hy-swatch value="spacing.spa3" label="12px">
  <div class="rung-preview"></div>
</hy-swatch>

API

Properties

PropertyAttributeTypeDefaultDescription
valuevaluestring''Identity reported to the group on selection. Required when selectable.
colorcolorstring''Convenience fill for the sample square. Any CSS color or var(--hy-…).
labellabelstring''Label text (e.g. a stop number or px value).
labelPlacementlabel-placement'caption' | 'inset''caption'Where the label sits: caption (below) or inset (on the fill).
selectedselectedbooleanfalseWhether this cell is selected. Managed by the group; reflected for styling.
disableddisabledbooleanfalseDisables the cell, removing it from selection and keyboard navigation.
tooltiptooltipstring''Hover/focus tooltip text — e.g. the resolved value (#d4d4d4). When set, the cell is wrapped in an hy-tooltip so the value surfaces on hover without a permanent on-screen readout. Empty = no tooltip. The cell stays the swatch host, so a wrapping hy-swatch-group's grid/selection is unaffected.

Slots

SlotDescription
defaultCustom sample content (gradient, image, dimension preview). Overrides the color fill.
captionRich caption content below the sample. Overrides the label attribute.

CSS Parts

PartDescription
baseThe cell wrapper (carries hover / selected / focus chrome).
sampleThe visual sample square.
labelThe inset label rendered over the sample.
captionThe caption text element below the sample.

CSS Custom Properties

PropertyDescription
--hy-swatch-border-hoverCell border on hover.
--hy-swatch-border-selectedCell border when selected.
--hy-swatch-background-hoverCell background on hover.
--hy-swatch-background-selectedCell background when selected.
--hy-swatch-sample-borderBorder framing the sample square.
--hy-swatch-caption-colorCaption text color.
--hy-swatch-radiusCell and sample corner radius.
--hy-swatch-gapInternal gap and padding.
--hy-swatch-sizeMinimum sample size (default 2.5rem; the group sets it per size).

Built with Lit. Documented with VitePress.