# All files / components/Spacing Spacing.js

0% Statements 0/31
0% Branches 0/25
0% Functions 0/8
0% Lines 0/25

    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

     import styled from 'styled-components';
    import PropTypes from 'prop-types';
    import get from 'extensions/themeGet';

    const normalize = size => {
      if (!size) {
        return 'large';
      }

      switch (size) {
        case 'xs':
        case 'extraSmall':
          return 'extraSmall';
        case 'sm':
        case 'small':
          return 'small';
        case 'lg':
        case 'large':
          return 'large';
        case 'xl':
        case 'extraLarge':
          return 'extraLarge';
        case 'md':
        case 'medium':
          return 'medium';
        default:
          return size;
      }
    };

    const getSpacing = (props, size) => {
      try {
        return get(`spacing.${normalize(size)}`)(props);
      } catch (err) {
        return size;
      }
    };

    const Spacing = styled.div`
      ${props => `padding: ${getSpacing(props, props.size)};`} ${props =>
      props.top ? `padding-top: ${getSpacing(props, props.top)};` : ''} ${props =>
      props.bottom
        ? `padding-bottom: ${getSpacing(props, props.bottom)};`
        : ''} ${props =>
      props.left
        ? `padding-left: ${getSpacing(props, props.left)};`
        : ''} ${props =>
      props.right ? `padding-right: ${getSpacing(props, props.right)};` : ''};
    `;

    Spacing.propTypes = {
      /**
       * The default size to be applied to all directions. [xs, sm, md, lg, xl] or a CSS dimension.
       */
      size: PropTypes.string,
      /**
       * The top spacing size. [xs, sm, md, lg, xl] or a CSS dimension.
       */
      top: PropTypes.string,
      /**
       * The bottom spacing size. [xs, sm, md, lg, xl] or a CSS dimension.
       */
      bottom: PropTypes.string,
      /**
       * The left spacing size. [xs, sm, md, lg, xl] or a CSS dimension.
       */
      left: PropTypes.string,
      /**
       * The right spacing size. [xs, sm, md, lg, xl] or a CSS dimension.
       */
      right: PropTypes.string,
      /**
       * Adds a class name to the element.
       */
      className: PropTypes.string,
      /**
       * Adds an id to the element.
       */
      id: PropTypes.string,
    };

    Spacing.defaultProps = {
      size: 'lg',
      top: null,
      bottom: null,
      left: null,
      right: null,
      className: null,
      id: null,
    };

    /**
     * @component
     */
    export default Spacing;