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

Fix: ensuring we don't fail on button styles for old themes #825

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createGlobalStyle, css } from 'styled-components';
import React from 'react';
import { createGlobalStyle, css, useTheme } from 'styled-components';
import { globalAnchorCss } from '../../navigation/Anchor';
import { globalHeadingsCss } from '../../containers/heading/Heading';
import { globalButtonCss } from '../../controls/buttons/Button';
Expand Down Expand Up @@ -29,8 +30,12 @@ export const globalStyleDefaults = css`
${globalDrowdownCss}
`;

export const GlobalStyleDefaults = createGlobalStyle`
export const GlobalStyles = createGlobalStyle`
${globalStyleDefaults}
`;

export default GlobalStyleDefaults;
export default function GlobalStyleDefaults() {
const theme = useTheme();

return theme?.font ? <GlobalStyles /> : <></>;
}
40 changes: 20 additions & 20 deletions packages/es-components/src/components/controls/buttons/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,15 @@ export const globalButtonCss = css`
${({ theme }) =>
getButtonCss({
theme,
colors: getButtonColors(
theme,
false,
theme.buttonStyles.button.variant.default as GuaranteedButtonVariant
),
colors: getButtonColors(theme, false, 'default'),
borderRadii: getBorderRadii(theme.buttonStyles.button.size.default),
buttonSize: theme.buttonStyles.button.size.default
})}
${({ theme }) =>
buttonVariantStyleTypes.map(style =>
buttonSizes.map(size => {
const buttonSize = theme.buttonStyles.button.size[size];
const variant = theme.buttonStyles.button.variant[
style
] as GuaranteedButtonVariant;
const buttonColors = getButtonColors(theme, false, variant);
const buttonColors = getButtonColors(theme, false, style);
const borderRadii = getBorderRadii(buttonSize);
return css`
&.${[style, size].join('.')} {
Expand All @@ -201,6 +194,12 @@ export const globalButtonCss = css`
}
`;

export const buttonStyleTypes = [
...buttonVariantStyleTypes,
'inherited'
] as const;
export type ButtonStyleType = (typeof buttonStyleTypes)[number];

const buttonColorProps = [
'bgColor',
'textColor',
Expand All @@ -223,10 +222,19 @@ type GuaranteedButtonVariant = Omit<ButtonVariant, 'bgColor'> & {
bgColor: NonNullable<CSS.Property.BackgroundColor>;
};

function getButtonVariant(
theme: DefaultTheme,
buttonVariant: ButtonStyleType
): GuaranteedButtonVariant {
const variant = theme.buttonStyles.button.variant[buttonVariant] || {};
variant.bgColor = variant.bgColor || theme.colors.gray4;
return variant;
}

function getButtonColors<T extends boolean>(
theme: DefaultTheme,
isInheritedStyle: T,
buttonVariant?: T extends false ? GuaranteedButtonVariant : undefined
buttonVariant: ButtonStyleType = 'default'
): ButtonColors {
const baseButtonColors: ButtonColors = {
bgColor: 'inherited',
Expand All @@ -245,7 +253,7 @@ function getButtonColors<T extends boolean>(
return baseButtonColors;
}

const variant = buttonVariant as GuaranteedButtonVariant;
const variant = getButtonVariant(theme, buttonVariant);

const focusBoxShadowColor = tinycolor
.mix(variant.bgColor, theme.colors.black, 14)
Expand Down Expand Up @@ -279,11 +287,6 @@ function getButtonColors<T extends boolean>(
return calculatedButtonColors;
}

export const buttonStyleTypes = [
...buttonVariantStyleTypes,
'inherited'
] as const;
export type ButtonStyleType = (typeof buttonStyleTypes)[number];
export type ButtonProps = ButtonBaseProps & {
size?: ButtonSize;
styleType?: ButtonStyleType;
Expand All @@ -308,9 +311,6 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(function Button(
) {
const theme = useTheme();
const buttonSize = theme.buttonStyles.button.size[size];
const variant = theme.buttonStyles.button.variant[
styleType
] as GuaranteedButtonVariant;
const isInheritedStyle = styleType === 'inherited';
const mobileBlockSetting =
flatLeftEdge || flatRightEdge ? false : mobileBlock;
Expand All @@ -323,7 +323,7 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(function Button(
bottomLeft: flatLeftEdge ? 0 : defaultRadius
};

const buttonColors = getButtonColors(theme, isInheritedStyle, variant);
const buttonColors = getButtonColors(theme, isInheritedStyle, styleType);

return (
<StyledButton
Expand Down
Loading