All files / utils/styledHelpers/utils buttonThemes.js

100% Statements 24/24
100% Branches 14/14
100% Functions 5/5
100% Lines 22/22

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      56x   19x 19x   19x 1x   18x 96x 96x 96x             96x 30x       96x 30x       96x     56x 7x 7x   7x   14x         3x   2x         1x    
import { getGlobalStyles } from 'src/global-styles';
import { resolveColor } from './resolvers';
 
export const getButtonThemesByVariant = (type, globalOverrides) => {
  // return all themes of that type.
  const { buttonVariant: buttonVariantSchema } = globalOverrides;
  const variantThemes = buttonVariantSchema[type];
 
  if (!variantThemes) {
    return {};
  }
  return Object.keys(variantThemes).reduce((prev, current) => {
    const variantName = type !== 'plain' ? `${type}-${current}` : current;
    const currentTheme = variantThemes[current];
    const vTheme = {
      ...prev,
      [variantName]: {
        backgroundColor: resolveColor(currentTheme.backgroundColor, globalOverrides, null),
        color: resolveColor(currentTheme.color, globalOverrides, null),
      },
    };
    if (variantThemes[current].focusColor) {
      vTheme[variantName]['&:focus'] = {
        backgroundColor: resolveColor(currentTheme.focusColor, globalOverrides, null),
      };
    }
    if (variantThemes[current].hoverColor) {
      vTheme[variantName]['&:hover'] = {
        backgroundColor: resolveColor(currentTheme.hoverColor, globalOverrides, null),
      };
    }
    return vTheme;
  }, {});
};
export const buttonThemes = (variantTypes, globalOverrides) => {
  const globalStyles = globalOverrides || getGlobalStyles();
  const { buttonVariant: buttonVariantSchema } = globalStyles;
 
  if (!variantTypes) {
    // return all button variants
    return Object.keys(buttonVariantSchema).reduce((prev, current) => ({
      ...prev,
      ...getButtonThemesByVariant(current, globalStyles),
    }), {});
  }
  if (Array.isArray(variantTypes)) {
    // return button variants based on indicated types.
    return variantTypes.reduce((prev, current) => ({
      ...prev,
      ...getButtonThemesByVariant(current, globalStyles),
    }), {});
  }
  return null;
};