forked from vitest-dev/vitest
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useEyeDropper): Reactive EyeDropper API (vitest-dev#911)
Co-authored-by: Jelf <haichao.liang@parameters.cn> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
ca0ff73
commit b65f705
Showing
8 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { defineComponent, reactive } from 'vue-demi' | ||
import { useEyeDropper } from '@vueuse/core' | ||
|
||
export const UseEyeDropper = defineComponent({ | ||
name: 'UseEyeDropper', | ||
props: { | ||
sRGBHex: String, | ||
}, | ||
setup(props, { slots }) { | ||
const data = reactive(useEyeDropper()) | ||
|
||
return () => { | ||
if (slots.default) | ||
return slots.default(data) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<template> | ||
<template v-if="isSupported"> | ||
<div>isSupported: {{ isSupported }}</div> | ||
<div>sRGBHex: {{ sRGBHex }}</div> | ||
</template> | ||
<div> | ||
<button | ||
:disabled="!isSupported" | ||
@click="open" | ||
> | ||
{{ isSupported ? 'Open Eye Dropper' : 'Not Supported by Your Browser' }} | ||
</button> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { useEyeDropper } from '@vueuse/core' | ||
const { isSupported, open, sRGBHex } = useEyeDropper() | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
category: Browser | ||
--- | ||
|
||
# useEyeDropper | ||
|
||
Reactive [EyeDropper API](https://developer.mozilla.org/en-US/docs/Web/API/EyeDropper_API) | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useEyeDropper } from '@vueuse/core' | ||
|
||
const { isSupported, open, sRGBHex } = useEyeDropper() | ||
``` | ||
|
||
## Component | ||
|
||
```html | ||
<UseEyeDropper v-slot="{ isSupported, sRGBHex, open }"> | ||
<button :disabled="!isSupported" @click="open"> | ||
sRGBHex: {{ sRGBHex }} | ||
</button> | ||
</UseEyeDropper> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ref } from 'vue-demi' | ||
|
||
export function useEyeDropper() { | ||
const isSupported = Boolean(window && 'EyeDropper' in window) | ||
const sRGBHex = ref('') | ||
|
||
async function open() { | ||
// @ts-expect-error | ||
const eyeDropper = new window.EyeDropper() | ||
const result = await eyeDropper.open() | ||
sRGBHex.value = result.sRGBHex | ||
return result | ||
} | ||
|
||
return { isSupported, sRGBHex, open } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters