npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-canvas-color-picker

v1.0.21

Published

Simple and customizable canvas color picker built in React + TS

Downloads

83

Readme

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

| Prop | Required | Type | Default | Description | | ----------------- | -------- | ------------------------------------------ | ---------------------------------- | ----------------------------------------------------- | | initialColor | no | RGBA, HSLA, HSVA, HEX, CSS Color | { r: 255, g: 255, b: 255, a: 1 } | Color the color picker will mount with | | formats | no | ColorFormats[] ex: ["rgba", hsla"] | ["rgba"] | Array of color formats sent in callback functions | | spectrum | no | "hsva" or "hsla" | hsva | Specify which color spectrum to use | | spectrumWidth | no | number | 240 | Sets width of color box and sliders | | spectrumHeight | no | number | 240 | Sets height of color box | | sliderHeight | no | number | 14 | Sets height of hue and alpha sliders | | className | no | string | undefined | Additional classes for parent container | | spectrumClasses | no | string | undefined | Additional classes for hsla / hsva spectrum container | | hueClasses | no | string | undefined | Additional classes for hue slider container | | alphaClasses | no | string | undefined | Additional classes for alpha slider container | | handleClasses | no | string | undefined | Additional classes for slider and spectrum handles | | hideAlpha | no | boolean | false | Removes the alpha selector | | onPanStart | no | (event: colorPickerChangeEvent) => void | undefined | Callback function when pan starts | | onPan | no | (event: colorPickerChangeEvent) => void | undefined | Callback function on pan | | onPanEnd | no | (event: colorPickerChangeEvent) => void | undefined | Callback 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