All files / packages/design-system/ui/components/color-picker index.jsx

97.95% Statements 48/49
93.33% Branches 28/30
100% Functions 3/3
97.95% Lines 48/49

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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350                              1x                                                             1x           240x                                 10x 9x   9x                                                                                   8x 8x 8x   8x     224x                                             7x 7x 7x 7x 7x 7x 7x   7x                                                                                                                                                                           1x 9x                 1x 4x                         10x   9x       9x 9x 9x 9x       9x 9x       5x 5x       3x 3x       18x 18x       9x 9x 9x 9x     9x     9x     4x             2x   3x                 9x                               1x                
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
import React from 'react';
import _ from '../../shared/helpers';
import classNames from 'classnames';
 
// internal imports
import SvgIcon from '../../shared/svg-icon';
import { Button } from '../buttons/base/example';
import Tabs from '../tabs/index.react';
import { Popover } from '../popovers/base/example';
import { FormElement } from '../form-element';
import Input from '../input/';
 
const swatchColors = [
  '#e3abec',
  '#c2dbf7',
  '#9fd6ff',
  '#9de7da',
  '#9df0c0',
  '#fff099',
  '#fed49a',
  '#d073e0',
  '#86baf3',
  '#5ebbff',
  '#44d8be',
  '#3be282',
  '#ffe654',
  '#ffb758',
  '#bd35bd',
  '#5779c1',
  '#5ebbff',
  '#00aea9',
  '#3cba4c',
  '#f5bc25',
  '#f99221',
  '#580d8c',
  '#001970',
  '#0a2399',
  '#0b7477',
  '#0b6b50',
  '#b67e11',
  '#b85d0d'
];
 
const errorMessage = 'Please ensure value is correct';
 
/**
 * Swatch Subcomponent
 */
const Swatch = props => {
  return (
    <span
      key={_.uniqueId('swatch-')}
      className="slds-swatch"
      style={{ background: props.color }}
    >
      <span className="slds-assistive-text" aria-hidden={props.ariaHidden}>
        {props.color}
      </span>
    </span>
  );
};
 
/**
 * Summary Subcomponent
 */
export const ColorPickerSummary = props => {
  const { hasSummaryError } = props;
  const errorId = 'color-picker-summary-error';
 
  return (
    <FormElement
      formElementClassName="slds-color-picker__summary"
      labelContent="Choose Color"
      labelClassName="slds-color-picker__summary-label"
      inputId="color-picker-summary-input"
      hasError={hasSummaryError}
    >
      <Button
        className="slds-color-picker__summary-button slds-button_icon slds-button_icon-more"
        title="Choose Color"
      >
        <Swatch color="hsl(220, 46%, 55%)" suppressAssistiveText />
        <SvgIcon
          className="slds-button__icon slds-button__icon_small slds-m-left_xx-small"
          sprite="utility"
          symbol="down"
        />
        <span className="slds-assistive-text">
          Choose a color. Current color: #5679C0
        </span>
      </Button>
      <div className="slds-color-picker__summary-input">
        <Input
          id="color-picker-summary-input"
          defaultValue="#5679C0"
          aria-describedby={hasSummaryError ? errorId : null}
        />
      </div>
      {hasSummaryError ? (
        <p className="slds-form-error" id={errorId}>
          {errorMessage}
        </p>
      ) : null}
    </FormElement>
  );
};
 
/**
 * Swatches (list of Swatch elements) Subcomponent
 */
export const ColorPickerSwatches = props => {
  const { isMenuRole } = props;
  const swatchesRole = isMenuRole ? 'menu' : 'listbox';
  const linkRole = isMenuRole ? 'menuitem' : 'option';
 
  return (
    <ul className="slds-color-picker__swatches" role={swatchesRole} aria-label="Preset colors">
      {swatchColors.map((swatch, index) => (
        <li
          key={_.uniqueId('color-picker-swatch-')}
          className="slds-color-picker__swatch"
          role="presentation"
        >
          <a
            className="slds-color-picker__swatch-trigger"
            href="#"
            role={linkRole}
            tabIndex={index === 0 ? 0 : -1}
          >
            <Swatch color={swatch} index={index} />
          </a>
        </li>
      ))}
    </ul>
  );
};
 
/**
 * Custom Picker Subcomponent
 */
