This page as markdown: /color-field.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt

ColorField

Status: stable

A colour swatch that opens a themed picker — saturation/value area, hue and alpha sliders, hex entry, presets, and transparent as a value rather than a separate toggle.

Keywords: color, colour, color picker, swatch, hex, alpha, transparency, eyedropper, property, workspace

Usage

Use ColorField wherever a colour is a property: a canvas background, an effect parameter, a stroke or fill. It is sized to the control ladder so it lines up inside a PropertyRow or PropertyPair. Deliberately not , which opens the OS dialog — that ignores the theme entirely, cannot show a transparency checkerboard, has no alpha channel, hosts no eyedropper or presets, and renders at whatever size the platform picks, so it never aligns in a property grid.

Best practices

Anatomy

Part Required Description
Trigger yes A swatch at --control-xs with a hairline ring, on a checkerboard so a translucent or transparent value reads as such.
Picker yes Saturation/value area over the current hue, a hue slider, an alpha slider when allowAlpha, and a hex field.
Presets no An optional swatch row, and the Transparent entry when allowTransparent.

Props

Name Type Default Description
value string — The current value: "transparent", "#rrggbb", or "#rrggbbaa".
onValueChange (value: string) => void — Called with the new value on every commit, including mid-drag.
allowTransparent boolean false Offers "transparent" as a value inside the picker. Colour → transparent → colour restores the colour you had.
allowAlpha boolean false Enables the alpha slider, and therefore 8-digit hex output. Off by default because it widens the value format.
swatches Array<string | { value: string; label: string }> — Optional preset row. A bare hex is named by its hex; give a preset a label ("Brand red") when the hex says nothing to a listener.
label string "Colour" Accessible name for the trigger; the current value is appended to it.
name string — Submits the colour with a form under this name, through a hidden input: "#rrggbb" (lowercase), "#rrggbbaa" when allowAlpha and translucent, or "transparent" — the same shapes onValueChange emits.
form string — Id of a form elsewhere on the page that the hidden input belongs to.
readOnly boolean false Shows the colour and keeps it focusable, but the picker does not open. The value still submits.
required boolean — Accepted so a Field marked required does not leak the attribute onto the button. A ColorField always holds a value, so there is nothing for it to block.
transparentLabel string "Transparent" The transparent value's name, in the trigger's accessible name and on its toggle.
hexLabel (label: string) => string (label) => ${label} hex Accessible name of the hex input, given label.
eyedropperLabel string "Pick colour from screen" Accessible name of the eyedropper button.

Tokens used

panel, panel-raised, hairline, hairline-strong, ring, popover

Motion

Accessibility

Related

Examples

In a form, with named presets

import { ColorField, Field } from "@sableui/react"

<Field label="Brand colour">
  <ColorField
    name="brand"
    value={brand}
    onValueChange={setBrand}
    swatches={[{ value: "#e11d48", label: "Brand red" }, { value: "#0f172a", label: "Ink" }]}
  />
</Field>

Canvas background

import { ColorField, PropertyRow } from "@sableui/react"

<PropertyRow label="Background">
  <ColorField value={background} onValueChange={setBackground} allowTransparent />
</PropertyRow>

With alpha and a palette

import { ColorField } from "@sableui/react"

<ColorField
  value={tint}
  onValueChange={setTint}
  allowAlpha
  swatches={["#f43f5e", "#facc15", "#22c55e", "#3b82f6", "#a855f7"]}
  label="Tint"
/>

The value format is the contract

import { isTransparent, parseHex } from "@sableui/react"

// ColorField only ever EMITS these three shapes:
//   "transparent" | "#rrggbb" | "#rrggbbaa"
// It accepts #rgb and #rgba on paste but never produces them, so a consumer's
// parser has three cases to handle rather than five.
//
// Alpha is omitted when fully opaque — "#3b82f6", never "#3b82f6ff" — so a
// parser written before alpha support keeps reading opaque colours correctly.
// Only enable allowAlpha once your parser and your persisted format accept the
// 8-digit form, or a document saved by a newer build degrades on an older one.

import { parseHex, isTransparent } from "@sableui/react"

const parsed = isTransparent(value) ? null : parseHex(value) // null = not a colour