# All files / utils/styledHelpers controlStyles.js

100% Statements 39/39
91.3% Branches 21/23
100% Functions 6/6
100% Lines 38/38

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

    56x

    56x
    243x
    243x
    243x

    56x

    252x
    252x
    252x
    7x

    245x

    56x

    56x
    243x
    243x
    243x
    243x
    4x
    239x
    161x

    78x

    243x

    247x

    56x

    243x
    243x
    243x
    243x
    243x
    243x
    243x

    243x
    6x

    237x

    56x
    243x
    243x

    243x

     import { getGlobalStyles, getGlobalOverrides } from 'src/global-styles';
    import { DateFieldControlOverrides } from 'src/elements/forms/DateField/constants';
    import { resolveBorderRadius, resolveColor } from './utils';
    import { defaultControlStyles, defaultStylesBase } from './cssDefaults';
    import { colorCore } from './core';

    const { border: borderSchema, grid: gridSchema } = getGlobalStyles();

    export const controlColor = props => {
      const { bg, formStyle } = props;
      const nextBg = (formStyle === 'filled' && !bg) ? 'formControlFilledBg' : bg || defaultStylesBase.bg;
      return colorCore({ ...props, bg: nextBg });
    };

    export const focusStyles = props => {
      const {
        activeColor,
        formStyle,
      } = props;
      const focusColor = !activeColor ? resolveColor(defaultControlStyles.activeColor, props) : resolveColor(activeColor, props);
      if (formStyle === 'filled') {
        return `
          border-bottom: 2px solid ${focusColor};
          margin-bottom: -1px;
          + label { color: ${focusColor}; }
        `;
      }
      return `
        border-color: ${focusColor};
        box-shadow: 0 0 0 1px ${focusColor};
        + label { color: ${focusColor}; }
      `;
    };

    export const disabledStyle = `
      opacity: 0.3;
      ~ * { color: rgba(0,0,0,0.3); }
    `;

    const scaleFactor = props => {
      const { sm, lg } = props;
      const { borderRadius: { base: schemaBase, sm: schemaSm, lg: schemaLg } } = borderSchema;
      let value = schemaBase;
      if (lg) {
        value = schemaLg;
      } else if (sm) {
        value = schemaSm;
      } else {
        value = schemaBase;
      }
      return value;
    };

    export const getFinalFieldPadding = (padding, formStyle, labelText) => ((formStyle === 'filled' && labelText)
      ? `
        padding: ${Number.parseInt(padding, 10) * 1.5}rem ${padding} ${Number.parseInt(padding, 10) / 2}rem;
        .grape-ui-select__control,
        .DayPickerInput > input {
          margin: -${Number.parseInt(padding, 10) * 1.5}rem -${padding} -${Number.parseInt(padding, 10) / 2}rem;
          padding: ${Number.parseInt(padding, 10) * 1.5}rem ${padding} ${Number.parseInt(padding, 10) / 2}rem;
          + .DayPickerInput-OverlayWrapper {
            transform: translateX(-${padding}) translateY(calc(${Number.parseInt(padding, 10) / 2}rem + 2px));
          }
        }
        + label {
          background: transparent;
          line-height: 1;
          top: ${Number.parseInt(padding, 10) - 0.2}rem;
        }
      `
      : `
        padding: ${padding};
        .grape-ui-select__control,
        .DayPickerInput > input {
          margin: -${padding};
          padding: ${padding};
          + .DayPickerInput-OverlayWrapper {
            transform: translateX(-${padding}) translateY(calc(${padding} + 2px));
          }
        }
      `);

    const getFinalStyle = props => {
      const {
        activeColor,
        borderColor,
        formStyle,
        labelText,
        padding,
        placeholderColor,
      } = props;
      const scale = scaleFactor(props);
      const globalOverrides = getGlobalOverrides(props);
      const focusStyle = focusStyles({ activeColor, formStyle, ...globalOverrides });
      const resolvedBorderColor = resolveColor(borderColor, globalOverrides);
      const resolvedPlaceholderColor = resolveColor(placeholderColor, globalOverrides);
      const controlSharedStyle = `
        appearance: none;
        border-radius: ${resolveBorderRadius(props)};
        outline: 0;
        width: 100%;
        box-sizing: border-box;
        ${getFinalFieldPadding(padding, formStyle, labelText)}
        &[disabled] {
          ${disabledStyle}
        }
        &:focus, &:focus-within{ ${focusStyle} }
        &::placeholder,
        input::placeholder {
          color: ${resolvedPlaceholderColor}
        }
        ${DateFieldControlOverrides}
      `;
      if (formStyle === 'filled') {
        return `
          border: 0;
          border-bottom: 1px solid ${resolvedBorderColor};
          border-radius: ${scale} ${scale} 0 0;
          ${controlSharedStyle}
        `;
      }
      return `
        border: 1px solid ${resolvedBorderColor};
        border-radius: ${scale};
        ${controlSharedStyle}
      `;
    };

    export const controlStyles = (props = {}) => {
      let overrides = null;
      overrides = {
        ...defaultControlStyles,
        ...props,
        padding: gridSchema[props.gutter] || defaultControlStyles.padding,
      };
      return getFinalStyle({
        ...props,
        ...overrides,
      });
    };