react-colorful

repository·master·Indexed 26 days ago

https://github.com/omgovich/react-colorful

A tiny (3.1 KB), dependency-free, and accessible color picker component for React and Preact apps. It supports multiple color models including HEX, RGB, HSL, and HSV, and is written in strict TypeScript. The library provides various picker components (e.g., HexColorPicker, RgbColorPicker) and a HexColorInput for text entry, along with utility functions for color conversion between formats.

Tokens
3.2K
Snippets
10
Records
25
Agent score
86%

What's inside react-colorful

  1. Overview of react-colorful

    master

    react-colorful is a lightweight, high-performance color picker for React. It is designed for developers who prioritize bundle size and client-side performance.

    Key characteristics:

    • Zero dependencies: Reduces security risks and prevents unexpected bundle size increases.
    • Modern architecture: Built exclusively with React hooks and functional components (no classes or polyfills required).
    • Optimized algorithms: Uses manually optimized color conversion algorithms instead of heavy external color manipulation libraries.
  2. Use TypeScript with react-colorful

    master

    react-colorful ships with built-in types. You can import specific types associated with the color models you are using to improve type safety in your own code.

    Example for HSL:

    import { HslColorPicker, HslColor } from "react-colorful";
    
    const myHslValue: HslColor = { h: 0, s: 0, l: 0 };
  3. Use react-colorful with Preact and TypeScript

    master

    If you are using Preact with TypeScript and encounter type issues (often caused by @types/react being present in your dependency tree), you can resolve this by adding a declaration to a declaration.d.ts file:

    import React from "react";
    
    declare global {
        namespace React {
            interface ReactElement {
                nodeName: any;
                attributes: any;
                children: any;
            }
        }
    }
  4. Customize color picker styles

    master

    You can customize the appearance of the color picker by overriding its default CSS classes in your own stylesheet.

    .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;
    }
  5. Use `onChangeEnd` for expensive operations

    master

    The onChangeEnd callback is triggered only when the user finishes changing a color (e.g., on mouseup, touchend, or keyup). This is ideal for operations like saving to a database or undo/redo functionality to avoid performance issues caused by running logic on every intermediate color change.

    const YourComponent = () => {
      const [color, setColor] = useState("#aabbcc");
      return (
        <HexColorPicker
          color={color}
          onChange={setColor}
          onChangeEnd={(color) => saveToDatabase(color)}
        />
      );
    };
  6. Basic usage of a color picker

    master

    You can use any of the provided color picker components by passing a color state and an onChange handler. Here is an example using the HexColorPicker:

    import { HexColorPicker } from "react-colorful";
    
    const YourComponent = () => {
      const [color, setColor] = useState("#aabbcc");
      return <HexColorPicker color={color} onChange={setColor} />;
    };
  7. Supported props for color pickers

    master

    All color picker components accept the following props:

    PropTypeDefaultDescription
    colorstring or objectThe current color value
    onChange(color) => voidCalled on every color change (during dragging, on each key press)
    onChangeEnd(color) => voidCalled when the user finishes changing a color (mouseup, touchend, or keyup)

    Additionally, all pickers accept all standard div attributes such as className, style, and aria-label.

  8. Use HexColorInput for text input support

    master

    Since the color pickers do not include an input field by default, you can use the HexColorInput component to allow users to type or paste hex colors.

    HexColorInput accepts the following props:

    • alpha (boolean, default: false): Allows #rgba and #rrggbbaa color formats.
    • prefixed (boolean, default: false): Enables # prefix displaying.

    Example usage:

    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>
      );
    };
  9. Find react-colorful ports for other frameworks

    master

    If you are not using React or Preact, you can use community-maintained ports of react-colorful:

    • vanilla-colorful: A reimplementation in vanilla Custom Elements.
    • angular-colorful: A version rewritten for the Angular framework.
  10. Available color picker components

    master

    react-colorful provides various components for different color models. Choose the one that matches your required input/output format:

    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)"