A color field allows users to edit a hex color or individual color channel value.
Value
Use the value or defaultValue prop to set the color value, and onChange to handle user input. The value may be a string or Color object, parsed using the parseColor function. onChange is always called with a Color object.
import {ColorField, type Color} from '@react-spectrum/s2/ColorField';
import {ColorSwatch} from '@react-spectrum/s2/ColorSwatch';
import {useState} from 'react';
import {parseColor} from '@react-stately/color';
function Example() {
let [value, setValue] = useState<Color | null>(parseColor('#e73623'));
return (
<ColorField
label="Primary color"
placeholder="Enter a color"
value={value}
onChange={setValue}
prefix={<ColorSwatch size="XS" color={value ? value.toString('hex') : undefined} />} />
);
}
Channel
By default, ColorField displays a hex value. Set the colorSpace and channel props to display a specific color channel.
Current value: #7F007F
import {ColorField, parseColor, type Color} from '@react-spectrum/s2/ColorField';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import {useState} from 'react';
function Example() {
let [color, setColor] = useState<Color | null>(parseColor('#7f007f'));
return (
<div className={style({ display: 'flex', flexDirection: 'column', gap: 8 })}>
<ColorField
label="Hue"
placeholder="Select a hue"
value={color}
onChange={setColor}
colorSpace="hsl"
channel="hue" />
<ColorField
label="Saturation"
placeholder="Select a saturation"
value={color}
onChange={setColor}
colorSpace="hsl"
channel="saturation" />
<ColorField
label="Lightness"
placeholder="Select a lightness"
value={color}
onChange={setColor}
colorSpace="hsl"
channel="lightness" />
<pre className={style({font: 'body'})}>Current value: {color?.toString('hex')}</pre>
</div>
);
}
Forms
Use the name prop to submit the text value to the server. Set the isRequired prop to validate the value, or implement custom client or server-side validation. See the Forms guide to learn more.
API
| Name | Type | Default |
|---|---|---|
size | 'S'
|Β 'M'
|Β 'L'
|Β 'XL' | Default: 'M'
|
The size of the color field. | ||
prefix | ReactNode | Default: β |
The prefix to display in the ColorField. A non-interactive element that appears before the input. | ||
channel | ColorChannel | Default: β |
The color channel that this field edits. If not provided, the color is edited as a hex value. | ||
colorSpace | ColorSpace | Default: β |
The color space that the color field operates in if a | ||
isWheelDisabled | boolean | Default: β |
Enables or disables changing the value with scroll. | ||
isDisabled | boolean | Default: β |
Whether the input is disabled. | ||
isReadOnly | boolean | Default: β |
Whether the input can be selected but not changed by the user. | ||
styles | StylesProp | Default: β |
Spectrum-defined styles, returned by the | ||
placeholder | string | Default: β |
Temporary text that occupies the text input when it is empty. See MDN. | ||
value | T | Default: β |
The current value (controlled). | ||
defaultValue | T | Default: β |
The default value (uncontrolled). | ||
onChange | | Default: β |
Handler that is called when the value changes. | ||
label | ReactNode | Default: β |
The content to display as the label. | ||
labelPosition | LabelPosition | Default: 'top'
|
The label's overall position relative to the element it is labeling. | ||
labelAlign | Alignment | Default: 'start'
|
The label's horizontal alignment relative to the element it is labeling. | ||
contextualHelp | ReactNode | Default: β |
A ContextualHelp element to place next to the label. | ||
isRequired | boolean | Default: β |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | Default: β |
Whether the input value is invalid. | ||
validate | | Default: β |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' |Β 'aria' | Default: 'native'
|
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
necessityIndicator | NecessityIndicator | Default: 'icon'
|
Whether the required state should be shown as an icon or text. | ||
description | ReactNode | Default: β |
A description for the field. Provides a hint such as specific requirements for what to choose. | ||
errorMessage | ReactNode |Β | Default: β |
An error message for the field. | ||
onFocus | | Default: β |
Handler that is called when the element receives focus. | ||
onBlur | | Default: β |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: β |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: β |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: β |
Handler that is called when a key is released. | ||
onCopy | ClipboardEventHandler | Default: β |
Handler that is called when the user copies text. See MDN. | ||
onCut | ClipboardEventHandler | Default: β |
Handler that is called when the user cuts text. See MDN. | ||
onPaste | ClipboardEventHandler | Default: β |
Handler that is called when the user pastes text. See MDN. | ||
onCompositionStart | CompositionEventHandler | Default: β |
Handler that is called when a text composition system starts a new text composition session. See MDN. | ||
onCompositionEnd | CompositionEventHandler | Default: β |
Handler that is called when a text composition system completes or cancels the current text composition session. See MDN. | ||
onCompositionUpdate | CompositionEventHandler | Default: β |
Handler that is called when a new character is received in the current text composition session. See MDN. | ||
onSelect | ReactEventHandler | Default: β |
Handler that is called when text in the input is selected. See MDN. | ||
onBeforeInput | FormEventHandler | Default: β |
Handler that is called when the input value is about to be modified. See MDN. | ||
onInput | FormEventHandler | Default: β |
Handler that is called when the input value is modified. See MDN. | ||
name | string | Default: β |
The name of the input element, used when submitting an HTML form. See MDN. | ||
form | string | Default: β |
The | ||
autoFocus | boolean | Default: β |
Whether the element should receive focus on render. | ||
id | string | Default: β |
The element's unique identifier. See MDN. | ||
excludeFromTabOrder | boolean | Default: β |
Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. | ||
aria-label | string | Default: β |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: β |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: β |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: β |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
aria-errormessage | string | Default: β |
Identifies the element that provides an error message for the object. | ||
UNSAFE_className | UnsafeClassName | Default: β |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: β |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string |Β null | Default: β |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||