React Color Palette
react-color-palette is a lightweight, performant React component library providing a comprehensive color picker interface. Currently stable at version 7.3.1, it receives regular maintenance and feature updates, with minor releases typically occurring every few months. Its core strength lies in its flexibility, offering a complete `<ColorPicker />` component for immediate use, alongside individual `<Saturation />`, `<Hue />`, and `<Alpha />` components for creating highly customized color picker layouts. It fully supports TypeScript, shipping with declaration files for all its components and hooks like `useColor`. Key features include robust support for various CSS color formats (HEX, RGB, HSL, named colors) in the `useColor` hook, an `onChangeComplete` callback for handling final color selections, and options to customize input visibility or disable the picker entirely. This makes it a versatile choice for applications requiring intuitive and customizable color selection capabilities within a React environment.
Common errors
-
Module not found: Can't resolve 'react-color-palette/css' in '[your-project-path]'
cause The CSS stylesheet was not imported, or the import path is incorrect.fixAdd `import 'react-color-palette/css';` to your component file or global stylesheet entry point. -
React Hook "useColor" is called in function "MyComponent" that is neither a React function component nor a custom React Hook function.
cause The `useColor` hook is being called in a regular JavaScript function or class component, violating React's Rules of Hooks.fixEnsure `useColor` is only called inside a React functional component or another custom hook. -
TypeError: Cannot read properties of undefined (reading 'hex')
cause This often occurs if the `color` state from `useColor` is not initialized properly, or if you're trying to access its properties (`hex`, `rgb`, etc.) before the component has fully rendered or if the color value is unexpectedly `null` or `undefined`.fixAlways initialize `useColor` with a valid default color value (e.g., `useColor('#000000')`). Ensure components consuming the color object handle potential `null` or `undefined` states if they can occur asynchronously.
Warnings
- gotcha The default styles for `react-color-palette` components are provided via a separate CSS file. Forgetting to import `'react-color-palette/css'` will result in an unstyled or poorly formatted color picker.
- gotcha The `useColor` hook must follow React's Rules of Hooks. It cannot be called inside loops, conditions, or nested functions, and must always be called at the top level of a function component or custom hook.
- gotcha As of v7.2.2, the library replaced `pointer` events with separate `mouse` and `touch` events for broader compatibility. While this was a bug fix, applications that may have relied on specific `pointer` event properties or custom handlers interacting with the picker's internal mechanics might need re-testing.
- gotcha Starting with v7.1.1, the `.css` files are marked as `sideEffect` in the package.json. If you are using a bundler (like Webpack or Rollup) configured for aggressive tree-shaking that does not respect the `sideEffects` flag, the necessary CSS might be inadvertently removed during the build process, leading to unstyled components.
Install
-
npm install react-color-palette -
yarn add react-color-palette -
pnpm add react-color-palette
Imports
- ColorPicker
const { ColorPicker } = require('react-color-palette');import { ColorPicker } from 'react-color-palette'; - useColor
import useColor from 'react-color-palette';
import { useColor } from 'react-color-palette'; - Stylesheet
import { css } from 'react-color-palette';import 'react-color-palette/css';
- IColor
import { IColor } from 'react-color-palette';import { type IColor } from 'react-color-palette'; - Saturation
import { Saturation } from 'react-color-palette';
Quickstart
import React from 'react';
import { ColorPicker, useColor } from 'react-color-palette';
import 'react-color-palette/css';
function MyColorPickerComponent() {
// Initialize the color state using the useColor hook.
// It can accept HEX, RGB, HSL, or named CSS colors.
const [color, setColor] = useColor('#561ecb');
// Optional: Define a callback for when the color selection is finalized (e.g., mouse up).
const handleColorChangeComplete = (selectedColor) => {
console.log('Final color selected:', selectedColor.hex);
// Example: Save to local storage or dispatch an action
localStorage.setItem('lastSelectedColor', selectedColor.hex);
};
return (
<div>
<h1>My Application Color Selector</h1>
<ColorPicker
color={color}
onChange={setColor}
height={250}
width={450}
onChangeComplete={handleColorChangeComplete}
hideInput={['rgb', 'hsv']}
/>
<p>Current HEX Color: {color.hex}</p>
<p>Current RGB Color: {color.rgb.r}, {color.rgb.g}, {color.rgb.b}</p>
</div>
);
}
export default MyColorPickerComponent;