UNPKG

@sv-use/core

Version:

A collection of Svelte 5 utilities.

29 lines (28 loc) 973 B
import { isSupported } from '../__internal__/is.svelte.js'; import { defaultWindow } from '../__internal__/configurable.js'; /** * Provides a mechanism for creating an eye dropper tool. * @see https://svelte-librarian.github.io/sv-use/docs/core/create-eye-dropper */ export function createEyeDropper(options = {}) { const { initialValue = undefined, window = defaultWindow } = options; const _isSupported = isSupported(() => !!window && 'EyeDropper' in window); let _current = $state(initialValue); async function open(openOptions) { if (!_isSupported.current || !window) return; const eyeDropper = new window.EyeDropper(); const result = await eyeDropper.open(openOptions); _current = result.sRGBHex; return result; } return { get isSupported() { return _isSupported.current; }, get current() { return _current; }, open }; }