react-color
repository·master·Indexed 11 days ago
https://github.com/casesandberg/react-colorA collection of 13 different color picker components for React applications, including styles inspired by Sketch, Photoshop, Chrome, GitHub, and Twitter. Version 2.19.3 provides building block components for creating custom pickers and supports various color formats including Hex, RGB, and HSL.
What's inside react-color
- React Color provides 13 different color picker components inspired by popular design tools such as Sketch, Photoshop, and Chrome. Additionally, it provides building block components that allow you to create your own custom color pickers.
Use a color picker component
masterYou can import and use any of the available picker components. The library provides 13 different pickers including
Sketch,Photoshop,Chrome, and more. All components use 100% inline styles via ReactCSS.import React from 'react' import { SketchPicker } from 'react-color' class Component extends React.Component { render() { return <SketchPicker /> } }Create a custom color picker using CustomPicker
masterTo build a custom color picker, you must create a top-level component to act as a bridge and wrap it with the
CustomPickerhigher-order component (HOC) fromreact-color.Your component will receive the following props from the
CustomPickerHOC:hex: A string representing the current color in hex format.rgb: An object representing the current color in RGB format.hsl: An object representing the current color in HSL format.onChange: A callback function that must be invoked to propagate color changes. You can pass a hex string, or anrgborhslobject to this function.
import React from 'react'; import { CustomPicker } from 'react-color'; class MyColorPicker extends React.Component { render() { // Access props like hex, rgb, hsl, and onChange here return <div>MyColorPicker</div>; } } export default CustomPicker(MyColorPicker);Install react-color via npm
masterTo use
react-colorin your project, install the package using npm with the--saveflag to add it to your dependencies.npm install react-color --saveInclude a color picker component
masterTo use a color picker, import the desired picker component from
react-colorand include it within your component's render function.Available picker components include:
AlphaPickerBlockPickerChromePickerCirclePickerCompactPickerGithubPickerHuePickerMaterialPickerPhotoshopPickerSketchPickerSliderPickerSwatchesPickerTwitterPicker
import React from 'react'; import { SketchPicker } from 'react-color'; class Component extends React.Component { render() { return <SketchPicker />; } }Optimize bundle size by importing pickers individually
masterInstead of importing from the main
react-colorentry point, you can import specific pickers directly from their respective files inreact-color/lib/to reduce your final bundle size.import SketchPicker from 'react-color/lib/Sketch'; import ChromePicker from 'react-color/lib/Chrome';Handle color changes with the onChange prop
masterThe
onChangeprop accepts a callback function that is triggered every time the color is modified. This is useful for updating the state of a parent component in real-time.Note on Performance: Because
onChangeis called during drag events, it can fire very frequently. If you only need to capture the final color after a user has finished interacting with the picker, useonChangeCompleteinstead to avoid unnecessary re-renders or heavy computations.import React from 'react'; import { SwatchesPicker } from 'react-color'; class Component extends React.Component { handleChange(color, event) { // The color object contains: // hex: string // rgb: { r, g, b, a } // hsl: { h, s, l, a } } render() { return <SwatchesPicker onChange={ this.handleChange } />; } }Customize picker styles using the `styles` prop
masterYou can override the default inline styles of any color picker component by passing a
stylesobject to thestylesprop.The
stylesobject follows a specific hierarchy:- A top-level key (e.g.,
default) which allows you to target specific themes or states. - A nested key representing the specific part of the picker you wish to style (e.g.,
picker). You should refer to the individual picker's source code to identify the exact keys available for styling. - Standard CSS property names as keys.
Example of overriding the
boxShadowon aSketchPicker:import React from 'react' import { SketchPicker } from 'react-color' const sketchPickerStyles = { default: { picker: { boxShadow: 'none', }, }, } export default class Component extends React.Component { render() { return ( <SketchPicker styles={sketchPickerStyles} /> ) } }import React from 'react' import { SketchPicker } from 'react-color' const sketchPickerStyles = { default: { picker: { boxShadow: 'none', }, }, } export default class Component extends React.Component { render() { return ( <SketchPicker styles={sketchPickerStyles} /> ) } }- A top-level key (e.g.,
Configure the active color in a color picker
masterThe
colorprop controls the currently active color in a color picker component (such asSketchPicker). Use this prop to initialize the picker with a specific color or to keep the picker in sync with a parent component's state.Supported color formats for the
colorprop:- Hex string: e.g.,
'#333'or'#fff'. - RGB object: e.g.,
{ r: 51, g: 51, b: 51 }. Supports an alpha channel witha: 1. - HSL object: e.g.,
{ h: 0, s: 0, l: .10 }. Supports an alpha channel witha: 1. - Transparent: The string
'transparent'.
import React from 'react'; import { SketchPicker } from 'react-color'; class Component extends React.Component { state = { background: '#fff', }; handleChangeComplete = (color) => { this.setState({ background: color.hex }); }; render() { return ( <SketchPicker color={ this.state.background } onChangeComplete={ this.handleChangeComplete } /> ); } }- Hex string: e.g.,
Use onChangeComplete to handle finished color changes
masterThe
onChangeCompleteprop accepts a callback function that is triggered only once the user has finished interacting with the color picker (e.g., after they stop dragging a slider or release the mouse). This is useful for performing expensive operations, such as API calls or heavy state updates, that should only happen after the selection is finalized rather than on every incremental change.The callback receives two arguments:
color: An object containing the selected color properties (e.g.,hex,rgb,hsl).event: The original browser event that triggered the completion.
import React from 'react'; import { PhotoshopPicker } from 'react-color'; class Component extends React.Component { state = { background: '#fff', }; handleChangeComplete = (color, event) => { this.setState({ background: color.hex }); }; render() { return <PhotoshopPicker onChangeComplete={ this.handleChangeComplete } />; } }Available picker components
masterThe following picker components are available for import from
react-color:AlphaPickerBlockPickerChromePickerCirclePickerCompactPickerGithubPickerHuePickerMaterialPickerPhotoshopPickerSketchPickerSliderPickerSwatchesPickerTwitterPicker
The structure of the color object in onChange
masterWhen the
onChangecallback is invoked, it receives acolorobject containing the current color values in several formats. The object structure is as follows:{ hex: '#333', rgb: { r: 51, g: 51, b: 51, a: 1, }, hsl: { h: 0, s: 0, l: .20, a: 1, } }