All files / checkbox styled-components.js

58.33% Statements 49/84
37.11% Branches 36/97
63.64% Functions 7/11
58.33% Lines 49/84

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                          18x 18x 18x   18x   18x     18x         17x   17x               16x 16x     1x 1x   17x 17x 17x                               18x 18x 18x 18x   18x               18x               18x                 18x 2x 16x 1x   15x           17x 17x 17x     4x 18x 18x                             4x 18x 18x   18x       18x                             18x                     18x       18x                                                                 4x 17x 17x 17x                     4x                   4x                                       4x                                                       4x                                          
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import {styled} from '../styles/index.js';
import {STYLE_TYPE} from './constants.js';
 
import type {SharedStylePropsT} from './types.js';
 
function getBorderColor(props) {
  const {$disabled, $checked, $isError, $isIndeterminate, $theme} = props;
  const {colors} = $theme;
  Iif ($disabled) {
    return colors.tickFillDisabled;
  } else Iif ($checked || $isIndeterminate) {
    return 'transparent';
  } else Iif ($isError) {
    return colors.negative400;
  } else {
    return colors.tickBorder;
  }
}
 
function getLabelPadding(props) {
  const {$labelPlacement = '', $theme} = props;
  let paddingDirection;
  switch ($labelPlacement) {
    case 'top':
      paddingDirection = 'Bottom';
      break;
    case 'bottom':
      paddingDirection = 'Top';
      break;
    case 'left':
      paddingDirection = 'Right';
      break;
    default:
    case 'right':
      paddingDirection = 'Left';
      break;
  }
  const {sizing} = $theme;
  const {scale300} = sizing;
  return {
    [`padding${paddingDirection}`]: scale300,
  };
}
 
function getBackgroundColor(props) {
  const {
    $disabled,
    $checked,
    $isIndeterminate,
    $isFocused,
    $isError,
    $isHovered,
    $isActive,
    $theme,
    $checkmarkType,
  } = props;
  const isToggle = $checkmarkType === STYLE_TYPE.toggle;
  const {colors} = $theme;
  Iif ($disabled) {
    return isToggle ? colors.sliderTrackFillDisabled : colors.tickFillDisabled;
  } else Iif ($isError && ($isIndeterminate || $checked)) {
    if ($isActive || $isFocused) {
      return colors.tickFillErrorSelectedHoverActive;
    } else if ($isHovered) {
      return colors.tickFillErrorSelectedHover;
    } else {
      return colors.tickFillErrorSelected;
    }
  } else Iif ($isError) {
    if ($isActive || $isFocused) {
      return colors.tickFillErrorHoverActive;
    } else if ($isHovered) {
      return colors.tickFillErrorHover;
    } else {
      return colors.tickFillError;
    }
  } else Iif ($isIndeterminate || $checked) {
    if ($isActive || $isFocused) {
      return colors.tickFillSelectedHoverActive;
    } else if ($isHovered) {
      return colors.tickFillSelectedHover;
    } else {
      return colors.tickFillSelected;
    }
  } else {
    if ($isActive || $isFocused) {
      return isToggle ? colors.sliderTrackFillActive : colors.tickFillActive;
    } else if ($isHovered) {
      return isToggle ? colors.sliderTrackFillHover : colors.tickFillHover;
    } else {
      return isToggle ? colors.sliderTrackFill : colors.tickFill;
    }
  }
}
 
function getLabelColor(props) {
  const {$disabled, $theme} = props;
  const {colors} = $theme;
  return $disabled ? colors.foregroundAlt : colors.foreground;
}
 
export const Root = styled<SharedStylePropsT>('label', props => {
  const {$disabled, $labelPlacement} = props;
  return {
    flexDirection:
      $labelPlacement === 'top' || $labelPlacement === 'bottom'
        ? 'column'
        : 'row',
    display: 'flex',
    alignItems:
      $labelPlacement === 'top' || $labelPlacement === 'bottom'
        ? 'center'
        : 'flex-start',
    cursor: $disabled ? 'not-allowed' : 'pointer',
    userSelect: 'none',
  };
});
 
