5.6.1 β€’ Published 2 years ago

react-colorful v5.6.1

Weekly downloads
33,862
License
MIT
Repository
github
Last release
2 years ago

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.

Available pickers

ImportValue 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.

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 β†’

PropertyDefaultDescription
alphafalseAllows #rgba and #rrggbbaa color formats
prefixedfalseEnables # 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.

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.

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:

NameBundle sizeBundle size (gzip)Dependencies
react-colorfulnpm.ionpm.ionpm.io
react-colornpm.ionpm.ionpm.io
react-input-colornpm.ionpm.ionpm.io
rc-color-pickernpm.ionpm.ionpm.io

Projects using react-colorful

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.

@storybook/addon-knobs@univerjs/base-uicarbonio-mails-ui@grafana/ui@optergy/color-picker@ugikie/component-library@ugikie/inputslayoutlab-reactfeanaro_helper_react_tailwind@gourban/ui-components@infinitebrahmanuniverse/nolb-react-colhithlum_tsx@knapsack/toby@abtasty/pulsar-common-uirect-components-007@everything-registry/sub-chunk-2546react-components-007hackhubb-componentsaccessibilik@bitbybit-labs/bbb-componentsnotion-wysiwygdesenhador-pack@arche-mc2/ui-kitant-color-panelgws-components-library@appkit/dek-uiheresta-ui@avsync.live/formationargous3w3m-levaaccessibility-react-widgetDante2@sendoutcards/quantum-design-ui@toridyar/storybook-addon-knobs@showcasejs/cli@shivarajapple/first-library@tryghost/admin-x-settings@shubh799/radix-ui@rphk/constants@skedulr/nebula-ui-library@ucloud-fe/design-token-editor@synerise/ds-color-picker@wearepsh/strapi-plugin-custom-fields@teko-builder/pb-editor@superhuit/wordpress-components@wacoco/y@wordpress/components@tempoplatform/tpds@teravn/onui@webfox/webfox-ui@neuvernetzung/design-system@nextjs-cms/cms@nextjs-cms/ui@nitrogenbuilder/core@nubebytes/ui-angular@nubebytes/ui-react@nouance/payload-better-fields-plugin@openscreen/ui-kit@orca-fe/deye-props-editor@orca-fe/painter@marquee-equity/typify@one-components/core@onemedia/ui@alephdata/react-ftm@akkurateio/formsad-editorgewu-uigewu-uxgewu-ux-extendgeru-componentsgeru-editor@21epub-ui/color-picker@bento-editor/text-format@beachfront/uihight-orchestratorbot-componentsantd-pro-components-lowcode-materialsantd-token-previewerantd-token-previewer-web3insight-design-editor@bitbybit-packages/bbb-ui@buerli.io/plugins@buerli.io/react-cad@brikl/design-dashboard@chaibuilder/sdkcalendar-ui@connect-soft/mui-hook-form@codeleap/webblocksin-systemd4t-ui-demod4t-uidante3mapguide-react-layoutdigitalcnerfviewernuudel-coreopen-react-3d-viewerlevachakra-adminlib-fg-oftas-test@diamondlightsource/davidia
5.6.1

2 years ago

5.6.0

2 years ago

5.5.1

2 years ago

5.5.0

3 years ago

5.3.1

3 years ago

5.4.0

3 years ago

5.3.0

3 years ago

5.2.3

3 years ago

5.2.2

3 years ago

5.2.1

3 years ago

5.1.4

3 years ago

5.1.3

3 years ago

5.2.0

3 years ago

5.1.2

3 years ago

5.1.1

3 years ago

5.1.0

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.4.4

3 years ago

4.4.3

3 years ago

4.4.2

3 years ago

4.4.1

3 years ago

4.4.0

3 years ago

4.2.0

3 years ago

4.3.0

3 years ago

4.1.4

4 years ago

4.1.3

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.7

4 years ago

4.0.6

4 years ago

4.0.5

4 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

3.0.0-beta.1

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.3.0-beta.1

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.2-beta.1

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

2.0.0-beta.2

4 years ago

2.0.0-beta.1

4 years ago

2.0.0-beta.0

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.93

4 years ago

0.0.91

4 years ago

0.0.9

4 years ago