All files / color-chrome/src index.tsx

93.93% Statements 31/33
85% Branches 51/60
91.66% Functions 11/12
93.75% Lines 30/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                                                            1x                               18x 18x     18x 18x 18x 8x 4x   8x 3x   8x 1x     18x 18x 18x                   18x             18x       18x 18x       18x                             1x                                     18x                         1x                         1x                               1x                       3x 2x                       1x                   288x         1x      
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<GithubProps, 'colors'> {
  inputType?: ChromeInputType;
  showEditableInput?: boolean;
  showEyeDropper?: boolean;
  showColorPreview?: boolean;
  showHue?: boolean;
  showAlpha?: boolean;
  horizontal?: boolean;
}
 
const Chrome = React.forwardRef<HTMLDivElement, ChromeProps>((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<string | number> = { paddingTop: 6 };
  const inputStyle: CSS.Properties<string | number> = { textAlign: 'center', paddingTop: 4, paddingBottom: 4 };
  const wrapperStyle: CSS.Properties<string | number> = {
    '--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<string | number>;
 
  const alphaStyle: CSS.Properties<string | number> = {
    '--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<string | number>;
 
  const handleClickColor = (hex: string) => {
    let result = hexToHsva(hex);
    handleChange({ ...result });
  };
  const styleSize = { height: 14, width: 14 };
  const pointerProps = {
    style: { ...styleSize },
    fillProps: { style: styleSize },
  };
  return (
    <Github
      ref={ref}
      color={hsva}
      style={wrapperStyle}
      colors={undefined}
      className={[prefixCls, className].filter(Boolean).join(' ')}
      placement={GithubPlacement.TopLeft}
      {...other}
      addonAfter={
        <Fragment>
          <Saturation
            hsva={hsva}
            style={horizontal ? { width: 230, height: 120 } : { width: '100%', height: 130 }}
            onChange={(newColor) => {
              handleChange({ ...hsva, ...newColor, a: hsva.a });
            }}
          />
          <div style={{ display: 'flex', flexDirection: 'column', width: horizontal ? 230 : 'inherit' }}>
            <div style={{ padding: 15, display: 'flex', alignItems: 'center', gap: 10 }}>
              {getIsEyeDropperSupported() && showEyeDropper && <EyeDropper onPickColor={handleClickColor} />}
              {showColorPreview && (
                <Alpha
                  width={28}
                  height={28}
                  hsva={hsva}
                  radius={2}
                  style={{
                    borderRadius: '50%',
                  }}
                  bgProps={{ style: { background: 'transparent' } }}
                  innerProps={{
                    style: alphaStyle,
                  }}
                  pointer={() => <Fragment />}
                />
              )}
              <div style={{ flex: 1 }}>
                {showHue == true && (
                  <Hue
                    hue={hsva.h}
                    style={{ width: '100%', height: 12, borderRadius: 2 }}
                    pointerProps={pointerProps}
                    bgProps={{
                      style: { borderRadius: 2 },
                    }}
                    onChange={(newHue) => {
                      handleChange({ ...hsva, ...newHue });
                    }}
                  />
                )}
                {showAlpha == true && (
                  <Alpha
                    hsva={hsva}
                    style={{ marginTop: 6, height: 12, borderRadius: 2 }}
                    pointerProps={pointerProps}
                    bgProps={{
                      style: { borderRadius: 2 },
                    }}
                    onChange={(newAlpha) => {
                      handleChange({ ...hsva, ...newAlpha });
                    }}
                  />
                )}
              </div>
            </div>
            {showEditableInput && (
              <div style={{ display: 'flex', alignItems: 'flex-start', padding: '0 15px 15px 15px', userSelect: 'none' }}>
                <div style={{ flex: 1 }}>
                  {type == ChromeInputType.RGBA && (
                    <EditableInputRGBA
                      hsva={hsva}
                      rProps={{ labelStyle, inputStyle }}
                      gProps={{ labelStyle, inputStyle }}
                      bProps={{ labelStyle, inputStyle }}
                      aProps={showAlpha == false ? false : { labelStyle, inputStyle }}
                      onChange={(reColor) => handleChange(reColor.hsva)}
                    />
                  )}
                  {type === ChromeInputType.HEXA && (
                    <EditableInput
                      label="HEX"
                      labelStyle={labelStyle}
                      inputStyle={inputStyle}
                      value={
                        hsva.a > 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 && (
                    <EditableInputHSLA
                      hsva={hsva}
                      hProps={{ labelStyle, inputStyle }}
                      sProps={{ labelStyle, inputStyle }}
                      lProps={{ labelStyle, inputStyle }}
                      aProps={showAlpha == false ? false : { labelStyle, inputStyle }}
                      onChange={(reColor) => handleChange(reColor.hsva)}
                    />
                  )}
                </div>
                <Arrow onClick={handleClick} />
              </div>
            )}
          </div>
        </Fragment>
      }
      rectRender={() => <Fragment />}
    />
  );
});
 
Chrome.displayName = 'Chrome';
 
export default Chrome;