Color Field
Select and input colors in Sveltia CMS with an interactive color picker.
The Color field type allows users to select and input colors using a color picker interface.
User Interface
Editor
The browser’s native color picker.
If the enableAlpha option is set to true, a slider for selecting the alpha (transparency) channel will also be displayed.
If the allowInput option is set to true, users can manually enter color values as text.
Preview
A small color swatch showing the selected color, along with its RGB(A) hex value and rgb() function notation.
Data Type
A string representing the color in RGB format, e.g. #RRGGBB. The value is stored in uppercase letters.
If the enableAlpha option is set to true, the color will be stored in RGBA format, e.g. #RRGGBBAA.
If the required option is set to false and the field is left empty, the value will be an empty string.
Data Validation
- If the
requiredoption is set totrue, the color value must not be an empty string. - The color value must be a valid hex color code, either in RGB (
#RRGGBB) or RGBA (#RRGGBBAA) format, depending on theenableAlphaoption. - If the
patternoption is provided, the color value must match the specified regular expression pattern.
Options
In addition to the common field options, the Color field supports the following options:
Required Options
widget
- Type:
string - Default:
string
Must be set to color.
Optional Options
default
- Type:
string - Default:
""(empty string)
The default color value for the field, e.g. #FF0000 for red. If enableAlpha is true, the default value should include the alpha channel, e.g. #FF0000FF for opaque red.
enableAlpha
- Type:
boolean - Default:
false
If set to true, the color picker will allow selection of the alpha (transparency) channel, and the color value will be stored in RGBA format.
allowInput
- Type:
boolean - Default:
false
If set to true, users can manually enter color values as text in addition to using the color picker.
Examples
Basic Color Field
This example shows a simple Color field configuration.
- name: background_color
label: Background Color
widget: color[[fields]]
name = "background_color"
label = "Background Color"
widget = "color"{
"name": "background_color",
"label": "Background Color",
"widget": "color"
}{
name: "background_color",
label: "Background Color",
widget: "color",
}Output example:
background_color: '#FFFFFF'background_color = "#FFFFFF"{
"background_color": "#FFFFFF"
}Color Field with Alpha and Input Allowed
This example shows a Color field configuration with alpha channel enabled, manual input allowed and a default value set.
- name: overlay_color
label: Overlay Color
widget: color
enableAlpha: true
allowInput: true
default: '#00000080'[[fields]]
name = "overlay_color"
label = "Overlay Color"
widget = "color"
enableAlpha = true
allowInput = true
default = "#00000080"{
"name": "overlay_color",
"label": "Overlay Color",
"widget": "color",
"enableAlpha": true,
"allowInput": true,
"default": "#00000080"
}{
name: "overlay_color",
label: "Overlay Color",
widget: "color",
enableAlpha: true,
allowInput: true,
default: "#00000080",
}Output example:
overlay_color: '#00000080'overlay_color = "#00000080"{
"overlay_color": "#00000080"
}