Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-colorful

omgovich16.1mMIT5.6.1TypeScript support: included

🎨 A tiny (2,8 KB) color picker component for React and Preact apps. Fast, well-tested, dependency-free, mobile-friendly and accessible

react, color picker, react-component, colorpicker, tiny, hex, color, front-end, color-picker, accessible, react-color, accessibility, aria, a11y, wai-aria

readme

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

Features

  • 🗜 Small: Just 2,8 KB gzipped (13x lighter than react-color).
  • 🌳 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 has 100% test coverage.
  • 🗂 Typed: Ships with types included
  • 😍 Simple: The interface is straightforward and easy to use.
  • 👫 Cross-browser: Works out-of-the-box for most browsers, regardless of version.
  • 📲 Mobile-friendly: Supports mobile devices and touch screens.
  • 💬 Accessible: Follows the WAI-ARIA guidelines to support users of assistive technologies.
  • 💨 No dependencies

Live demos

Table of Contents

Getting Started

npm install react-colorful
import { HexColorPicker } from "react-colorful";

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.

<summary>How to use another color model</summary>

Available pickers

Import Value example
{ HexColorPicker } "#ffffff"
{ HexAlphaColorPicker } "#ffffff88"
{ 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";

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

Live demo →

Customization

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

.your-component .react-colorful {
  height: 240px;
}
.your-component .react-colorful__saturation {
  border-radius: 4px 4px 0 0;
}
.your-component .react-colorful__hue {
  height: 40px;
  border-radius: 0 0 4px 4px;
}
.your-component .react-colorful__hue-pointer {
  width: 12px;
  height: inherit;
  border-radius: 0;
}

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.

<summary>How to use HexColorInput</summary>
import { HexColorPicker, HexColorInput } from "react-colorful";

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

Live demo →

Property Default Description
alpha false Allows #rgba and #rrggbbaa color formats
prefixed false Enables # prefix displaying

HexColorInput does not have any default styles, but it also 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:

<HexColorInput color={color} onChange={setColor} placeholder="Type a color" prefixed alpha />

Code Recipes

TypeScript Support

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

<summary>How you can get the most from our TypeScript support</summary>

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 WMR, 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.

<summary>Preact + Typescript</summary>

react-colorful, like all other React + TS projects, can potentially cause issues in a Preact + TS application if you have the @types/react package installed, either as a direct dependency or a dependency of a dependency. For example, the Preact TS template comes with @types/enzyme which has @types/react as a dependency.

To fix this, create a declaration.d.ts file or add to your existing:

import React from "react";

declare global {
    namespace React {
        interface ReactElement {
            nodeName: any;
            attributes: any;
            children: any;
        }
    }
}

This will correct the types and allow you to use react-colorful along with many other React + TS libraries in your Preact + TS application.

Browser Support

It would be an easier task to list all of the browsers and versions that react-colorful does not support! We regularly test against browser versions going all the way back to 2013 and this includes IE11.

react-colorful works out-of-the-box for most browsers, regardless of version, and only requires an Object.assign polyfill be provided for full IE11 support.

Why react-colorful?

Today each dependency drags more dependencies and increases your project’s bundle size uncontrollably. But size is very important for everything that intends to work in a browser.

react-colorful is a simple color picker for those who care about their bundle size and client-side performance. It is fast and lightweight because:

  • has no dependencies (no risks in terms of vulnerabilities, no unexpected bundle size changes);
  • built with hooks and functional components only (no classes and polyfills for them);
  • ships only a minimal amount of manually optimized color conversion algorithms (while most of the popular pickers import entire color manipulation libraries that increase the bundle size by more than 10 KB and make your app slower).

To show you the problem that react-colorful is trying to solve, we have performed a simple benchmark (using bundlephobia.com) against popular React color picker libraries:

Name Bundle size Bundle size (gzip) Dependencies
react-colorful
react-color
react-input-color
rc-color-picker

Projects using react-colorful

<summary>Storybook — the most widely used open-source tool for developing UI components</summary> Storybook
<summary>Resume.io — online resume builder with over 9,400,000 users worldwide</summary> resume.io
<summary>Wireflow.co — free tool for creating modern user flow prototypes</summary> wireflow.co
<summary>MagicPattern.design — unique geometric pattern generator</summary> magicpattern.design
<summary>Viewst.com — online tool for designing, creating and automating ad campaigns</summary> viewst.com
<summary>Omatsuri.app — progressive web application with a lot of different frontend focused tools</summary> omatsuri.app
<summary>Leva — open source extensible GUI panel made for React</summary> pmndrs/leva
<summary>Composable — online tool for creating custom vector illustrations</summary> composable.art

Backers and sponsors

Ports

Not using React or Preact? Not a problem! Check out the list of react-colorful ports adapted to your favourite framework or technology of choice:

If your port is not in the list, reach us out via GitHub issues.

changelog

5.6.1

  • Update export settings to fix Jet 28 issues (via #191)

5.6.0

  • Add new HexAlphaColorPicker component (via #186)
  • Fix types export for TYpeScript 4.7. Thanks to @AnotherHermit (via #189)
  • Improve ARIA attribute values for sliders. Thanks to @aitchiss (via #177)

5.5.1

  • Fix embedding into <iframe>. The component is rendered correctly by libraries like react-frame-component. Thanks to @leoc4e (via #166)

5.5.0

  • Multitouch! ✌️ Thanks to @xnimorz (via #158)

5.4.0

  • Add new alpha property for HexColorInput to allow "#rgba" and "#rrggbbaa" formats (via #155)

5.3.1

  • Fix potential memory leaks and improve the performance. Thanks to @xnimorz (via #151)

5.3.0

  • Add new prefixed property for HexColorInput to display "#" prefix (via #146)

5.2.3

  • Fix: Make the picker take focus on click (via #143)

5.2.2

  • Fix "Unable to preventDefault inside passive event listener" (via #141)

5.2.1

  • Fix rounded corner rendering bug (via #140)

5.2.0

  • Improve input color parsers to support more CSS color notations and units (via #133)

5.1.4

  • Fix .mjs file publishing (via #129)

5.1.3

  • Export .mjs file to improve different environments and bundlers support. Thanks to @rschristian (via #127)

5.1.2

  • Add "default" fallback to the exports map (via #124)

5.1.1

  • Fix setNonce type declaration file publishing (via #123)

5.1.0

  • The picker complies with the strict CSP (Content Security Policy). The style tag added by the component uses a nonce hash provided by Webpack or the one defined manually by the new setNonce function (via #121)

5.0.1

  • The picker supports all HTML attributes and DOM events that a regular tag does (e.g. id or onMouseEnter (via #119)

5.0

  • The library is 100% CSS-in-JS now. No need to import the CSS file to render the picker properly (via #101)

4.4.4

  • Fix bug when a user releases the mouse button outside of the document bounds (via #99)

4.4.3

  • Get rid of useLayoutEffect warning when using on the server (via #95)

4.4.2

  • Fix CSS loading in Webpack v5 (via #91)

4.4.1

  • Disable static class names minification (via #86)

4.4.0

  • Migrate from CSS modules to static class names (via #84)
  • Better Skypack support. Thanks to @rschristian (via #83)

4.3.0

  • Better React 17 support (via #80)

4.2.0

  • Round output values (via #77)

4.1.4

  • Fix pointer rendering bug on Safari 14 (via #74)

4.1.3

  • Do not display the default focus styles

4.1.2

  • 100% code coverage

4.1.1

  • Better Internet Explorer 11 support (via #65)

4.1.0

  • The picker follows the WAI-ARIA guidelines to support users of assistive technologies. The component is completely accessible with keyboard navigation: you can focus on any picker's part using the Tab button and change the color with the arrow keys. Made by @omgovich (via #63)

4.0.7

  • Simplify gradient CSS styles

4.0.6

  • Improve pasting from clipboard to HexColorInput

4.0.5

  • Fix: Fast Tap and Release in iOS Safari (via #56)
  • Adding HsvStringColorPicker and HsvaStringColorPicker components. Thanks to @rschristian (via #48)

4.0.4

  • Resolving TouchEvent error on Firefox. Thanks to @rschristian (via #53)

4.0.3

  • Improve Interactive internal typing (via #50)

4.0.2

  • Allow to pass custom onBlur callback to HexInput (via #49)
  • Improve HexColorInput types (via #49)

4.0.1

  • Add alpha picker demos

4.0

  • Alpha channel support (via #47)
  • Additional components to work with RGBA, HSLA, HSVA color models (via #47)

3.0.3

  • Improve TypeScript tooling. Thanks to @rschristian (via #45)

3.0.2

  • Fix sideEffects to keep CSS-files

3.0

  • Migrate to named exports. Thanks to @rschristian (via #42)
  • Mark the library as side-effect-free and add tree-shaking support. Thanks to @rschristian (via #42)
  • More consistent public component and type names. Thanks to @rschristian (via #42)
  • Fix type definitions: make all HexInput props optional
  • Enhance internal event type definitions. Thanks to @byr-gdp (via #41)
  • Escape from "useCallback hell" and improve performance by adding useEventCallback hook. Thanks to @jeetiss (via #40)

2.3.1

  • Extend allowed HexInput props with HTMLInputElement

2.3.0

  • The entire codebase was rewritten in TypeScript by @rschristian (via #23)

2.2.1

  • Fix type definitions: make all of the picker props optional

2.2.0

  • TypeScript types are now bundled with the library. Thanks to @rschristian (via #22)

2.1.2

  • Make the pointer grabbable even if it is outside of the picket bounds (via #21)

2.1.1

  • Fix bug if user taps on the picker and does not move the pointer afterward

2.1.0

  • Add HexInput component that allows to paste and type a HEX color

2.0.3

  • Reduce the number of folders published to NPM
  • Rewrite Interactive to make the bundle lighter

2.0.2

  • Fix HSV to RGB conversion algorithm
  • Rewrite utils to make the bundler lighter

2.0.1

  • Update docs and tooling

2.0

  • Support new input/output formats: RGB object, RGB string, HSL object, HSL string, HSV object

1.2.5

  • Test components with Jest and React Testing Library

1.2.4

  • Fix box-sizing of the pointers

1.2.3

  • Refactor Interactive a bit in order to make the package lighter

1.2.2

  • Get rid of unused className props in Hue and Saturation components

1.2.1

  • Add equalHex and equalColorObjects utils and write tests for them

1.2.0

  • Make the package dependency-free
  • Do not trigger onChange after the mounting

1.1.0

  • Migrate from color-fns to @swiftcarrot/color-fns which is 40% lighter

1.0.1

  • Use proper JSX pragma for React. Thanks to @jeetiss

1.0

  • HEX color picker component