Skip to content

color

color fields provides the end user with a color picker interface. They also validate submitted values using the TinyColor utility. Colors are saved as strings in 8 digit hex code, or #rrggbbaa, format.

Module field definition

javascript
// Configuring the `themeColor` field in a module's `fields.add` subsection:
themeColor: {
  type: 'color',
  label: 'Theme color'
}

Color Field UI

You have the ability to control and customize several parts of the color field's UI, as detailed in the options section below. Screenshot of the Apostrophe color field UI

Settings

Required

PropertyTypeDefaultDescription
labelStringn/aSets the visible label for the field in the UI
typeStringn/aSpecifies the field type (color for this type)

Optional

PropertyTypeDefaultDescription
defStringn/aThe default value for the field
helpStringn/aHelp text for the content editor
htmlHelpStringn/aHelp text with support for HTML markup
ifObject{}Conditions to meet before the field is active. See the guide for details.
requiredIfObject{}Conditions to meet before the field is required. See the guide for details.
hiddenBooleanfalseIf true, the field is hidden
requiredBooleanfalseIf true, the field is mandatory
readOnlyBooleanfalseIf true, prevents the user from editing the field value
optionsObjectn/aAn object containing color picker configuration. See below.

options

Color fields have additional settings configured in an options object:

format

Type: String

The color string format saved to the database. Possible options are:

  • rgb
  • prgb
  • hex6
  • hex3
  • hex8
  • hsl
  • hsv

The default value is hex8.

javascript
backgroundColor: {
  type: 'color',
  label: 'Background color',
  options: {
    format: 'rgb'
  }
}

presetColors

An array of color values, used as swatches.

Type: Array, Boolean

TIP

Passing false will disable the preset colors UI

Default Value:

javascript
[
  '#D0021B', '#F5A623', '#F8E71C', '#8B572A', '#7ED321',
  '#417505', '#BD10E0', '#9013FE', '#4A90E2', '#50E3C2',
  '#B8E986', '#000000', '#4A4A4A', '#9B9B9B', '#FFFFFF'
]

Valid Color Strings:
Mix and match as you like.

FormatExample
hex3#f00
hex6#00ff00
hex8#00ff0055
rgbrgb(201, 76, 76)
rgbargba(0, 0, 255, 1)
hslhsl(89, 43%, 51%)
hslahsla(89, 43%, 51%, 0.6)
CSS Variable--my-primary-color

WARNING

When using CSS Variables as presets, Apostrophe will save the CSS Variable name as a string, regardless of the format option (for example '--my-primary-color').

Usage

javascript
backgroundColor: {
  type: 'color',
  label: 'Background color',
  options: {
    presetColors: ['#ea433a', '#cc9300', '#b327bf', '#66f', '#00bf9a']
  }
}

disableAlpha

Type: Boolean

Control alpha transparency with a range input.

Default value

javascript
false

disableFields

Type: Boolean

Disable the inputs for editing the hex & RGBA value of a color. Good for limiting the user's choices.

Default value

javascript
false

disableSpectrum

Type: Boolean

Disable the full color spectrum UI. Good for limiting the user's choices.

Default value

javascript
false

Use in templates

nunjucks
<button style="background-color: {{ data.piece.themeColor or '#639' }}">
  Enhance
</button>