A tiny color picker custom element for modern web apps (2.7 KB) 🎨
Installations
npm install vanilla-colorful
Score
99.4
Supply Chain
99.6
Quality
76
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
Yes
Node Version
16.14.2
NPM Version
8.5.0
Statistics
800 Stars
206 Commits
29 Forks
8 Watching
3 Branches
4 Contributors
Updated on 27 Nov 2024
Bundle Size
6.17 kB
Minified
2.78 kB
Minified + Gzipped
Languages
TypeScript (89.89%)
JavaScript (5.4%)
HTML (2.57%)
CSS (2.14%)
Total Downloads
Cumulative downloads
Total Downloads
12,158,488
Last day
-0.5%
52,066
Compared to previous day
Last week
4%
268,047
Compared to previous week
Last month
11.3%
1,133,080
Compared to previous month
Last year
435.5%
10,146,803
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
34
Features
- 🗜 Small: Just 2,7 KB (minified and gzipped). Size Limit controls the size.
- 🚀 Fast: Built with standards based Custom Elements.
- 🛡 Bulletproof: Written in strict TypeScript and has 100% test coverage.
- 🗂 Typed: Ships with types included.
- 😍 Simple: The interface is straightforward and easy to use.
- 💬 Accessible: Follows the WAI-ARIA guidelines to support users of assistive technologies.
- 📲 Mobile-friendly: Works well on mobile devices and touch screens.
- 👫 Framework-agnostic: Can be used with any framework.
- 💨 No dependencies
Live demos
Install
npm install vanilla-colorful --save
Or use one of the following content delivery networks:
1<script type="module" src="https://unpkg.com/vanilla-colorful?module"></script>
1<script type="module" src="https://cdn.skypack.dev/vanilla-colorful"></script>
1<script type="module" src="https://jspm.dev/vanilla-colorful"></script>
1<script type="module" src="https://esm.sh/vanilla-colorful"></script>
Usage
1<hex-color-picker color="#1e88e5"></hex-color-picker> 2<script type="module"> 3 import 'vanilla-colorful'; 4 5 const picker = document.querySelector('hex-color-picker'); 6 picker.addEventListener('color-changed', (event) => { 7 // get updated color value 8 const newColor = event.detail.value; 9 }); 10 11 // get current color value 12 console.log(picker.color); 13</script>
ES modules
vanilla-colorful is authored using ES modules which are natively supported
by modern browsers. However, all the code examples listed here use so-called "bare module specifiers":
import 'vanilla-colorful'
.
There is now a feature in the HTML Standard called import maps that enables resolving bare module specifiers without requiring any tools. As of October 2022, import maps are not yet shipped in all browsers.
In the meantime, we recommend using one of the tools that leverage ES modules based development, such as
vite
, @web/dev-server
,
or wmr
. None of these tools are needed when importing from CDN.
Supported color models
The default vanilla-colorful's input/output format is a HEX string (like #ffffff
). In case if
you need another color model, we provide 12 additional color picker bundles.
How to use another color model
Available pickers
File to import | HTML element | Value example |
---|---|---|
"hex-color-picker.js" | <hex-color-picker> | "#ffffff" |
"hex-alpha-color-picker.js" | <hex-alpha-color-picker> | "#ffffff88" |
"hsl-color-picker.js" | <hsl-color-picker> | { h: 0, s: 0, l: 100 } |
"hsl-string-color-picker.js" | <hsl-string-color-picker> | "hsl(0, 0%, 100%)" |
"hsla-color-picker.js" | <hsla-color-picker> | { h: 0, s: 0, l: 100, a: 1 } |
"hsla-string-color-picker.js" | <hsla-string-color-picker> | "hsla(0, 0%, 100%, 1)" |
"hsv-color-picker.js" | <hsv-color-picker> | { h: 0, s: 0, v: 100 } |
"hsv-string-color-picker.js" | <hsv-string-color-picker> | "hsv(0, 0%, 100%)" |
"hsva-color-picker.js" | <hsva-color-picker> | { h: 0, s: 0, v: 100, a: 1 } |
"hsva-string-color-picker.js" | <hsva-string-color-picker> | "hsva(0, 0%, 100%, 1)" |
"rgb-color-picker.js" | <rgb-color-picker> | { r: 255, g: 255, b: 255 } |
"rgba-color-picker.js" | <rgba-color-picker> | { r: 255, g: 255, b: 255, a: 1 } |
"rgb-string-color-picker.js" | <rgb-string-color-picker> | "rgb(255, 255, 255)" |
"rgba-string-color-picker.js" | <rgba-string-color-picker> | "rgba(255, 255, 255, 1)" |
Code example
1<rgba-color-picker></rgba-color-picker> 2<script type="module"> 3 import 'vanilla-colorful/rgba-color-picker.js'; 4 5 const picker = document.querySelector('rgba-color-picker'); 6 picker.color = { r: 50, g: 100, b: 150, a: 1 }; 7</script>
Overriding styles
vanilla-colorful exposes CSS Shadow Parts allowing to override the default styles:
1hex-color-picker { 2 height: 250px; 3} 4 5hex-color-picker::part(saturation) { 6 bottom: 30px; 7 border-radius: 3px 3px 0 0; 8} 9 10hex-color-picker::part(hue) { 11 height: 30px; 12 border-radius: 0 0 3px 3px; 13} 14 15hex-color-picker::part(saturation-pointer) { 16 border-radius: 5px; 17} 18 19hex-color-picker::part(hue-pointer) { 20 border-radius: 2px; 21 width: 15px; 22 height: inherit; 23}
HEX input
vanilla-colorful provides an additional <hex-input>
element that can be used to type a color:
1<hex-input color="#1e88e5"></hex-input> 2<script type="module"> 3 import 'vanilla-colorful/hex-input.js'; 4 5 const input = document.querySelector('hex-input'); 6 input.addEventListener('color-changed', (event) => { 7 const newColor = event.detail.value; 8 }); 9</script>
<hex-input>
renders an unstyled <input>
element inside a slot and exposes it for styling using
part
. You can also pass your own <input>
element as a child if you want to fully configure it.
In addition to color
property, <hex-input>
supports the following boolean properties:
Property | Default | Description |
---|---|---|
alpha | false | Allows #rgba and #rrggbbaa color formats |
prefixed | false | Enables # prefix displaying |
Base classes
vanilla-colorful provides a set of base classes that can be imported without registering custom elements. This is useful if you want to create your own color picker with a different tag name.
1import { RgbBase } from 'vanilla-colorful/lib/entrypoints/rgb.js'; 2 3customElements.define('custom-color-picker', class extends RgbBase {});
Code Recipes
- Custom styles and layout
- Prevent flash of unstyled content
- Prevent flash of unstyled content (picker with alpha)
- Text field to be able to type/copy/paste a color
TypeScript support
vanilla-colorful supports TypeScript and ships with types in the library itself; no need for any other install.
How you can get the most from our TypeScript support
Custom types
While not only typing its own class methods and variables, it can also help you type yours. Depending on
the element you are using, you can also import the type that is associated with the element.
For example, if you are using our <hsl-color-picker>
element, you can also import the HslColor
type.
1import type { HslColor } from 'vanilla-colorful/hsl-color-picker'; 2 3const myHslValue: HslColor = { h: 0, s: 0, l: 0 };
Typed events
All the included custom elements provide overrides for addEventListener
and removeEventListener
methods
to include typings for the color-changed
custom event detail
property:
1const picker = document.querySelector('rgba-color-picker');
2
3picker.addEventListener('color-changed', (event) => {
4 console.log(event.detail.value.a); // (property) RgbaColor.a: number
5});
Lit plugin
All the included custom elements are compatible with lit-analyzer and lit-plugin extension for Visual Studio Code, so you can benefit from type checking in Lit templates, for example validating binding names.
Browser support
vanilla-colorful uses Custom Elements and Shadow DOM, and does not support IE11 or legacy Edge.
Why vanilla-colorful?
vanilla-colorful has all the benefits of react-colorful with one important difference.
While react-colorful
claims to have zero dependencies, it still expects you to use React or Preact.
This means that Angular, Vue, Svelte or vanilla JS users would have an extra dependency in their apps.
Now when all the evergreen browsers support standards based Custom Elements, it's perfect time to build such tiny and lightweight UI controls as web components rather than framework components.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/tests.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/web-padawan/vanilla-colorful/tests.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/tests.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/web-padawan/vanilla-colorful/tests.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/tests.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/web-padawan/vanilla-colorful/tests.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/tests.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/web-padawan/vanilla-colorful/tests.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/tests.yml:46: update your workflow using https://app.stepsecurity.io/secureworkflow/web-padawan/vanilla-colorful/tests.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/tests.yml:48: update your workflow using https://app.stepsecurity.io/secureworkflow/web-padawan/vanilla-colorful/tests.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/tests.yml:62: update your workflow using https://app.stepsecurity.io/secureworkflow/web-padawan/vanilla-colorful/tests.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/tests.yml:64: update your workflow using https://app.stepsecurity.io/secureworkflow/web-padawan/vanilla-colorful/tests.yml/master?enable=pin
- Info: 0 out of 8 GitHub-owned GitHubAction dependencies pinned
- Info: 4 out of 4 npmCommand dependencies pinned
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/tests.yml:1
- Info: no jobLevel write permissions found
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 27 are checked with a SAST tool
Score
3.7
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to vanilla-colorful
react-colorful
🎨 A tiny (2,8 KB) color picker component for React and Preact apps. Fast, well-tested, dependency-free, mobile-friendly and accessible
@uiw/react-color-colorful
Color Colorful
@vanilla-extract/integration
Zero-runtime Stylesheets-in-TypeScript
vanilla-picker
A simple, easy to use vanilla JS color picker with alpha selection.