A tiny, lightweight color picker component library for React and Preact applications
npx @tessl/cli install tessl/npm-react-colorful@5.6.0React Colorful is a tiny (2.8 KB), lightweight color picker component library for React and Preact applications. It offers a comprehensive collection of color picker components supporting multiple color formats (HEX, RGB, RGBA, HSL, HSLA, HSV, HSVA) along with their string representations. The library is designed with performance and accessibility in mind, featuring zero dependencies, TypeScript support, and WAI-ARIA compliance.
npm install react-colorfulimport { HexColorPicker, RgbColorPicker, HslColorPicker } from "react-colorful";For CommonJS:
const { HexColorPicker, RgbColorPicker, HslColorPicker } = require("react-colorful");Tree-shakeable imports (recommended):
import { HexColorPicker } from "react-colorful";
import { RgbaColorPicker } from "react-colorful";
import { HexColorInput } from "react-colorful";import React, { useState } from "react";
import { HexColorPicker } from "react-colorful";
const ColorPickerApp = () => {
const [color, setColor] = useState("#aabbcc");
return (
<div>
<HexColorPicker color={color} onChange={setColor} />
<p>Selected color: {color}</p>
</div>
);
};React Colorful is built around several key components:
Color pickers for basic color formats without alpha channel support.
/**
* Color picker for hexadecimal color values (6-digit format)
* @param props - ColorPickerBaseProps with string color type
*/
function HexColorPicker(props: Partial<ColorPickerBaseProps<string>>): JSX.Element;
/**
* Color picker for RGB color objects
* @param props - ColorPickerBaseProps with RgbColor type
*/
function RgbColorPicker(props: Partial<ColorPickerBaseProps<RgbColor>>): JSX.Element;
/**
* Color picker for HSL color objects
* @param props - ColorPickerBaseProps with HslColor type
*/
function HslColorPicker(props: Partial<ColorPickerBaseProps<HslColor>>): JSX.Element;
/**
* Color picker for HSV color objects
* @param props - ColorPickerBaseProps with HsvColor type
*/
function HsvColorPicker(props: Partial<ColorPickerBaseProps<HsvColor>>): JSX.Element;Usage Examples:
import { HexColorPicker, RgbColorPicker, HslColorPicker } from "react-colorful";
// Hex color picker
<HexColorPicker color="#ff6b6b" onChange={setHexColor} />
// RGB color picker
<RgbColorPicker
color={{ r: 255, g: 107, b: 107 }}
onChange={setRgbColor}
/>
// HSL color picker
<HslColorPicker
color={{ h: 0, s: 75, l: 71 }}
onChange={setHslColor}
/>Color pickers that include alpha (transparency) channel support.
/**
* Color picker for hexadecimal colors with alpha (8-digit format)
* @param props - ColorPickerBaseProps with string color type
*/
function HexAlphaColorPicker(props: Partial<ColorPickerBaseProps<string>>): JSX.Element;
/**
* Color picker for RGBA color objects
* @param props - ColorPickerBaseProps with RgbaColor type
*/
function RgbaColorPicker(props: Partial<ColorPickerBaseProps<RgbaColor>>): JSX.Element;
/**
* Color picker for HSLA color objects
* @param props - ColorPickerBaseProps with HslaColor type
*/
function HslaColorPicker(props: Partial<ColorPickerBaseProps<HslaColor>>): JSX.Element;
/**
* Color picker for HSVA color objects
* @param props - ColorPickerBaseProps with HsvaColor type
*/
function HsvaColorPicker(props: Partial<ColorPickerBaseProps<HsvaColor>>): JSX.Element;Usage Examples:
import { HexAlphaColorPicker, RgbaColorPicker } from "react-colorful";
// Hex with alpha
<HexAlphaColorPicker color="#ff6b6b80" onChange={setHexAlphaColor} />
// RGBA color picker
<RgbaColorPicker
color={{ r: 255, g: 107, b: 107, a: 0.5 }}
onChange={setRgbaColor}
/>Color pickers that work with CSS string representations of colors.
/**
* Color picker for RGB color strings (e.g., "rgb(255, 107, 107)")
* @param props - ColorPickerBaseProps with string color type
*/
function RgbStringColorPicker(props: Partial<ColorPickerBaseProps<string>>): JSX.Element;
/**
* Color picker for RGBA color strings (e.g., "rgba(255, 107, 107, 0.5)")
* @param props - ColorPickerBaseProps with string color type
*/
function RgbaStringColorPicker(props: Partial<ColorPickerBaseProps<string>>): JSX.Element;
/**
* Color picker for HSL color strings (e.g., "hsl(0, 75%, 71%)")
* @param props - ColorPickerBaseProps with string color type
*/
function HslStringColorPicker(props: Partial<ColorPickerBaseProps<string>>): JSX.Element;
/**
* Color picker for HSLA color strings (e.g., "hsla(0, 75%, 71%, 0.5)")
* @param props - ColorPickerBaseProps with string color type
*/
function HslaStringColorPicker(props: Partial<ColorPickerBaseProps<string>>): JSX.Element;
/**
* Color picker for HSV color strings (e.g., "hsv(0, 58%, 100%)")
* @param props - ColorPickerBaseProps with string color type
*/
function HsvStringColorPicker(props: Partial<ColorPickerBaseProps<string>>): JSX.Element;
/**
* Color picker for HSVA color strings (e.g., "hsva(0, 58%, 100%, 0.5)")
* @param props - ColorPickerBaseProps with string color type
*/
function HsvaStringColorPicker(props: Partial<ColorPickerBaseProps<string>>): JSX.Element;Text input components with validation for color values.
/**
* Text input for hexadecimal color values with validation
* @param props - HexColorInputProps including validation options
*/
function HexColorInput(props: HexColorInputProps): JSX.Element;
interface HexColorInputProps extends ColorInputBaseProps {
/** Current hex color value (optional) */
color?: string;
/** Color change callback (optional) */
onChange?: (newColor: string) => void;
/** Enables "#" prefix displaying */
prefixed?: boolean;
/** Allows #rgba and #rrggbbaa color formats */
alpha?: boolean;
}Usage Examples:
import { HexColorInput } from "react-colorful";
// Basic hex input
<HexColorInput color={color} onChange={setColor} />
// With prefix display
<HexColorInput color={color} onChange={setColor} prefixed />
// With alpha support
<HexColorInput color={color} onChange={setColor} alpha />Utility functions for CSP compliance in applications with strict content security policies.
/**
* Signs style tags with a base64-encoded nonce for CSP compliance
* Must be called before any picker is rendered if not using Webpack for CSP
* @param hash - Base64-encoded nonce string
*/
function setNonce(hash: string): void;Usage Example:
import { setNonce } from "react-colorful";
// Set nonce before rendering any color pickers
setNonce("your-csp-nonce-here");/** RGB color representation with red, green, blue values (0-255) */
interface RgbColor {
r: number;
g: number;
b: number;
}
/** RGBA color representation extending RGB with alpha (0-1) */
interface RgbaColor extends RgbColor {
a: number;
}
/** HSL color representation with hue (0-360), saturation (0-100), lightness (0-100) */
interface HslColor {
h: number;
s: number;
l: number;
}
/** HSLA color representation extending HSL with alpha (0-1) */
interface HslaColor extends HslColor {
a: number;
}
/** HSV color representation with hue (0-360), saturation (0-100), value (0-100) */
interface HsvColor {
h: number;
s: number;
v: number;
}
/** HSVA color representation extending HSV with alpha (0-1) */
interface HsvaColor extends HsvColor {
a: number;
}
/** Union type of all color object types */
type ObjectColor = RgbColor | HslColor | HsvColor | RgbaColor | HslaColor | HsvaColor;
/** Union type of string and object color types */
type AnyColor = string | ObjectColor;/** Base props for color picker components */
interface ColorPickerBaseProps<T extends AnyColor> extends Omit<React.HTMLAttributes<HTMLDivElement>, "color" | "onChange" | "onChangeCapture"> {
/** Current color value */
color: T;
/** Color change callback */
onChange: (newColor: T) => void;
}
/** Base props for color input components */
interface ColorInputBaseProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "onChange" | "value"> {
/** Current color value (optional) */
color?: string;
/** Color change callback (optional) */
onChange?: (newColor: string) => void;
}/** Color model configuration interface (internal use) */
interface ColorModel<T extends AnyColor> {
defaultColor: T;
toHsva: (defaultColor: T) => HsvaColor;
fromHsva: (hsva: HsvaColor) => T;
equal: (first: T, second: T) => boolean;
}"#ffffff" (6-digit hex)"#ffffff88" (8-digit hex with alpha){ r: 255, g: 255, b: 255 }{ r: 255, g: 255, b: 255, a: 1 }"rgb(255, 255, 255)""rgba(255, 255, 255, 1)"{ h: 0, s: 0, l: 100 }{ h: 0, s: 0, l: 100, a: 1 }"hsl(0, 0%, 100%)""hsla(0, 0%, 100%, 1)"{ h: 0, s: 0, v: 100 }{ h: 0, s: 0, v: 100, a: 1 }"hsv(0, 0%, 100%)""hsva(0, 0%, 100%, 1)"