All files / src/utils useStyle.ts

95.65% Statements 66/69
88.89% Branches 32/36
100% Functions 12/12
95.59% Lines 65/68

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                  75x 75x 75x                                     75x                                             75x 75x   75x               3x 3x 3x           24x 24x 23x   1x       321x 321x 301x   20x       317x 317x 310x   7x       48x 48x 48x           15x 15x 15x           7655x   7655x 7655x 7655x 7655x 1157x 1157x 8x   1157x     7655x 1161x 1161x 1099x   1161x 5x 5x               1156x 1106x 1106x 3x   1106x 24x   1106x 321x   1106x 317x   1106x 48x   1106x 15x   1106x   1102x         4x                     1156x           7655x       7650x 7650x    
import * as React from 'react';
import _get from 'lodash/get';
import _kebabCase from 'lodash/kebabCase';
 
import { ThemeContext, css } from '../styled';
import { border, borderRadius, breakpoint, fontSize, palette, space, fontWeight } from './theme';
 
import { cssProps as cssPropsMap, pickCSSProps } from './cssProps';
 
const borderAttributes = ['border'];
const borderRadiusAttributes = ['borderRadius'];
const colorAttributes = [
  'color',
  'backgroundColor',
  'borderBlockEndColor',
  'borderBlockStartColor',
  'borderBottomColor',
  'borderColor',
  'borderInlineEndColor',
  'borderInline-startColor',
  'borderLeftColor',
  'borderRightColor',
  'borderTopColor',
  'borderBottomColor',
  'caretColor',
  'columnRuleColor',
  'outlineColor',
  'textDecorationColor',
  'textEmphasisColor'
];
const spaceAttributes = [
  'margin',
  'marginLeft',
  'marginRight',
  'marginTop',
  'marginBottom',
  'marginX',
  'marginY',
  'padding',
  'paddingLeft',
  'paddingRight',
  'paddingTop',
  'paddingBottom',
  'paddingX',
  'paddingY',
  'top',
  'left',
  'bottom',
  'right',
  'grid-gap',
  'grid-column-gap',
  'grid-row-gap'
];
const fontSizeAttributes = ['fontSize'];
const fontWeightAttributes = ['fontWeight'];
 
const attributeMaps = {
  marginY: ['marginTop', 'marginBottom'],
  paddingY: ['paddingTop', 'paddingBottom'],
  marginX: ['marginLeft', 'marginRight'],
  paddingX: ['paddingLeft', 'paddingRight']
};
 
function getBorderValue({ theme, value }) {
  const borderValue = border(value)({ theme });
  Eif (borderValue) {
    return `${borderValue.width} solid ${borderValue.color}`;
  }
  return value;
}
 
function getBorderRadiusValue({ theme, value }) {
  const borderRadiusValue = borderRadius(value)({ theme });
  if (borderRadiusValue) {
    return borderRadiusValue;
  }
  return value;
}
 
function getColorValue({ theme, value }) {
  const color = palette(value)({ theme });
  if (color) {
    return color;
  }
  return value;
}
 
function getSpaceValue({ theme, value }) {
  const spacing = space(value)({ theme });
  if (spacing) {
    return `${spacing}rem`;
  }
  return value;
}
 
function getFontSizeValue({ theme, value }) {
  const size = fontSize(value)({ theme });
  Eif (size) {
    return `${size}rem`;
  }
  return value;
}
 
function getFontWeightValue({ theme, value }) {
  const weight = fontWeight(value)({ theme });
  Eif (weight) {
    return weight;
  }
  return value;
}
 
function getStyleFromProps(props, theme) {
  const cssProps = pickCSSProps(props);
 
  let style = { ...cssProps };
  Eif (style) {
    let styleEntries = Object.entries(style);
    styleEntries = styleEntries.reduce((prevStyle, [attribute, value]) => {
      let entries = [[attribute, value]];
      if (attributeMaps[attribute]) {
        entries = attributeMaps[attribute].map(attribute => [attribute, value]);
      }
      return [...prevStyle, ...entries];
    }, []);
 
    style = styleEntries.reduce((prevStyle, [attribute, value]) => {
      let newValue = value;
      if (typeof newValue === 'string') {
        newValue = { default: value };
      }
      if (attribute.includes('_')) {
        const pseudoSelector = cssPropsMap[attribute];
        return css`
          ${prevStyle};
 
          ${pseudoSelector} {
            ${getStyleFromProps(value, theme)}
          }
        `;
      }
      const newStyle = Object.entries(newValue || {}).reduce((prevStyle, [bp, value]) => {
        let newValue = value;
        if (borderAttributes.includes(attribute)) {
          newValue = getBorderValue({ theme, value });
        }
        if (borderRadiusAttributes.includes(attribute)) {
          newValue = getBorderRadiusValue({ theme, value });
        }
        if (colorAttributes.includes(attribute)) {
          newValue = getColorValue({ theme, value });
        }
        if (spaceAttributes.includes(attribute)) {
          newValue = getSpaceValue({ theme, value });
        }
        if (fontSizeAttributes.includes(attribute)) {
          newValue = getFontSizeValue({ theme, value });
        }
        if (fontWeightAttributes.includes(attribute)) {
          newValue = getFontWeightValue({ theme, value });
        }
        if (bp === 'default') {
          // @ts-ignore
          return css`
            ${prevStyle}
            ${_kebabCase(attribute)}: ${newValue} !important;
          `;
        }
        return css`
          ${prevStyle};
          ${breakpoint(
            bp,
            // @ts-ignore
            css`
              ${_kebabCase(attribute)}: ${newValue} !important;
            `
          )({ theme })};
        `;
      }, css``);
      return css`
        ${prevStyle} ${newStyle};
      `;
    }, css``);
  }
 
  return style;
}
 
export function useStyle(props) {
  const theme = React.useContext(ThemeContext);
  return getStyleFromProps(props, theme);
}