Package Exports
- use-eye-dropper
- use-eye-dropper/lib/index.js
- use-eye-dropper/lib/index.module.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (use-eye-dropper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
useEyeDropper
ππ©Έπ§« 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.
- Safely detect and fallback on unsupported browsers using
isSupportedmethod. - Closes eye dropper when corresponding component is unmounted.
- Provides explicit
closemethod to cancel eye dropper (signals can still be used).
Installation
Yarn
yarn add use-eye-droppernpm
npm install use-eye-dropperUsage
import React, { useState } 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 = () => {
open()
.then(color => setColor(color.sRGBHex))
.catch(e => {
console.log(e)
// Ensures component is still mounted
// before calling setState
if (!e.canceled) setError(e)
})
}
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 && <span>{error.message}</span>}
</>
)
}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
rgbavalue.close() => voidThis method closes the EyeDropper API selector if it is open and rejects the promise from
open. Otherwise this performs a no-op.isSupported() => booleanDetermines if the EyeDropper API is supported in the current browser.