Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
react-colorful
Advanced tools
π¨ A tiny (2,8 KB) color picker component for React and Preact apps. Fast, well-tested, dependency-free, mobile-friendly and accessible
The react-colorful package is a tiny (approximately 1.5 KB) color picker component for React applications that is fast and without dependencies. It provides a modern, accessible, and extensible color picking solution that can be easily integrated into any React project.
Hex Color Picker
This feature allows users to pick a color using a hex color picker. The component manages the color state and provides a callback for when the color changes.
{"import { HexColorPicker } from 'react-colorful';\n\nfunction App() {\n const [color, setColor] = useState('#aabbcc');\n\n return <HexColorPicker color={color} onChange={setColor} />;\n}"}
RGB Color Picker
This feature provides an RGB color picker where users can select a color based on its red, green, and blue components. The color state is managed by the component and updates are provided through a callback function.
{"import { RgbColorPicker } from 'react-colorful';\n\nfunction App() {\n const [color, setColor] = useState({ r: 170, g: 187, b: 204 });\n\n return <RgbColorPicker color={color} onChange={setColor} />;\n}"}
HSL Color Picker
This feature enables users to pick a color using HSL (Hue, Saturation, Lightness) values. The component allows for the selection of HSL values and provides updates through a change callback.
{"import { HslColorPicker } from 'react-colorful';\n\nfunction App() {\n const [color, setColor] = useState({ h: 210, s: 50, l: 60 });\n\n return <HslColorPicker color={color} onChange={setColor} />;\n}"}
Custom Color Picker
This feature allows for the creation of custom color pickers using the react-colorful library. Users can define their own UI and logic for the color picker while still leveraging the underlying functionality provided by react-colorful.
{"import { CustomPicker } from 'react-colorful';\nimport { useState } from 'react';\n\nconst MyColorPicker = CustomPicker(({ hex, onChange }) => (\n <div>\n <div style={{ backgroundColor: hex, width: '100px', height: '100px' }} />\n <input value={hex} onChange={(e) => onChange(e.target.value)} />\n </div>\n));\n\nfunction App() {\n const [color, setColor] = useState('#aabbcc');\n\n return <MyColorPicker color={color} onChange={setColor} />;\n}"}
React-color is a collection of color pickers for React with a larger size compared to react-colorful. It offers more built-in picker styles and customization options but at the cost of a larger bundle size.
Rc-color-picker is another color picker component for React that provides a simple interface for color selection. It is less lightweight than react-colorful and offers fewer customization options.
Coloreact is a color picker component for React that is similar to react-colorful in terms of size and simplicity. However, it may not be as widely used or as actively maintained as react-colorful.
npm install react-colorful
import { HexColorPicker } from "react-colorful";
const YourComponent = () => {
const [color, setColor] = useState("#aabbcc");
return <HexColorPicker color={color} onChange={setColor} />;
};
We provide 12 additional color picker components for different color models, unless your app needs a HEX string as an input/output format.
Import | Value 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)" |
import { RgbColorPicker } from "react-colorful";
const YourComponent = () => {
const [color, setColor] = useState({ r: 50, g: 100, b: 150 });
return <RgbColorPicker color={color} onChange={setColor} />;
};
The easiest way to tweak react-colorful is to create another stylesheet to override the default styles.
.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;
}
As you probably noticed the color picker itself does not include an input field, but do not worry if you need one. react-colorful is a modular library that allows you to build any picker you need. Since v2.1
we provide an additional component that works perfectly in pair with our color picker.
HexColorInput
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>
);
};
Property | Default | Description |
---|---|---|
alpha | false | Allows #rgba and #rrggbbaa color formats |
prefixed | false | Enables # prefix displaying |
HexColorInput
does not have any default styles, but it also accepts all properties that a regular input
tag does (such as className
, placeholder
and autoFocus
). That means you can place and modify this component as you like. Also, that allows you to combine the color picker and input in different ways:
<HexColorInput color={color} onChange={setColor} placeholder="Type a color" prefixed alpha />
react-colorful supports TypeScript and ships with types in the library itself; no need for any other install.
While not only typing its own functions and variables, it can also help you type yours. Depending on the component you are using, you can also import the type that is associated with the component. For example, if you are using our HSL color picker component, you can also import the HSL
type.
import { HslColorPicker, HslColor } from "react-colorful";
const myHslValue: HslColor = { h: 0, s: 0, l: 0 };
Take a look at Supported Color Models for more information about the types and color formats you may want to use.
react-colorful will work flawlessly with Preact out-of-the-box if you are using WMR, Preact-CLI, NextJS with Preact, or a few other tools/boilerplates thanks to aliasing.
If you are using another solution, please refer to the Aliasing React to Preact section of the Preact documentation.
react-colorful, like all other React + TS projects, can potentially cause issues in a Preact + TS application if you have the @types/react
package installed, either as a direct dependency or a dependency of a dependency. For example, the Preact TS template comes with @types/enzyme
which has @types/react
as a dependency.
To fix this, create a declaration.d.ts
file or add to your existing:
import React from "react";
declare global {
namespace React {
interface ReactElement {
nodeName: any;
attributes: any;
children: any;
}
}
}
This will correct the types and allow you to use react-colorful along with many other React + TS libraries in your Preact + TS application.
It would be an easier task to list all of the browsers and versions that react-colorful does not support! We regularly test against browser versions going all the way back to 2013 and this includes IE11.
react-colorful works out-of-the-box for most browsers, regardless of version, and only requires an Object.assign
polyfill be provided for full IE11 support.
Today each dependency drags more dependencies and increases your projectβs bundle size uncontrollably. But size is very important for everything that intends to work in a browser.
react-colorful is a simple color picker for those who care about their bundle size and client-side performance. It is fast and lightweight because:
To show you the problem that react-colorful is trying to solve, we have performed a simple benchmark (using bundlephobia.com) against popular React color picker libraries:
Name | Bundle size | Bundle size (gzip) | Dependencies |
---|---|---|---|
react-colorful | |||
react-color | |||
react-input-color | |||
rc-color-picker |
Not using React or Preact? Not a problem! Check out the list of react-colorful ports adapted to your favourite framework or technology of choice:
vanilla-colorful β a react-colorful reimplementation in vanilla Custom Elements, generously ported by @web-padavan.
angular-colorful β a react-colorful rewritten for use with the Angular framework, lovingly ported by @fil0157.
If your port is not in the list, reach us out via GitHub issues.
5.6.1
FAQs
π¨ A tiny (2,8 KB) color picker component for React and Preact apps. Fast, well-tested, dependency-free, mobile-friendly and accessible
The npm package react-colorful receives a total of 3,266,437 weekly downloads. As such, react-colorful popularity was classified as popular.
We found that react-colorful demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.