Skip to content

Commit 511b824

Browse files
Add types for dom-eyedropper (EyeDropper API) (DefinitelyTyped#74253)
1 parent 8d36ed2 commit 511b824

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

types/dom-eyedropper/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Basic constructor test
2+
const eyeDropper = new EyeDropper();
3+
4+
// Test open() with no options
5+
eyeDropper.open().then((result) => {
6+
// $ExpectType ColorSelectionResult
7+
result;
8+
9+
// $ExpectType string
10+
result.sRGBHex;
11+
});
12+
13+
// Test open() with empty options
14+
eyeDropper.open({}).then((result) => {
15+
// $ExpectType ColorSelectionResult
16+
result;
17+
});
18+
19+
// Test open() with AbortSignal
20+
const controller = new AbortController();
21+
eyeDropper.open({ signal: controller.signal }).then((result) => {
22+
// $ExpectType ColorSelectionResult
23+
result;
24+
25+
// $ExpectType string
26+
result.sRGBHex;
27+
});
28+
29+
// Test aborting the eyedropper
30+
const controller2 = new AbortController();
31+
const promise = eyeDropper.open({ signal: controller2.signal });
32+
controller2.abort();
33+
promise.catch((error) => {
34+
// $ExpectType any
35+
error;
36+
});
37+
38+
// Test using async/await
39+
async function selectColor() {
40+
const eyeDropper = new EyeDropper();
41+
const result = await eyeDropper.open();
42+
43+
// $ExpectType ColorSelectionResult
44+
result;
45+
46+
// $ExpectType string
47+
result.sRGBHex;
48+
49+
return result.sRGBHex;
50+
}
51+
52+
// Test with user interaction (e.g., button click)
53+
document.getElementById("color-picker")?.addEventListener("click", async () => {
54+
const eyeDropper = new EyeDropper();
55+
try {
56+
const result = await eyeDropper.open();
57+
console.log(result.sRGBHex);
58+
} catch (error) {
59+
console.error("Color selection failed:", error);
60+
}
61+
});

types/dom-eyedropper/index.d.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Options passed to the open() method of the EyeDropper API
3+
*/
4+
interface ColorSelectionOptions {
5+
/**
6+
* An AbortSignal. The eyedropper selection is aborted when abort() is called on the associated AbortController.
7+
*/
8+
signal?: AbortSignal;
9+
}
10+
11+
/**
12+
* Result returned from the open() method of the EyeDropper API when a color is successfully selected
13+
*/
14+
interface ColorSelectionResult {
15+
/**
16+
* A string in hexadecimal sRGB format (#RRGGBB) representing the selected color
17+
*/
18+
sRGBHex: string;
19+
}
20+
21+
/**
22+
* The EyeDropper interface provides access to a browser-supplied eyedropper tool.
23+
* It allows users to sample colors from their screen, including content outside of the browser window.
24+
*/
25+
interface EyeDropper {
26+
/**
27+
* Initiates the eyedropper mode.
28+
* Returns a promise that resolves with the selected color when the user selects a pixel,
29+
* or rejects if the operation is cancelled or fails.
30+
*
31+
* @param options - Optional configuration including an AbortSignal to cancel the operation
32+
* @returns A promise that resolves to a ColorSelectionResult containing the selected color in sRGB hex format
33+
* @throws {DOMException} NotAllowedError if user activation is required but not present
34+
* @throws {DOMException} InvalidStateError if the eyedropper is already open
35+
* @throws {DOMException} AbortError if the operation is aborted via the AbortSignal
36+
* @throws {DOMException} OperationError if the operation fails for other reasons
37+
*/
38+
open(options?: ColorSelectionOptions): Promise<ColorSelectionResult>;
39+
}
40+
41+
declare var EyeDropper: {
42+
prototype: EyeDropper;
43+
/**
44+
* Creates a new EyeDropper instance
45+
*/
46+
new(): EyeDropper;
47+
};

types/dom-eyedropper/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"private": true,
3+
"name": "@types/dom-eyedropper",
4+
"version": "0.1.9999",
5+
"nonNpm": true,
6+
"nonNpmDescription": "EyeDropper API",
7+
"projects": [
8+
"https://wicg.github.io/eyedropper-api/"
9+
],
10+
"devDependencies": {
11+
"@types/dom-eyedropper": "workspace:."
12+
},
13+
"owners": [
14+
{
15+
"name": "Nathan Babcock",
16+
"githubUsername": "nathanbabcock"
17+
}
18+
]
19+
}

types/dom-eyedropper/tsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6",
6+
"dom"
7+
],
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictFunctionTypes": true,
11+
"strictNullChecks": true,
12+
"types": [],
13+
"noEmit": true,
14+
"forceConsistentCasingInFileNames": true
15+
},
16+
"files": [
17+
"index.d.ts",
18+
"dom-eyedropper-tests.ts"
19+
]
20+
}

0 commit comments

Comments
 (0)