export const Checkmark = styled<SharedStylePropsT>('span', props => {
  const {$checked, $disabled, $isIndeterminate, $theme} = props;
  const {sizing, animation} = $theme;
 
  const tickColor = $disabled
    ? $theme.colors.tickMarkFillDisabled
    : $theme.colors.tickMarkFill;
 
  const indeterminate = encodeURIComponent(`
    <svg width="12" height="2" viewBox="0 0 12 2" fill="none" xmlns="http://www.w3.org/2000/svg">
    <line
      x1="1"
      y1="-1"
      x2="11"
      y2="-1"
      transform="translate(0 2)"
      stroke="${tickColor}"
      stroke-width="2"
      stroke-linecap="round"
    />
    </svg>
  `);
 
  const check = encodeURIComponent(`
    <svg width="11" height="10" viewBox="0 0 11 10" fill="none" xmlns="http://www.w3.org/2000/svg">
      <path
        fill-rule="evenodd"
        clip-rule="evenodd"
        d="M10.6 0.200059C11.0418 0.53143 11.1314 1.15823 10.8 1.60006L4.8 9.60006C4.62607 9.83197 4.36005 9.97699 4.07089 9.99754C3.78173 10.0181 3.49788 9.91215 3.29289 9.70717L0.292893 6.70717C-0.0976311 6.31664 -0.0976311 5.68348 0.292893 5.29295C0.683417 4.90243 1.31658 4.90243 1.70711 5.29295L3.89181 7.47765L9.2 0.400059C9.53137 -0.0417689 10.1582 -0.131312 10.6 0.200059Z"
        fill="${tickColor}"
      />
    </svg>
  `);
 
  const borderRadius = $theme.borders.useRoundedCorners
    ? $theme.borders.radius200
    : null;
 
  return ({
    flex: '0 0 auto',
    transitionDuration: animation.timing100,
    transitionTimingFunction: animation.easeOutCurve,
    width: sizing.scale700,
    height: sizing.scale700,
    left: '4px',
    top: '4px',
    boxSizing: 'border-box',
    borderStyle: 'solid',
    borderWidth: '2px',
    borderColor: getBorderColor(props),
    borderTopLeftRadius: borderRadius,
    borderTopRightRadius: borderRadius,
    borderBottomRightRadius: borderRadius,
    borderBottomLeftRadius: borderRadius,
    display: 'inline-block',
    verticalAlign: 'middle',
    backgroundImage: $isIndeterminate
      ? `url('data:image/svg+xml,${indeterminate}');`
      : $checked
        ? `url('data:image/svg+xml,${check}');`
        : null,
    backgroundColor: getBackgroundColor(props),
    backgroundRepeat: 'no-repeat',
    backgroundPosition: 'center',
    marginTop: $theme.sizing.scale0,
    marginBottom: $theme.sizing.scale0,
    marginLeft: $theme.sizing.scale0,
    marginRight: $theme.sizing.scale0,
  }: {});
});
 
export const Label = styled<SharedStylePropsT>('div', props => {
  const {$theme, $checkmarkType} = props;
  const {typography} = $theme;
  return ({
    flex: $checkmarkType === STYLE_TYPE.toggle ? 'auto' : null,
    verticalAlign: 'middle',
    ...getLabelPadding(props),
    color: getLabelColor(props),
    ...typography.font350,
    lineHeight: '24px',
  }: {});
});
 
// tricky style for focus event cause display: none doesn't work
export const Input = styled('input', {
  opacity: 0,
  width: 0,
  height: 0,
  overflow: 'hidden',
  margin: 0,
  padding: 0,
  position: 'absolute',
});
 
export const Toggle = styled<SharedStylePropsT>('div', ({$theme}) => {
  const borderRadius = $theme.borders.useRoundedCorners
    ? $theme.borders.radius200
    : null;
  return ({
    ...$theme.borders.border300,
    alignItems: 'center',
    backgroundColor: $theme.colors.mono100,
    borderTopLeftRadius: borderRadius,
    borderTopRightRadius: borderRadius,
    borderBottomRightRadius: borderRadius,
    borderBottomLeftRadius: borderRadius,
    boxShadow: $theme.lighting.shadow400,
    display: 'flex',
    justifyContent: 'center',
    height: $theme.sizing.scale800,
    width: $theme.sizing.scale800,
  }: {});
});
 
export const ToggleInner = styled<SharedStylePropsT>('div', props => {
  function backgroundColor() {
    if (props.$disabled) {
      return props.$theme.colors.sliderHandleInnerFillDisabled;
    }
 
    if (props.$isActive && props.$checked) {
      return props.$theme.colors.sliderHandleInnerFillSelectedActive;
    }
 
    if (props.$isHovered && props.$checked) {
      return props.$theme.colors.sliderHandleInnerFillSelectedHover;
    }
 
    return props.$theme.colors.sliderHandleInnerFill;
  }
 
  return {
    height: props.$theme.sizing.scale300,
    width: props.$theme.sizing.scale0,
    borderTopLeftRadius: props.$theme.borders.radius100,
    borderTopRightRadius: props.$theme.borders.radius100,
    borderBottomRightRadius: props.$theme.borders.radius100,
    borderBottomLeftRadius: props.$theme.borders.radius100,
    backgroundColor: backgroundColor(),
  };
});
 
export const ToggleTrack = styled<SharedStylePropsT>('div', props => {
  const borderRadius = props.$theme.borders.useRoundedCorners
    ? props.$theme.borders.radius200
    : null;
  return ({
    alignItems: 'center',
    backgroundColor: getBackgroundColor(props),
    borderTopLeftRadius: borderRadius,
    borderTopRightRadius: borderRadius,
    borderBottomRightRadius: borderRadius,
    borderBottomLeftRadius: borderRadius,
    display: 'flex',
    height: props.$theme.sizing.scale600,
    justifyContent: props.$checked ? 'flex-end' : 'flex-start',
    marginTop: props.$theme.sizing.scale100,
    marginBottom: props.$theme.sizing.scale100,
    marginLeft: props.$theme.sizing.scale100,
    marginRight: props.$theme.sizing.scale100,
    width: props.$theme.sizing.scale1000,
  }: {});
});