Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Theme] - Change Primary Buttons colors for Secondary Theme #651

Merged
merged 9 commits into from
Dec 17, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { keyframes } from '@emotion/core';

import { ButtonTypeWithAction } from '../..';
import { ThemeColors, ThemeType } from '../../../../constants';
import { primaryButtonLoadingBackgroundColor } from '../../../../utils/themeStyles';

const statefulLoader = keyframes`
0% { opacity: 1; transform: translate3d(0, 0, 0) scale(1, 1); }
Expand All @@ -13,9 +14,7 @@ const statefulLoader = keyframes`
`;

const primaryLoadingStyles = (theme: ThemeType) => `
background-color: ${
theme.__type === 'primary' ? theme.COLORS.white : theme.COLORS.primary
};
background-color: ${primaryButtonLoadingBackgroundColor(theme)};
`;

const accentLoadingStyles = (buttonColor: ThemeColors) => `
Expand Down
17 changes: 6 additions & 11 deletions src/shared-components/button/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ import tinycolor from 'tinycolor2';
import { style as TYPOGRAPHY_STYLE } from '../typography';
import { ANIMATION, SPACER, ThemeColors, ThemeType } from '../../constants';
import { textColorsAssociatedWithColors } from './constants';
import {
primaryButtonFontColor,
primaryButtonBackgroundColor,
} from '../../utils/themeStyles';

import { ButtonTypeWithAction } from '.';

const primaryStyles = (buttonColor: ThemeColors, theme: ThemeType) => {
let backgroundColor = buttonColor;
// If it is not a custom color and the theme is secondary
if (
backgroundColor === theme.COLORS.primary &&
theme.__type === 'secondary'
) {
backgroundColor = theme.COLORS.secondary;
}

const fontColor =
theme.__type === 'primary' ? theme.COLORS.white : theme.COLORS.primary;
const backgroundColor = primaryButtonBackgroundColor(theme, buttonColor);
const fontColor = primaryButtonFontColor(theme);

return `
background-color: ${backgroundColor};
Expand Down
17 changes: 5 additions & 12 deletions src/shared-components/typography/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import round from 'lodash.round';

import { withDeprecationWarning } from '../../utils';
import { ThemeType } from '../../constants';

/**
* We use theme.FONTS.baseFont for all primary styles, but use a
* different secondary font for Display, Heading, and Title styles
*/
const setSecondaryHeadingFont = (theme: ThemeType) =>
theme.__type === 'secondary' ? `font-family: ${theme.FONTS.headerFont};` : '';
import {
setSecondaryHeadingFont,
setButtonStyleFontWeight,
} from '../../utils/themeStyles';

const displayStyle = (theme: ThemeType) => `
color: ${theme.COLORS.primary};
Expand Down Expand Up @@ -71,11 +68,7 @@ const buttonStyle = (theme: ThemeType) => `
color: ${theme.COLORS.primaryTint1};
font-size: ${theme.TYPOGRAPHY.fontSize.button};
line-height: ${round(20 / 12, 2)};
${
theme.__type === 'primary'
? `font-weight: ${theme.TYPOGRAPHY.fontWeight.bold};`
: ''
}
${setButtonStyleFontWeight(theme)}
letter-spacing: 1px;
text-transform: uppercase;
`;
Expand Down
31 changes: 31 additions & 0 deletions src/utils/themeStyles/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ThemeColors, ThemeType } from '../../constants';

export const primaryButtonFontColor = (theme: ThemeType) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! I wonder if we should add a comment at the top of the file that explains that any ternary that is checking theme.__type should probably be moved into a util function in this file? (With the one exception being the Icon component, as you left it).

This isn't not a blocking comment--thank you for this cleanup 🙂

theme.__type === 'primary' ? theme.COLORS.white : theme.COLORS.primary;

export const primaryButtonBackgroundColor = (
theme: ThemeType,
buttonColor: ThemeColors,
) => {
if (buttonColor === theme.COLORS.primary && theme.__type === 'secondary') {
return theme.COLORS.secondary;
}

// If buttonColor is not COLORS.primary then it is custom
return buttonColor;
};

export const primaryButtonLoadingBackgroundColor = (theme: ThemeType) =>
theme.__type === 'primary' ? theme.COLORS.white : theme.COLORS.primary;

/**
* We use theme.FONTS.baseFont for all primary styles, but use a
* different secondary font for Display, Heading, and Title styles
*/
export const setSecondaryHeadingFont = (theme: ThemeType) =>
theme.__type === 'secondary' ? `font-family: ${theme.FONTS.headerFont};` : '';

export const setButtonStyleFontWeight = (theme: ThemeType) =>
theme.__type === 'primary'
? `font-weight: ${theme.TYPOGRAPHY.fontWeight.bold};`
: '';