Skip to main content

useColorField

Reads the value of a color field.

function useColorField(props: {field: string; defaultColor?: string; defaultAlpha?: number}): Color

Arguments

PropertyTypeDescription
fieldstringThe name of the field.
defaultColorstringThe hex color (e.g. #ffffff) used while the field is empty.
defaultAlphanumberThe opacity between 0 and 1 used while the field has no opacity. Defaults to 1.

Returns

An instance of the Color class, which is exported by @modbros/dashboard-sdk:

MethodReturnsDescription
isEmpty()booleanWhether the user didn't select a color. Ignores the defaultColor.
getHex()string | nullThe hex color, e.g. #ff0000, falling back to the defaultColor.
getAlpha()numberThe opacity between 0 and 1, falling back to the defaultAlpha.
getHexAlpha()stringThe opacity as hex value.
getHexWithAlpha()string | nullThe hex color including the opacity.
toRgb()Rgba | nullThe color as {r, g, b, a} object. Rgba is exported by @modbros/dashboard-sdk.
toRgbaCss()stringThe color as CSS value, e.g. rgba(255, 0, 0, 1). An empty string if there is no color.
toValueColor()ValueColor | undefinedThe raw value of the field.

Example

src/widgets/example/Example.tsx
import React from 'react'
import {Color, useColorField} from '@modbros/dashboard-sdk'

export default function Example() {
const color: Color = useColorField({field: 'color', defaultColor: '#ffffff'})

return <span style={{color: color.toRgbaCss()}}>I'm colored</span>
}