npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-eye-dropper

v1.6.1

Published

Browser color picker hook for React

Downloads

28,033

Readme

useEyeDropper

img img img img img

👀🩸🧫 Browser color picker hook for React.

Implements the EyeDropper API into an easy-to-use React hook. This API is currently only available in Chromium based browsers.

Features

  • Supports Server-Side rendering.
  • Test coverage with unit and integration tests.
  • TypeScript support
  • Under 670 bytes GZipped
  • Safely detect and fallback on unsupported browsers using isSupported method.
  • Closes eye dropper when corresponding component is unmounted.
  • Provides explicit close method to cancel eye dropper (signals can still be used).

Installation

pnpm

pnpm add use-eye-dropper

Yarn

yarn add use-eye-dropper

npm

npm install use-eye-dropper

Usage

import React, { useState, useCallback } from 'react'
import useEyeDropper from 'use-eye-dropper'

const App = () => {
  const { open, close, isSupported } = useEyeDropper()
  const [color, setColor] = useState('#fff')
  const [error, setError] = useState()
  // useEyeDropper will reject/cleanup the open() promise on unmount,
  // so setState never fires when the component is unmounted.
  const pickColor = useCallback(() => {
    // Using async/await (can be used as a promise as-well)
    const openPicker = async () => {
      try {
        const color = await open()
        setColor(color.sRGBHex)
      } catch (e) {
        console.log(e)
        // Ensures component is still mounted
        // before calling setState
        if (!e.canceled) setError(e)
      }
    }
    openPicker()
  }, [open])
  return (
    <>
      <div style={{ padding: '64px', background: color }}>Selected color</div>
      {isSupported() ?
          <button onClick={pickColor}>Pick color</button>
        : <span>EyeDropper API not supported in this browser</span>
      }
      {!!error && <div>{error.message}</div>}
    </>
  )
}

Usage with TypeScript

With TypeScript all of the types will be inferred where possible, however if you need to use error handling some type-guard functions can be used to narrow down the type of the error from the catch block. Otherwise it will be unknown.

Here is one approach to deal with this when working with TypeScript and dropper errors:

type DropperError = { 
  message: string
  canceled?: boolean
}

const isError = <T, >(err: DropperError | T): err is DropperError => 
  !!err && err instanceof Error && !!err.message

const isNotCanceled = <T, >(err: DropperError | T): err is DropperError =>
  isError(err) && !err.canceled

and then !e.canceled can be replaced with isNotCanceled(e), the type-guard will enforce a proper type when using setError.

This will also work for isError.

Methods

  • open({ signal?: AbortSignal }) => Promise<{ sRGBHex: string }>'

    Opens the EyeDropper API in supported browsers and returns a promise which will resolve with the selected color. Alternatively the promise will be rejected if the user cancels the operation, for instance by hitting escape. Additionally if the browser does not support the API, the promise is rejected. While the spec currently indicates that a 6-digit HEX value is returned, the current Chrome implementation returns a rgba value.

  • close() => void

    This method closes the EyeDropper API selector if it is open and rejects the promise from open. Otherwise this performs a no-op.

  • isSupported() => boolean

    Determines if the EyeDropper API is supported in the current browser.