useEyeDropper
使用 EyeDropper API 来选择颜色。
Usage
实时编辑器
function Demo() { const [color, setColor] = useState(""); const [supported, open] = useEyeDropper(); if (supported) { return ( <div style={{ padding: 40 }}> 支持状态: {supported.toString()} <br /> 颜色: {color} <button type="button" onClick={async () => setColor((await open()).sRGBHex)} > 选择颜色 </button> </div> ); } return <span>您的浏览器不支持此功能</span>; };
结果
支持状态: true
颜色:
颜色:
API
useEyeDropper
Returns
readonly [boolean, (options?: UseEyeDropperOpenOptions | undefined) => Promise<UseEyeDropperOpenReturnType>]: 包含以下元素的元组:
- 浏览器是否支持该特性。
- 打开颜色选择器的函数。
Arguments
UseEyeDropperOpenOptions
| 参数名 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| signal | 终止信号 | AbortSignal | - |
UseEyeDropperOpenReturnType
| 参数名 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| sRGBHex | rgb 颜色值 | string (必填) | - |