ColorInput

Capture color from user with a text input and color picker dropdown.

Import

import { ColorInput } from '@tidbcloud/uikit'

Usage

ColorInput allows users to enter a color value manually or pick from a color picker dropdown.

import { ColorInput } from '@tidbcloud/uikit'
 
function Demo() {
  return <ColorInput label="Pick a color" placeholder="Enter color" />
}

Controlled

import { useState } from 'react'
import { ColorInput } from '@tidbcloud/uikit'
 
function Demo() {
  const [value, setValue] = useState('')
  return <ColorInput value={value} onChange={setValue} />
}

Formats

ColorInput supports hex, hexa, rgb, rgba, hsl and hsla color formats. Slider to change opacity is displayed only for hexa, rgba and hsla formats:

import { ColorInput } from '@tidbcloud/uikit'
 
function Demo() {
  return <ColorInput format="rgba" label="RGBA format" />
}

With swatches

You can add predefined color swatches:

import { ColorInput } from '@tidbcloud/uikit'
 
function Demo() {
  return (
    <ColorInput
      format="hex"
      label="Pick color"
      swatches={[
        '#25262b',
        '#868e96',
        '#fa5252',
        '#e64980',
        '#be4bdb',
        '#7950f2',
        '#4c6ef5',
        '#228be6',
        '#15aabf',
        '#12b886',
        '#40c057',
        '#82c91e',
        '#fab005',
        '#fd7e14'
      ]}
    />
  )
}

Disable free input

Set disallowInput prop to prevent manual input:

import { ColorInput } from '@tidbcloud/uikit'
 
function Demo() {
  return <ColorInput disallowInput label="Click to pick" />
}

Eye dropper

By default, if EyeDropper API is available, an eye dropper icon will be displayed. To disable it:

import { ColorInput } from '@tidbcloud/uikit'
 
function Demo() {
  return <ColorInput withEyeDropper={false} label="Without eye dropper" />
}

Props

PropTypeDefaultDescription
valuestring-Controlled component value
defaultValuestring-Uncontrolled component default value
onChange(value: string) => void-Called when value changes
format’hex’ | ‘hexa’ | ‘rgb’ | ‘rgba’ | ‘hsl’ | ‘hsla''hex’Color format
swatchesstring[]-Predefined color swatches
swatchesPerRownumber7Number of swatches per row
withPickerbooleantrueShow color picker dropdown
withEyeDropperbooleantrueShow eye dropper button
disallowInputbooleanfalseDisable manual input
closeOnColorSwatchClickbooleanfalseClose dropdown on swatch click
fixOnBlurbooleantrueRevert to last valid value on blur
labelReactNode-Input label
descriptionReactNode-Input description
errorReactNode-Error message
disabledbooleanfalseDisable input
radiusMantineRadius-Border radius
sizeMantineSize’sm’Input size