Color picker
Choose a colour from preset swatches, the native colour control, or a hex value.
SourceStyles source@monoline/react 0.5.0Markdown for AI toolsstable
- 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
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | – | The colour as "#rrggbb". |
defaultValue | string | '#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. |
colors | string[] | PRESETS | Preset swatches, each a CSS hex colour. Set from this prop, never from the stylesheet. |
disabled | boolean | – | Stops the user changing the colour. |
aria-label | string | – | Accessible name for the group. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[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
| Class | Stylesheet |
|---|---|
.field | 03-form-fields.css |
.field-hint | 03-form-fields.css |
.input | 03-form-fields.css |
.input-bare | 03-form-fields.css |
.input-end | 03-form-fields.css |
.input-lg | 03-form-fields.css |
.input-sm | 03-form-fields.css |
.input-wrap | 03-form-fields.css |
.muted | 03-form-fields.css |
.seg | 03-form-fields.css |
.seg-opt | 03-form-fields.css |
.select | 03-form-fields.css |
.setting | 03-form-fields.css |
.switch | 03-form-fields.css |
.textarea | 03-form-fields.css |
.color-picker | color-picker.css |
.color-picker-error | color-picker.css |
.color-picker-hex | color-picker.css |
.color-picker-native | color-picker.css |
.color-picker-row | color-picker.css |
.color-picker-swatch | color-picker.css |
.color-picker-swatches | color-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.