Playground

Color picker

Choose a colour from preset swatches, the native colour control, or a hex value.

  • Built on native elements
  • A group with an accessible name; each swatch is a button named by its hex value, with aria-pressed for the current colour.
  • The native colour input and the hex text field each have their own name.
  • The hex field sets aria-invalid and points to an error message with aria-describedby until the value is valid; colour is never the only signal because the hex text is always shown.
  • Styles in one file: 03-form-fields.css

Import

import '@monoline/css';
import {ColorPicker} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add color-picker

Usage

import {useState} from 'react';
import {ColorPicker} from '@monoline/react';

function Controlled() {
  const [c, setC] = useState('#0090ff');
  return <div style={{maxWidth: '280px'}}><ColorPicker aria-label="Brand colour" value={c} onValueChange={setC} /><p>Chosen: {c}</p></div>;
}

<Controlled />

Examples

Presets, picker and hex

Custom swatches

Props

ColorPickerProps

PropTypeDefaultDescription
valuestring–The colour as "#rrggbb".
defaultValuestring'#000000'The initial colour as "#rrggbb" when uncontrolled.
onValueChange((value: string) => void)–Called with the new colour as "#rrggbb" when the user picks a swatch or types a valid hex.
colorsstring[]PRESETSPreset swatches, each a CSS hex colour. Set from this prop, never from the stylesheet.
disabledboolean–Stops the user changing the colour.
aria-labelstring–Accessible name for the group.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[aria-checked="true"]03-form-fields.css
[aria-invalid="true"]03-form-fields.css
[aria-pressed="true"]03-form-fields.css
[aria-pressed="true"]color-picker.css
[data-disabled]color-picker.css

CSS classes

ClassStylesheet
.field03-form-fields.css
.field-hint03-form-fields.css
.input03-form-fields.css
.input-bare03-form-fields.css
.input-end03-form-fields.css
.input-lg03-form-fields.css
.input-sm03-form-fields.css
.input-wrap03-form-fields.css
.muted03-form-fields.css
.seg03-form-fields.css
.seg-opt03-form-fields.css
.select03-form-fields.css
.setting03-form-fields.css
.switch03-form-fields.css
.textarea03-form-fields.css
.color-pickercolor-picker.css
.color-picker-errorcolor-picker.css
.color-picker-hexcolor-picker.css
.color-picker-nativecolor-picker.css
.color-picker-rowcolor-picker.css
.color-picker-swatchcolor-picker.css
.color-picker-swatchescolor-picker.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.field {
  border-color: var(--accent);
}
.field[aria-checked="true"] {
  background: var(--wash);
}

Accessibility

  • A group with an accessible name; each swatch is a button named by its hex value, with aria-pressed for the current colour.
  • The native colour input and the hex text field each have their own name.
  • The hex field sets aria-invalid and points to an error message with aria-describedby until the value is valid; colour is never the only signal because the hex text is always shown.