EyeDropper API Polyfill

1.1.5 · active · verified Wed Apr 22

The `eyedropper-polyfill` package provides a robust polyfill for the W3C EyeDropper API, enabling developers to integrate a native-like color picking experience into web applications even in browsers that lack native support. Currently at stable version `1.1.5`, the library maintains a steady release cadence, primarily focusing on bug fixes and performance improvements as seen in recent patch releases in January 2026. Its key differentiator is its ability to replicate the EyeDropper functionality by leveraging `html2canvas-pro` internally, which allows it to capture colors from rendered web page content. This makes it a critical tool for ensuring consistent UX across a wider range of browsers, bridging compatibility gaps where the native API is not available, such as older browser versions or specific environments. It's designed for browser-side use and ships with TypeScript definitions for enhanced developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize and use the EyeDropper polyfill to pick a color, including error handling for user aborts and a fallback check for API availability. It changes the background color based on selection.

import 'eyedropper-polyfill';

const openEyeDropper = async () => {
  if (!window.EyeDropper) {
    console.error('EyeDropper API or polyfill not available.');
    alert('Your browser does not support the EyeDropper API.');
    return;
  }

  const eyeDropper = new window.EyeDropper();
  const abortController = new window.AbortController();
  const signal = abortController.signal;

  // Optionally abort after 10 seconds
  const timeoutId = setTimeout(() => {
    console.log('EyeDropper operation timed out, aborting.');
    abortController.abort();
  }, 10000);

  try {
    const colorSelectionResult = await eyeDropper.open({ signal });
    clearTimeout(timeoutId);
    console.log('Selected color:', colorSelectionResult.sRGBHex);
    document.body.style.backgroundColor = colorSelectionResult.sRGBHex;
    alert(`Selected color: ${colorSelectionResult.sRGBHex}`);
  } catch (error) {
    clearTimeout(timeoutId);
    if (error.name === 'AbortError') {
      console.log('EyeDropper operation was aborted.');
    } else {
      console.error('Error opening EyeDropper:', error);
    }
  }
};

// Attach to a button click or call directly
// Example: create a button and call this function
const button = document.createElement('button');
button.textContent = 'Pick a color';
button.style.padding = '10px 20px';
button.style.fontSize = '18px';
button.style.margin = '20px';
button.onclick = openEyeDropper;
document.body.appendChild(button);

view raw JSON →