React Colorful Color Picker
React Colorful is a highly optimized and lightweight color picker component designed for React and Preact applications. Currently stable at version 5.6.1, it provides a variety of color models including HEX, RGBA, HSLA, and HSVA, boasting a minimal bundle size (just 2.8 KB gzipped) and zero runtime dependencies. The library features a modern API leveraging React hooks and functional components, and comes with comprehensive TypeScript support. It prioritizes accessibility, adhering to WAI-ARIA guidelines, and offers full keyboard navigation and touch screen compatibility. A key differentiator is its significant performance and size advantage over alternatives like `react-color`, coupled with a tree-shakable architecture. Since v5, it has eliminated the need for external CSS imports, streamlining integration into any CSS-in-JS or component library environment. Releases are driven by feature enhancements and bug fixes, ensuring continuous improvement and stability.
Common errors
-
TypeError: (0 , react_colorful__WEBPACK_IMPORTED_MODULE_0__.HexColorPicker) is not a function
cause Attempting to use CommonJS `require()` syntax or an incorrect named import for a library that primarily uses ES Modules and named exports (since v3.0).fixUse ES Module named imports: `import { HexColorPicker } from 'react-colorful';` -
Module not found: Error: Can't resolve 'react-colorful/dist/index.css'
cause Attempting to import the CSS file after v5.0, when the library removed the separate CSS export.fixRemove the line `import 'react-colorful/dist/index.css';` from your project. The component's styles are now embedded. -
TypeError: Cannot read properties of undefined (reading 'r') OR Expected color to be a string, got object.
cause Passing an incorrect color value format to a specific color picker component. For instance, passing an `{r, g, b}` object to `HexColorPicker` which expects a string, or a HEX string to `RgbColorPicker` which expects an object.fixEnsure the `color` prop passed to each picker component matches its expected format. `HexColorPicker` expects `"#rrggbb"` or `"#rrggbbaa"` string. `RgbColorPicker` expects `{ r: number, g: number, b: number }` object. Check the 'Supported Color Models' section in the documentation for each picker's specific input/output format.
Warnings
- breaking Starting with v5, `react-colorful` no longer requires or supports a separate CSS import. The styles are now embedded within the component, simplifying usage and making it compatible with CSS-in-JS solutions. Attempting to import `react-colorful/dist/index.css` will result in a module not found error.
- breaking Version 3.0 migrated the library to named exports and removed the default export. Direct `require('react-colorful')` or `import ColorPicker from 'react-colorful'` will no longer work for accessing components.
- gotcha With the removal of external CSS in v5, default styles are set to a higher priority. If you had custom styles overriding the previous external CSS, you might need to adjust your specificity or use more targeted selectors to ensure your styles apply correctly.
- gotcha Since v5.2, the input color parsers have been significantly improved to support a wider range of valid CSS color notations and units (e.g., percentage syntax for RGB, various angular units for HSL, space/slash separated syntax). While not strictly a breaking change, ensure your color input strings conform to standard CSS formats for optimal parsing.
Install
-
npm install react-colorful -
yarn add react-colorful -
pnpm add react-colorful
Imports
- HexColorPicker
const HexColorPicker = require('react-colorful');import { HexColorPicker } from 'react-colorful'; - RgbaColorPicker
import RgbaColorPicker from 'react-colorful/rgba';
import { RgbaColorPicker } from 'react-colorful'; - HexAlphaColorPicker
import { HexAlphaColorPicker } from 'react-colorful'; - ColorPickerBaseProps
import type { ColorPickerBaseProps } from 'react-colorful';
Quickstart
import React, { useState } from 'react';
import { HexColorPicker } from 'react-colorful';
const MyColorPickerComponent = () => {
const [color, setColor] = useState('#aabbcc');
return (
<div>
<p>Selected Color: {color}</p>
<HexColorPicker color={color} onChange={setColor} />
<button onClick={() => setColor('#ff0000')}>Set Red</button>
<button onClick={() => setColor('#00ff00')}>Set Green</button>
<button onClick={() => setColor('#0000ff')}>Set Blue</button>
</div>
);
};
export default MyColorPickerComponent;