vanilla-colorful is a port of
react colorful to vanilla Custom Elements.
Features
- Small: Just 2,2 KB (minified and gzipped). Size Limit controls the size.
- Fast: Built with standards based Custom Elements.
- Bulletproof: Written in strict TypeScript and covered by 20+ tests.
- Simple: The interface is straight forward and easy to use.
- Mobile-friendly: Works well on mobile devices and touch screens.
- Framework-agnostic: Can be used with any framework.
- No dependencies
Live demo
Install
npm install vanilla-colorful --save
Or use one of the following content delivery networks:
unpkg.com CDN:
<script type="module" src="https://unpkg.com/vanilla-colorful?module"></script>
Skypack CDN:
<script type="module" src="https://cdn.skypack.dev/vanilla-colorful"></script>
Usage
<color-picker-hex color="#1e88e5"></color-picker-hex>
<script type="module">
import 'vanilla-colorful';
const picker = document.querySelector('color-picker-gex');
picker.addEventListener('color-changed', (event) => {
const newColor = event.detail.value;
});
</script>
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 5 additional color picker bundles.
How to use another color model
Available pickers
File to import | HTML element | Value example |
---|
"color-picker-rgb.js" | <color-picker-rgb> | { r: 255, g: 255, b: 255 } |
"color-picker-rgb-string.js" | <color-picker-rgb-string> | "rgb(255, 255, 255)" |
"color-picker-hsl.js" | <color-picker-hsl> | { h: 0, s: 0, l: 100 } |
"color-picker-hsl-string.js" | <color-picker-hsl-string> | "hsl(0, 0%, 100%)" |
"color-picker-hsv.js" | <color-picker-hsv> | { h: 0, s: 0, v: 100 } |
Code example
<color-picker-rgb></color-picker-rgb>
<script type="module">
import 'vanilla-colorful/color-picker-rgb.js';
const picker = document.querySelector('color-picker-rgb');
picker.color = { r: 50, g: 100, b: 150 };
</script>
Overriding styles
vanilla-colorful exposes CSS Shadow Parts
allowing to override the default styles:
color-picker-hex {
height: 250px;
}
color-picker-hex::part(saturation) {
bottom: 30px;
border-radius: 3px 3px 0 0;
}
color-picker-hex::part(hue) {
height: 30px;
border-radius: 0 0 3px 3px;
}
color-picker-hex::part(saturation-pointer) {
border-radius: 5px;
}
color-picker-hex::part(hue-pointer) {
border-radius: 2px;
width: 15px;
height: inherit;
}
HEX input
vanilla-colorful provides an additional <hex-input>
element that can be used to type a color:
<hex-input color="#1e88e5"></hex-input>
<script type="module">
import 'vanilla-colorful/hex-input.js';
const input = document.querySelector('hex-input');
input.addEventListener('color-changed', (event) => {
const newColor = event.detail.value;
});
</script>
<hex-input>
does not use Shadow DOM and renders an input
element without any custom styles. You
can also provide a custom input
element as a child if you want to configure it.
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.
import { RgbBase } from 'vanilla-colorful/lib/entrypoints/rgb.js';
customElements.define('custom-color-picker', class extends RgbBase {});
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
While not only typing its own class methods and variables, it can also help you type yours. Depending on
the element you're using, you can also import the type that is associated with the element.
For example, if you're using our HSL color picker component, you can also import the HSL
type.
import type { HSL } from 'vanilla-colorful/color-picker-hsl';
const myHslValue: HSL = { h: 0, s: 0, l: 0 };
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-html templates.
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.