/ Color

A tiny color picker component for modern React apps

A tiny color picker component for modern React apps

react-colorful

react-colorful is a tiny color picker component for modern React apps.

design

Features

  • Small: Just 1,5 KB (minified and gzipped). Size Limit controls the size.
  • Tree-shakeable: Only the parts you use will be imported into your app's bundle.
  • Fast: Built with hooks and functional components only.
  • Bulletproof: Written in strict TypeScript and covered by 30+ tests.
  • Simple: The interface is straight forward and easy to use.
  • Mobile-friendly: Works well on mobile devices and touch screens.
  • No dependencies

Install

npm install react-colorful --save

Usage

import { HexColorPicker } from "react-colorful";
import "react-colorful/dist/index.css";

const YourComponent = () => {
  const [color, setColor] = useState("#aabbcc");
  return <HexColorPicker color={color} onChange={setColor} />;
};

Supported color models

We provide 12 additional color picker components for different color models, unless your app needs a HEX string as an input/output format.

How to use another color model

Available pickers

Import Value example
{ HexColorPicker } "#ffffff"
{ RgbColorPicker } { r: 255, g: 255, b: 255 }
{ RgbaColorPicker } { r: 255, g: 255, b: 255, a: 1 }
{ RgbStringColorPicker } "rgb(255, 255, 255)"
{ RgbaStringColorPicker } "rgba(255, 255, 255, 1)"
{ HslColorPicker } { h: 0, s: 0, l: 100 }
{ HslaColorPicker } { h: 0, s: 0, l: 100, a: 1 }
{ HslStringColorPicker } "hsl(0, 0%, 100%)"
{ HslaStringColorPicker } "hsla(0, 0%, 100%, 1)"
{ HsvColorPicker } { h: 0, s: 0, v: 100 }
{ HsvaColorPicker } { h: 0, s: 0, v: 100, a: 1 }
{ HsvStringColorPicker } "hsv(0, 0%, 100%)"
{ HsvaStringColorPicker } "hsva(0, 0%, 100%, 1)"

Code example

import { RgbColorPicker } from "react-colorful";
import "react-colorful/dist/index.css";

const YourComponent = () => {
  const [color, setColor] = useState({ r: 50, g: 100, b: 150 });
  return <RgbColorPicker color={color} onChange={setColor} />;
};

Live demo →

Overriding styles

The easiest way to tweak react-colorful is to create another stylesheet to override the default styles.

.react-colorful {
  height: 250px;
}
.react-colorful__saturation {
  border-radius: 3px 3px 0 0;
}
.react-colorful__hue {
  height: 30px;
  border-radius: 0 0 3px 3px;
}
.react-colorful__saturation-pointer {
  border-radius: 5px;
}
.react-colorful__hue-pointer {
  border-radius: 2px;
  width: 15px;
  height: inherit;
}

See examples →

How to paste or type a color?

As you probably noticed the color picker itself does not include an input field, but do not worry if you need one. react-colorful is a modular library that allows you to build any picker you need. Since v2.1 we provide an additional component that works perfectly in pair with our color picker.

How to use HexColorInput
import { HexColorPicker, HexColorInput } from "react-colorful";
import "react-colorful/dist/index.css";

const YourComponent = () => {
  const [color, setColor] = useState("#aabbcc");
  return (
    <div>
      <HexColorPicker color={color} onChange={setColor} />
      <HexColorInput color={color} onChange={setColor} />
    </div>
  );
};

Live demo →

HexColorInput does not have any default styles, but accepts all properties that a regular input tag does (such as className, placeholder and autoFocus). That means you can place and modify this component as you like. Also, that allows you to combine the color picker and input in different ways.

TypeScript Support

react-colorful supports TypeScript and ships with types in the library itself; no need for any other install.

How you can get the most from our TypeScript support

While not only typing its own functions and variables, it can also help you type yours. Depending on the component you are using, you can also import the type that is associated with the component. For example, if you are using our HSL color picker component, you can also import the HSL type.

import { HslColorPicker, HslColor } from "react-colorful";

const myHslValue: HslColor = { h: 0, s: 0, l: 0 };

Take a look at Supported Color Models for more information about the types and color formats you may want to use.

Usage with Preact

react-colorful will work flawlessly with Preact out-of-the-box if you are using Preact-CLI, NextJS with Preact, or a few other tools/boilerplates thanks to aliasing.

If you are using another solution, please refer to the Aliasing React to Preact section of the Preact documentation.

Roadmap

  • [x] Additional modules to support RGB, HSL and HSV color models
  • [x] HEX input component
  • [x] TypeScript support
  • [x] Rewrite the codebase to TypeScript
  • [x] Alpha channel support (RGBA, HSLA and HSVA color models)
  • [ ] Accessibility

GitHub

Comments