Vanilla Colorful Color Picker

0.7.2 · active · verified Tue Apr 21

vanilla-colorful is a lightweight, framework-agnostic color picker library implemented as W3C standards-based Custom Elements. It serves as a vanilla JavaScript port of the popular `react-colorful` library, offering the same core functionality in a dependency-free, highly optimized package. The current stable version is 0.7.2, released in November 2022, and while no explicit release cadence is stated, it appears to be actively maintained. Key differentiators include its extremely small bundle size (just 2.7 KB minified and gzipped), 100% test coverage, full TypeScript typings, and comprehensive accessibility (WAI-ARIA) and mobile support. It provides various color picker types (HEX, RGB, HSL, HSV) directly usable as HTML custom elements, making integration into any web application or framework straightforward.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate and interact with `hex-color-picker` using standard Custom Elements. It registers the component, initializes its value, listens for `color-changed` events, and updates an output element. It also shows programmatic color setting.

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vanilla Colorful Quickstart</title>
  <style>
    body {
      font-family: -apple-system, BlinkMacMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      margin: 0;
      background-color: #f0f0f0;
    }
    .wrapper {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 20px;
    }
    output {
      display: block;
      margin-top: 10px;
      font-size: 1.5rem;
      font-weight: bold;
      text-align: center;
      padding: 10px 20px;
      border-radius: 5px;
      border: 1px solid #ddd;
      background-color: white;
    }
  </style>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <div class="wrapper">
    <h1>Choose a Color</h1>
    <hex-color-picker class="my-picker" color="#1e88e5"></hex-color-picker>
    <output id="colorOutput"></output>
  </div>
</body>
</html>

// main.js
import 'vanilla-colorful';

document.addEventListener('DOMContentLoaded', () => {
  const picker = document.querySelector('hex-color-picker.my-picker');
  const output = document.getElementById('colorOutput');

  // Set initial color output
  if (picker && output) {
    output.textContent = picker.color;
    output.style.color = picker.color; // For text color visualization
  }

  // Listen for color changes
  picker?.addEventListener('color-changed', (event) => {
    const newColor = event.detail.value;
    if (output) {
      output.textContent = newColor;
      output.style.color = newColor; // Update text color
      console.log('Color changed to:', newColor);
    }
  });

  // Example of programmatically changing the color after some time
  setTimeout(() => {
    if (picker) {
      picker.color = '#ff4500'; // Set new color (e.g., OrangeRed)
      console.log('Programmatically set color to:', picker.color);
    }
  }, 3000);
});

view raw JSON →