import React, { Fragment } from 'react'; import type * as CSS from 'csstype'; import { type HsvaColor, hsvaToRgbaString, color as handleColor, validHex, hexToHsva, hsvaToHex, hsvaToHexa, } from '@uiw/color-convert'; import Github, { type GithubProps, GithubPlacement } from '@uiw/react-color-github'; import Saturation from '@uiw/react-color-saturation'; import Hue from '@uiw/react-color-hue'; import Alpha from '@uiw/react-color-alpha'; import EditableInput from '@uiw/react-color-editable-input'; import EditableInputRGBA from '@uiw/react-color-editable-input-rgba'; import EditableInputHSLA from '@uiw/react-color-editable-input-hsla'; import { useState } from 'react'; import Arrow from './Arrow'; import { EyeDropper, getIsEyeDropperSupported } from './EyeDropper'; export enum ChromeInputType { HEXA = 'hexa', RGBA = 'rgba', HSLA = 'hsla', } export interface ChromeProps extends Omit { inputType?: ChromeInputType; showEditableInput?: boolean; showEyeDropper?: boolean; showColorPreview?: boolean; showHue?: boolean; showAlpha?: boolean; horizontal?: boolean; } const Chrome = React.forwardRef((props, ref) => { const { prefixCls = 'w-color-chrome', className, style, color, showEditableInput = true, showEyeDropper = true, showColorPreview = true, showHue = true, showAlpha = true, horizontal = false, inputType = ChromeInputType.RGBA, rectProps = {}, onChange, ...other } = props; const hsva = ( typeof color === 'string' && validHex(color) ? hexToHsva(color) : color || { h: 0, s: 0, l: 0, a: 0 } ) as HsvaColor; const handleChange = (hsv: HsvaColor) => onChange && onChange(handleColor(hsv)); const [type, setType] = useState(inputType); const handleClick = () => { if (type === ChromeInputType.RGBA) { setType(ChromeInputType.HSLA); } if (type === ChromeInputType.HSLA) { setType(ChromeInputType.HEXA); } if (type === ChromeInputType.HEXA) { setType(ChromeInputType.RGBA); } }; const labelStyle: CSS.Properties = { paddingTop: 6 }; const inputStyle: CSS.Properties = { textAlign: 'center', paddingTop: 4, paddingBottom: 4 }; const wrapperStyle: CSS.Properties = { '--chrome-arrow-fill': '#333', '--chrome-arrow-background-color': '#e8e8e8', borderRadius: 0, flexDirection: horizontal ? 'row' : 'column', width: horizontal ? 460 : 230, padding: 0, ...style, } as CSS.Properties; const alphaStyle: CSS.Properties = { '--chrome-alpha-box-shadow': 'rgb(0 0 0 / 25%) 0px 0px 1px inset', borderRadius: '50%', background: hsvaToRgbaString(hsva), boxShadow: 'var(--chrome-alpha-box-shadow)', } as CSS.Properties; const handleClickColor = (hex: string) => { let result = hexToHsva(hex); handleChange({ ...result }); }; const styleSize = { height: 14, width: 14 }; const pointerProps = { style: { ...styleSize }, fillProps: { style: styleSize }, }; return ( { handleChange({ ...hsva, ...newColor, a: hsva.a }); }} />
{getIsEyeDropperSupported() && showEyeDropper && } {showColorPreview && ( } /> )}
{showHue == true && ( { handleChange({ ...hsva, ...newHue }); }} /> )} {showAlpha == true && ( { handleChange({ ...hsva, ...newAlpha }); }} /> )}
{showEditableInput && (
{type == ChromeInputType.RGBA && ( handleChange(reColor.hsva)} /> )} {type === ChromeInputType.HEXA && ( 0 && hsva.a < 1 ? hsvaToHexa(hsva).toLocaleUpperCase() : hsvaToHex(hsva).toLocaleUpperCase() } onChange={(_, value) => { if (typeof value === 'string') { handleChange(hexToHsva(/^#/.test(value) ? value : `#${value}`)); } }} /> )} {type === ChromeInputType.HSLA && ( handleChange(reColor.hsva)} /> )}
)}
} rectRender={() => } /> ); }); Chrome.displayName = 'Chrome'; export default Chrome;