forked from vueuse/vueuse
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
48 lines (42 loc) · 1.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { isSup } from '@vueuse/shared'
import { ref } from 'vue-demi'
export interface EyeDropperOpenOptions {
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
*/
signal?: AbortSignal
}
export interface EyeDropper {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new(): EyeDropper
open: (options?: EyeDropperOpenOptions) => Promise<{ sRGBHex: string }>
[Symbol.toStringTag]: 'EyeDropper'
}
export interface UseEyeDropperOptions {
/**
* Initial sRGBHex.
*
* @default ''
*/
initialValue?: string
}
/**
* Reactive [EyeDropper API](https://developer.mozilla.org/en-US/docs/Web/API/EyeDropper_API)
*
* @see https://vueuse.org/useEyeDropper
* @param initialValue string
*/
export function useEyeDropper(options: UseEyeDropperOptions = {}) {
const { initialValue = '' } = options
const isSupported = isSup(() => Boolean(typeof window !== 'undefined' && 'EyeDropper' in window))
const sRGBHex = ref(initialValue)
async function open(openOptions?: EyeDropperOpenOptions) {
if (!isSupported.value)
return
const eyeDropper: EyeDropper = new (window as any).EyeDropper()
const result = await eyeDropper.open(openOptions)
sRGBHex.value = result.sRGBHex
return result
}
return { isSupported, sRGBHex, open }
}