• Stars
    star
    731
  • Rank 59,723 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tiny color picker custom element for modern web apps (2.7 KB) 🎨
vanilla-colorful is a port of react-colorful to vanilla Custom Elements.

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:

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>

JSPM CDN:

<script type="module" src="https://jspm.dev/vanilla-colorful"></script>

ESM CDN:

<script type="module" src="https://esm.sh/vanilla-colorful"></script>

Usage

<hex-color-picker color="#1e88e5"></hex-color-picker>
<script type="module">
  import 'vanilla-colorful';

  const picker = document.querySelector('hex-color-picker');
  picker.addEventListener('color-changed', (event) => {
    // get updated color value
    const newColor = event.detail.value;
  });

  // get current color value
  console.log(picker.color);
</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

<rgba-color-picker></rgba-color-picker>
<script type="module">
  import 'vanilla-colorful/rgba-color-picker.js';

  const picker = document.querySelector('rgba-color-picker');
  picker.color = { r: 50, g: 100, b: 150, a: 1 };
</script>

Overriding styles

vanilla-colorful exposes CSS Shadow Parts allowing to override the default styles:

hex-color-picker {
  height: 250px;
}

hex-color-picker::part(saturation) {
  bottom: 30px;
  border-radius: 3px 3px 0 0;
}

hex-color-picker::part(hue) {
  height: 30px;
  border-radius: 0 0 3px 3px;
}

hex-color-picker::part(saturation-pointer) {
  border-radius: 5px;
}

hex-color-picker::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> 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.

import { RgbBase } from 'vanilla-colorful/lib/entrypoints/rgb.js';

customElements.define('custom-color-picker', class extends RgbBase {});

Code Recipes

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.

import type { HslColor } from 'vanilla-colorful/hsl-color-picker';

const 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:

const picker = document.querySelector('rgba-color-picker');

picker.addEventListener('color-changed', (event) => {
  console.log(event.detail.value.a); // (property) RgbaColor.a: number
});

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.

More Repositories

1

awesome-web-components

A curated list of awesome Web Components resources.
2,636
star
2

awesome-lit

A curated list of awesome Lit resources.
1,299
star
3

vanilla-hamburger

Animated hamburger menu icons for modern web apps (1.8 KB) 🍔
TypeScript
120
star
4

polymer3-webpack-starter

Polymer 3 starter built with Vaadin components and Vaadin.Router library, using webpack and modern tools
JavaScript
101
star
5

aybolit

Lightweight web components library built with LitElement.
JavaScript
100
star
6

webcomponents.today

A curated collection of Web Components resources.
JavaScript
80
star
7

lit-components

Moved to https://github.com/vaadin/component-mixins
JavaScript
60
star
8

lit-vaadin-helpers

A set of helpers for using Vaadin web components with Lit 2.
TypeScript
10
star
9

ts-vaadin-examples

Examples for using Vaadin components with TypeScript
TypeScript
8
star
10

pack

Polymer App Creation Kit
HTML
8
star
11

wc-jest-puppeteer-demo

JavaScript
5
star
12

polylit

An experiment to use Polymer based mixins with Lit.
JavaScript
4
star
13

a11y-vaadin-playground

Demo app showcasing accessibility fixes to Vaadin web components.
HTML
4
star
14

lit-overlays

Prototyping a set of overlay components with LitElement & TS
TypeScript
3
star
15

webpack-wc-demo

JavaScript
3
star
16

stylelint-config-polymer

shareable stylelint config for Polymer elements
JavaScript
2
star
17

elements-viewer

Vaadin core components demos & API docs https://web-padawan.github.io/elements-viewer/?vaadin-button#docs
HTML
2
star
18

fusion-wtr

TypeScript
1
star
19

a11y-vaadin-proto

JavaScript
1
star
20

npm-stats-training

HTML
1
star
21

colorizr

a WIP demo project built with Polymer 1.x
HTML
1
star
22

web-padawan.github.io

Homepage
Nunjucks
1
star
23

component-mixins

Moved to vaadin/component-mixins
TypeScript
1
star
24

hermione-esm

hermione plugin that starts @web/dev-server before running tests
JavaScript
1
star
25

pnpm-resolutions

An example how to make yarn resolutions work with pnpm
JavaScript
1
star
26

npm-downloads

JavaScript
1
star
27

skystart

frontend project template
CSS
1
star
28

components-flowchart

Visualise hierarchical relationships within Vaadin components.
1
star
29

ngplugins

plugins for NG CMS
PHP
1
star
30

shadow-examples

Examples of using Shadow DOM
HTML
1
star
31

polymer-sort-and-search

React Challenge #1 task built with Polymer
HTML
1
star