v6.0.35

ColorPicker

Inline color picker
Import

Usage

rgba(47, 119, 150, 0.7)
import { useState } from 'react';
import { ColorPicker, Text, Stack } from '@asuikit/core';
function Demo() {
const [value, onChange] = useState('rgba(47, 119, 150, 0.7)');
return (
<Stack align="center">
<ColorPicker format="rgba" value={value} onChange={onChange} />
<Text>{value}</Text>
</Stack>
);
}

Color format

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

#C5D899
import { ColorPicker } from '@asuikit/core';
function Demo() {
return <ColorPicker />;
}

With swatches

You can add any amount of predefined color swatches:

import { ColorPicker } from '@asuikit/core';
function Demo() {
return (
<ColorPicker
format="hex"
swatches={['#616161', '#9985FF', 'rgba(0, 0, 0, 0.7)', '#2BD695', '#EB5E52', '#5C92FF', '#FFD042', '#FF9966', '#25262b', '#616161', '#e64980', '#be4bdb', '#4c6ef5', '#15aabf', '#12b886', '#82c91e', '#F5F5F5', '#FFFFFF', '#29CC6A', '#FC5555', '#0A0A0A', '#fff']}
/>
);
}

By default, there will be 10 swatches per row, you can change this with swatchesPerRow prop:

import { ColorPicker } from '@asuikit/core';
function Demo() {
return (
<ColorPicker format="hex" swatches={['#616161', '#9985FF', 'rgba(0, 0, 0, 0.7)', '#2BD695', '#EB5E52', '#5C92FF', '#FFD042', '#FF9966', '#25262b', '#616161', '#e64980', '#be4bdb', '#4c6ef5', '#15aabf', '#12b886', '#82c91e', '#F5F5F5', '#FFFFFF', '#29CC6A', '#FC5555', '#0A0A0A', '#fff']} />
);
}

To display swatches without picker set withPicker={false} and fullWidth props:

import { useState } from 'react';
import { DEFAULT_THEME, ColorPicker, Text, Box } from '@asuikit/core';
function Demo() {
const [value, onChange] = useState(null);
return (
<Box maw={200} mx="auto">
<ColorPicker
format="hex"
value={value}
onChange={onChange}
withPicker={false}
fullWidth
swatches={[
...DEFAULT_THEME.colors.red,
...DEFAULT_THEME.colors.green,
...DEFAULT_THEME.colors.blue,
]}
/>
<Text align="center" mt={5}>
{value}
</Text>
</Box>
);
}

Size

Component has 5 predefined sizes: xs, sm, md, lg and xl:

Size
xs
sm
md
lg
xl
import { ColorPicker } from '@asuikit/core';
function Demo() {
return <ColorPicker />;
}

AlphaSlider

You can import and use AlphaSlider and HueSlider components outside of ColorPicker:

Alpha value: 1
Size
xs
sm
md
lg
xl
import { useState } from 'react';
import { AlphaSlider, Text } from '@asuikit/core';
function Demo() {
const [value, onChange] = useState(1);
return (
<>
<Text>Alpha value: {value}</Text>
<AlphaSlider color="#4785FF" value={value} onChange={onChange} />
</>
);
}

HueSlider

Hue value: 250
Size
xs
sm
md
lg
xl
import { useState } from 'react';
import { HueSlider, Text } from '@asuikit/core';
function Demo() {
const [value, onChange] = useState(250);
return (
<>
<Text>Hue value: {value}</Text>
<HueSlider value={value} onChange={onChange} />
</>
);
}

Accessibility and usability

ColorPicker, AlphaSlider and HueSlider components are accessible by default:

  • Saturation, hue and alpha sliders are focusable
  • When user uses mouse to interact with slider, focus is moved on slider
  • All values can be changed with arrows

To make component visible for screen readers provide following props:

<ColorPicker saturationLabel="Saturation" hueLabel="Hue" alphaLabel="Alpha" />

ColorPicker component props

NameTypeDescription
alphaLabel
string
Alpha slider aria-label
defaultValue
string
Uncontrolled component default value
focusable
boolean
Should interactive elements be focusable
format
"hex" | "hexa" | "rgba" | "rgb" | "hsl" | "hsla"
Color format
fullWidth
boolean
Force picker to take 100% width of its container
hueLabel
string
Hue slider aria-label
onChange
(color: string) => void
Called when color changes
onChangeEnd
(color: string) => void
Called when user stops dragging thumb or changes value with arrows
onColorSwatchClick
(color: string) => void
Called when color swatch is clicked
saturationLabel
string
Saturation slider aria-label
size
"xs" | "sm" | "md" | "lg" | "xl"
Predefined component size
swatches
string[]
Predefined colors
swatchesPerRow
number
Number of swatches displayed in one row
value
string
Controlled component value
withPicker
boolean
Set to false to display swatches only

ColorPicker component Styles API

NameStatic selectorDescription
wrapper.asuikit-ColorPicker-wrapperRoot element
body.asuikit-ColorPicker-bodyIncludes hue and alpha sliders and color preview
preview.asuikit-ColorPicker-previewColor preview
sliders.asuikit-ColorPicker-slidersHue and alpha sliders wrapper
slider.asuikit-ColorPicker-sliderAlpha and hue sliders
sliderOverlay.asuikit-ColorPicker-sliderOverlayHue and alpha sliders overlays
thumb.asuikit-ColorPicker-thumbHue, alpha and saturation sliders thumbs
saturation.asuikit-ColorPicker-saturationSaturation slider
swatch.asuikit-ColorPicker-swatchColorSwatch component in swatches list
swatches.asuikit-ColorPicker-swatchesWrapper around all swatches