Skip to content

Commit 2ea8caa

Browse files
committed
feat(Chremo): Add EyeDropper in Chrome component. #148
1 parent 64cb068 commit 2ea8caa

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

packages/color-alpha/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "../../tsconfig",
3-
"include": ["./src"],
3+
"include": ["./src", "../color-chrome/src/EyeDropper.tsx"],
44
"compilerOptions": {
55
"outDir": "./cjs",
66
"noEmit": false
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
3+
export const isSupportEyeDropper = 'EyeDropper' in window;
4+
5+
export interface EyeDropperProps {
6+
onPickColor?: (color: string) => void;
7+
}
8+
9+
export function EyeDropper(props: EyeDropperProps) {
10+
const click = () => {
11+
if ('EyeDropper' in window) {
12+
const eyeDropper = new (window as any).EyeDropper();
13+
eyeDropper.open().then((result: any) => {
14+
console.log(result);
15+
props.onPickColor?.(result.sRGBHex);
16+
});
17+
}
18+
};
19+
return (
20+
<svg viewBox="0 0 512 512" height="1em" width="1em" onClick={click}>
21+
<path
22+
fill="currentColor"
23+
d="M482.8 29.23c38.9 38.98 38.9 102.17 0 141.17L381.2 271.9l9.4 9.5c12.5 12.5 12.5 32.7 0 45.2s-32.7 12.5-45.2 0l-160-160c-12.5-12.5-12.5-32.7 0-45.2s32.7-12.5 45.2 0l9.5 9.4L341.6 29.23c39-38.974 102.2-38.974 141.2 0zM55.43 323.3 176.1 202.6l45.3 45.3-120.7 120.7c-3.01 3-4.7 7-4.7 11.3V416h36.1c4.3 0 8.3-1.7 11.3-4.7l120.7-120.7 45.3 45.3-120.7 120.7c-15 15-35.4 23.4-56.6 23.4H89.69l-39.94 26.6c-12.69 8.5-29.59 6.8-40.377-4-10.786-10.8-12.459-27.7-3.998-40.4L32 422.3v-42.4c0-21.2 8.43-41.6 23.43-56.6z"
24+
/>
25+
</svg>
26+
);
27+
}

packages/color-chrome/src/index.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import EditableInputRGBA from '@uiw/react-color-editable-input-rgba';
1717
import EditableInputHSLA from '@uiw/react-color-editable-input-hsla';
1818
import { useState } from 'react';
1919
import Arrow from './Arrow';
20+
import { EyeDropper, isSupportEyeDropper } from './EyeDropper';
2021

2122
export enum ChromeInputType {
2223
HEXA = 'hexa',
@@ -72,6 +73,10 @@ const Chrome = React.forwardRef<HTMLDivElement, ChromeProps>((props, ref) => {
7273
background: hsvaToRgbaString(hsva),
7374
boxShadow: 'var(--chrome-alpha-box-shadow)',
7475
};
76+
const handleClickColor = (hex: string) => {
77+
let result = hexToHsva(hex);
78+
handleChange({ ...result });
79+
};
7580
return (
7681
<Github
7782
ref={ref}
@@ -90,14 +95,14 @@ const Chrome = React.forwardRef<HTMLDivElement, ChromeProps>((props, ref) => {
9095
handleChange({ ...hsva, ...newColor, a: hsva.a });
9196
}}
9297
/>
93-
<div style={{ padding: 15, display: 'flex', alignItems: 'center' }}>
98+
<div style={{ padding: 15, display: 'flex', alignItems: 'center', gap: 15 }}>
99+
{isSupportEyeDropper && <EyeDropper onPickColor={handleClickColor} />}
94100
<Alpha
95-
width={24}
96-
height={24}
101+
width={28}
102+
height={28}
97103
hsva={hsva}
98104
radius={2}
99105
style={{
100-
marginRight: 15,
101106
borderRadius: '50%',
102107
}}
103108
bgProps={{ style: { background: 'transparent' } }}

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"declaration": true,
1717
"baseUrl": ".",
1818
"noFallthroughCasesInSwitch": true,
19-
"noEmit": true,
20-
"types": ["jest", "node"]
19+
"noEmit": true
2120
}
2221
}

0 commit comments

Comments
 (0)