react-color

repository·master·Indexed 11 days ago

https://github.com/casesandberg/react-color

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

Tokens
6.8K
Snippets
20
Records
34
Agent score
89%

What's inside react-color

  1. Use a color picker component

    master

    You 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 />
      }
    }
  2. Create a custom color picker using CustomPicker

    master

    To build a custom color picker, you must create a top-level component to act as a bridge and wrap it with the CustomPicker higher-order component (HOC) from react-color.

    Your component will receive the following props from the CustomPicker HOC:

    • 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 an rgb or hsl object 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);
  3. Include a color picker component

    master

    To use a color picker, import the desired picker component from react-color and include it within your component's render function.

    Available picker components include:

    • AlphaPicker
    • BlockPicker
    • ChromePicker
    • CirclePicker
    • CompactPicker
    • GithubPicker
    • HuePicker
    • MaterialPicker
    • PhotoshopPicker
    • SketchPicker
    • SliderPicker
    • SwatchesPicker
    • TwitterPicker
    import React from 'react';
    import { SketchPicker } from 'react-color';
    
    class Component extends React.Component {
    
      render() {
        return <SketchPicker />;
      }
    }
  4. Handle color changes with the onChange prop

    master

    The onChange prop 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 onChange is 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, use onChangeComplete instead 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 } />;
      }
    }
  5. Customize picker styles using the `styles` prop

    master

    You can override the default inline styles of any color picker component by passing a styles object to the styles prop.

    The styles object follows a specific hierarchy:

    1. A top-level key (e.g., default) which allows you to target specific themes or states.
    2. 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.
    3. Standard CSS property names as keys.

    Example of overriding the boxShadow on a SketchPicker:

    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} />
        )
      }
    }
  6. Configure the active color in a color picker

    master

    The color prop controls the currently active color in a color picker component (such as SketchPicker). 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 color prop:

    • Hex string: e.g., '#333' or '#fff'.
    • RGB object: e.g., { r: 51, g: 51, b: 51 }. Supports an alpha channel with a: 1.
    • HSL object: e.g., { h: 0, s: 0, l: .10 }. Supports an alpha channel with a: 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 }
          />
        );
      }
    }
  7. Use onChangeComplete to handle finished color changes

    master

    The onChangeComplete prop 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:

    1. color: An object containing the selected color properties (e.g., hex, rgb, hsl).
    2. 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 } />;
      }
    }
  8. Available picker components

    master

    The following picker components are available for import from react-color:

    • AlphaPicker
    • BlockPicker
    • ChromePicker
    • CirclePicker
    • CompactPicker
    • GithubPicker
    • HuePicker
    • MaterialPicker
    • PhotoshopPicker
    • SketchPicker
    • SliderPicker
    • SwatchesPicker
    • TwitterPicker