ColorPicker

API reference for the "ColorPicker" control.

Renders a color picker within the Canva UI.

To learn how controls work, refer to Rich controls.

Usage

canva.create('color_picker', {
id: 'colorPickerExample',
label: 'Color Picker Example',
color: '#ff0099',
});

Parameters

Name

Type

Required

Description

controlType

string

Yes

The type of control. To create a color picker, the value must be "color_picker".

opts

object

Yes

Properties for configuring the control.

opts.id

string

Yes

A unique ID for the control.

opts.label

string

No

A human readable label for the control. The default value is "Select a color".

opts.color

string

No

The color to display as selected in the color picker. This value must be a hex code. The default value is "#000000".

Event

Name

Type

Required

Description

opts

object

Yes

Information about the event.

opts.message

object

Yes

Information about the event.

opts.message.commit

boolean

Yes

If false, the user is actively interacting with the control. If true, the user has finished interacting with the control.

opts.message.controlId

string

Yes

The id of the control.

opts.message.controlType

string

Yes

The type of control. The value of this property is always "color_picker".

opts.message.message

object

Yes

Additional information about the event.

opts.message.message.type

string

Yes

The type of event. The value of this property is always "setColor".

opts.message.message.value

string

Yes

The hex code of the selected color.

Example

const canva = window.canva.init();
const state = {
colorPickerExample: '#ff0099',
};
canva.onReady(() => {
renderControls();
});
canva.onControlsEvent(async (opts) => {
if (!opts.message.commit) {
return;
}
if (opts.message.controlType === 'color_picker') {
state[opts.message.controlId] = opts.message.message.value;
}
renderControls();
});
function renderControls() {
const controls = [
canva.create('color_picker', {
id: 'colorPickerExample',
label: 'Color Picker Example',
color: state.colorPickerExample,
}),
];
canva.updateControlPanel(controls);
}