1.0.21 • Published 3 years ago

react-canvas-color-picker v1.0.21

Weekly downloads
438
License
MIT
Repository
github
Last release
3 years ago

Image of Color Picker

React Canvas Color Picker

Simple, fast, customizable canvas based color picker built in React. Supports HSLA, HSVA color spectrums, and hsla, hsva, rgba, hex, and hex8 color formats.

Install

npm

npm install react-canvas-color-picker

yarn

yarn add react-canvas-color-picker

Basic Usage

import React, { useState, useRef, useCallback } from "react";
import { ColorPicker, ColorFormats } from "react-canvas-color-picker";

export default function App() {
  const [color, setColor] = useState({ r: 255, g: 255, b: 255, a: 1 });
  const formats = useRef<ColorFormats[]>(["rgba"]);

  const handleChange = useCallback(({ colors }) => {
    setColor({ ...colors.rgba });
  }, []);

  return (
    <ColorPicker
      spectrum="hsva"
      formats={formats.current}
      initialColor={color}
      onPanStart={handleChange}
      onPan={handleChange}
    />
  );
}

Props

PropRequiredTypeDefaultDescription
initialColornoRGBA, HSLA, HSVA, HEX, CSS Color{ r: 255, g: 255, b: 255, a: 1 }Color the color picker will mount with
formatsnoColorFormats[] ex: ["rgba", hsla"]["rgba"]Array of color formats sent in callback functions
spectrumno"hsva" or "hsla"hsvaSpecify which color spectrum to use
spectrumWidthnonumber240Sets width of color box and sliders
spectrumHeightnonumber240Sets height of color box
sliderHeightnonumber14Sets height of hue and alpha sliders
classNamenostringundefinedAdditional classes for parent container
spectrumClassesnostringundefinedAdditional classes for hsla / hsva spectrum container
hueClassesnostringundefinedAdditional classes for hue slider container
alphaClassesnostringundefinedAdditional classes for alpha slider container
handleClassesnostringundefinedAdditional classes for slider and spectrum handles
hideAlphanobooleanfalseRemoves the alpha selector
onPanStartno(event: colorPickerChangeEvent) => voidundefinedCallback function when pan starts
onPanno(event: colorPickerChangeEvent) => voidundefinedCallback function on pan
onPanEndno(event: colorPickerChangeEvent) => voidundefinedCallback function when pan ends

Change Events

onPanStart, onPan, and onPanEnd all return an object containing the colors specified in the formats prop, and the id of the spectrum or slider that was panned with. Note: make sure to wrap handlers in useCallback.

{
  colors: object;
  canvasId: string;
}

setColor

Sometimes you'll want to set the color of the color picker after it's been mounted (imagine changing a hex input, or clicking a color swatch). For this use case there is a setColor method which can be access by passing a ref to the color picker.

import React, { useRef } from "react";
import { ColorPicker, SetColor } from "react-canvas-color-picker";

export default function App() {
  const colorPickerRef = useRef<SetColor>();

  const setRandomColor = () => {
    const newColor = {
      r: Math.random() * 255,
      g: Math.random() * 255,
      b: Math.random() * 255,
      a: 1
    };
    colorPickerRef.current?.setColor(newColor);
  };

  return (
    <>
      <ColorPicker ref={colorPickerRef} />
      <button onClick={setRandomColor}>Random Color</button>
    </>
  );
}

Advanced Example

See: https://codesandbox.io/s/react-canvas-color-picker-q4heh?file=/src/App.tsx:1280-1429

1.0.21

3 years ago

1.0.19

4 years ago

1.0.20

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.11

4 years ago

1.0.12

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.10

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.3

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago