openColorSelector
Opens a color selector.
Usage
Open a color selector
import { type Anchor, openColorSelector } from "@canva/asset";const anchor: Anchor = {width: 200,height: 40,top: 100,left: 100};await openColorSelector(anchor, {scopes: ["solid"],onColorSelect: (event) => {if (event.selection.type === "solid") {const color = event.selection.hexString;}}});
Open a color selector with a pre-selected color
import { type Anchor, type SolidColor, openColorSelector } from "@canva/asset";const anchor: Anchor = {width: 200,height: 40,top: 100,left: 100};const preselectedColor: SolidColor = {type: "solid",hexString: "#ff0000"};await openColorSelector(anchor, {scopes: ["solid"],selectedColor: preselectedColor,onColorSelect: (event) => {if (event.selection.type === "solid") {const color = event.selection.hexString;}}});
Close a color selector programmatically
import { type Anchor, openColorSelector } from "@canva/asset";const anchor: Anchor = {width: 200,height: 40,top: 100,left: 100};const close = await openColorSelector(anchor, {scopes: ["solid"],onColorSelect: (event) => {if (event.selection.type === "solid") {const color = event.selection.hexString;}}});// Later: Close the color selectorclose();
Parameters
anchorAnchorA bounding box.
heightnumberThe height of the bounding box, in pixels.
widthnumberThe width of the bounding box, in pixels.
topnumberThe top position of the bounding box, in pixels.
leftnumberThe left position of the bounding box, in pixels.
optionsColorSelectorOpts<Scope>Options for configuring a color selector.
scopesScope[]The supported scopes, used to determine the appearance and behavior of the color selector.
The only valid value is "solid".
onColorSelectfunctionA callback that runs when the user selects a color.
Parameters
eventColorSelectionEvent<Scope>The color selection event.
selectionobjectThe selected color.
The exact properties depends on the configured scope.
typestringThe type of color.
The only valid value is "solid".
hexStringstringThe color as a hexadecimal code.
This is always a 6-digit code that's prefixed with a # symbol.
Example
"#ff0099"
Returns
void
selectedColorobjectThe color to display as selected when the color selector is opened.
This is only required if there are multiple UI components for selecting colors.
For example, if the app renders multiple Swatch components.
If this value is undefined, the most recently selected color will be displayed as selected.
This property only affects Document colors and Default colors.
typestringThe type of color.
The only valid value is "solid".
hexStringstringThe color as a hexadecimal code.
This is always a 6-digit code that's prefixed with a # symbol.
Example
"#ff0099"
Returns
A function that closes the currently open color selector.
Promise<() => void>