const ColorPickerCustom = props => {
  const rangeInputId = _.uniqueId('color-picker-input-range-');
  const hexInputId = _.uniqueId('color-picker-input-hex-');
  const rInputId = _.uniqueId('color-picker-input-r-');
  const gInputId = _.uniqueId('color-picker-input-g-');
  const bInputId = _.uniqueId('color-picker-input-b-');
  const { hasCustomError } = props;
  const customErrorId = 'color-picker-custom-error';
 
  return (
    <div className="slds-color-picker__custom">
      <p id="color-picker-instructions" className="slds-assistive-text">
        Use arrow keys to select a saturation and brightness, on an x and y
        axis.
      </p>
      <div
        className="slds-color-picker__custom-range"
        style={{ background: 'hsl(220, 100%, 50%)' }}
      >
        <button
          className="slds-color-picker__range-indicator"
          style={{ bottom: '45%', left: '46%' }}
          aria-live="assertive"
          aria-atomic="true"
          aria-describedby="color-picker-instructions"
        >
          <span className="slds-assistive-text">
            hex #5679C0
          </span>
        </button>
      </div>
 
      <div className="slds-color-picker__hue-and-preview">
        <label className="slds-assistive-text" htmlFor={rangeInputId}>
          Select Hue
        </label>
        <input
          type="range"
          className="slds-color-picker__hue-slider"
          min="0"
          max="360"
          defaultValue="208"
          id={rangeInputId}
        />
        <Swatch color="#5679C0" ariaHidden />
      </div>
 
      <div className="slds-color-picker__custom-inputs">
        <FormElement
          labelContent="Hex"
          formElementClassName="slds-color-picker__input-custom-hex"
          hasError={hasCustomError}
          inputId={hexInputId}
        >
          <Input
            id={hexInputId}
            defaultValue="#5679C0"
            aria-describedby={hasCustomError ? customErrorId : null}
          />
        </FormElement>
 
        <FormElement
          labelContent={<abbr title="Red">R</abbr>}
          inputId={rInputId}
        >
          <Input defaultValue="86" id={rInputId} />
        </FormElement>
 
        <FormElement
          labelContent={<abbr title="Green">G</abbr>}
          inputId={gInputId}
        >
          <Input defaultValue="121" id={gInputId} />
        </FormElement>
 
        <FormElement
          labelContent={<abbr title="blue">B</abbr>}
          inputId={bInputId}
        >
          <Input defaultValue="192" id={bInputId} />
        </FormElement>
      </div>
 
      {hasCustomError ? (
        <p className="slds-form-error" id={customErrorId}>
          {errorMessage}
        </p>
      ) : null}
    </div>
  );
};
 
/**
 * Footer Subcomponent
 */
const ColorPickerFooter = () => (
  <div className="slds-color-picker__selector-footer">
    <Button isNeutral>Cancel</Button>
    <Button isBrand>Done</Button>
  </div>
);
 
/**
 * Tabs Subcomponent
 */
const ColorPickerTabs = props => (
  <Tabs selectedIndex={props.selectedTabIndex}>
    <Tabs.Item title="Default" id="color-picker-default">
      <ColorPickerSwatches />
    </Tabs.Item>
 
    <Tabs.Item title="Custom" id="color-picker-custom">
      <ColorPickerCustom hasCustomError={props.hasCustomError} />
    </Tabs.Item>
  </Tabs>
);
 
class ColorPicker extends React.Component {
  constructor(props) {
    super();
 
    this.state = {
      selectedTabIndex: props.selectedTabIndex || 0
    };
 
    this.isFullFeatureMode = this.isFullFeatureMode.bind(this);
    this.isPredefinedMode = this.isPredefinedMode.bind(this);
    this.isCustomOnlyMode = this.isCustomOnlyMode.bind(this);
    this.isSwatchesOnlyMode = this.isSwatchesOnlyMode.bind(this);
  }
 
  isFullFeatureMode() {
    const { hasPredefined, hasCustom } = this.props;
    return !!(hasPredefined && hasCustom);
  }
 
  isPredefinedMode() {
    const { hasPredefined, hasCustom } = this.props;
    return !!(hasPredefined && !hasCustom);
  }
 
  isCustomOnlyMode() {
    const { hasPredefined, hasCustom } = this.props;
    return !!(!hasPredefined && hasCustom);
  }
 
  isSwatchesOnlyMode() {
    const { hasPredefined, hasCustom } = this.props;
    return !!(!hasPredefined && !hasCustom);
  }
 
  render() {
    const { selectedTabIndex } = this.state;
    const { isOpen, hasSummaryError, hasCustomError } = this.props;
    const popoverState = isOpen ? 'slds-show' : 'slds-hide';
    const colorPickerSummary = this.isSwatchesOnlyMode() ? null : (
      <ColorPickerSummary hasSummaryError={hasSummaryError} />
    );
    const footerContent = this.isSwatchesOnlyMode() ? null : (
      <ColorPickerFooter />
    );
    let colorPickerContent = null;
 
    if (this.isFullFeatureMode()) {
      colorPickerContent = (
        <ColorPickerTabs
          hasCustomError={hasCustomError}
          selectedTabIndex={selectedTabIndex}
        />
      );
    } else if (this.isPredefinedMode()) {
      colorPickerContent = <ColorPickerSwatches />;
    } else if (this.isCustomOnlyMode()) {
      colorPickerContent = (
        <ColorPickerCustom hasCustomError={hasCustomError} />
      );
    } else if (this.isSwatchesOnlyMode()) {
      colorPickerContent = (
        <ColorPickerTabs selectedTabIndex={selectedTabIndex} />
      );
    }
 
    return (
      <div className="slds-color-picker">
        {colorPickerSummary}
 
        <Popover
          title="Choose a color"
          className={classNames('slds-color-picker__selector', popoverState)}
          footer={footerContent}
        >
          {colorPickerContent}
        </Popover>
      </div>
    );
  }
}
 
ColorPicker.defaultProps = {
  selectedTabIndex: 0,
  isOpen: false,
  hasPredefined: true,
  hasCustom: true
};
 
export default ColorPicker;