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: Color Mode BoxShadow for cards #2849

Merged
merged 8 commits into from
Mar 29, 2024
94 changes: 57 additions & 37 deletions packages/gamut/src/Card/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { Background, system, theme, variant } from '@codecademy/gamut-styles';
import { StyleProps } from '@codecademy/variance';
import {
Background,
ColorModes,
system,
theme,
useColorModes,
variant,
} from '@codecademy/gamut-styles';
import styled from '@emotion/styled';
import { ComponentProps } from 'react';
import * as React from 'react';

import { Box } from '../Box';

const outlineStyles = {
const outlineStyles = (mode: ColorModes) => ({
boxShadow: `-5px 5px ${theme.colors['background-current']}, -5px 5px 0 1px ${theme.colors.black}`,
'&:hover': {
transform: 'translate(4px, -4px)',
boxShadow: `-8px 8px 0 currentColor`,
boxShadow: `-8px 8px 0 ${
mode === 'dark' ? theme.colors['background-current'] : 'currentColor'
}`,
},
};
});

const DynamicCardWrapper = styled(Box)<CardWrapperProps>(
const DynamicCardWrapper = styled(Box)<CardWrapperProps>(({ mode, ...props }) =>
variant({
prop: 'shadow',
base: {
Expand All @@ -35,56 +43,66 @@ const DynamicCardWrapper = styled(Box)<CardWrapperProps>(
boxShadow: `-8px 8px 0 currentColor`,
},
},
outline: outlineStyles,
outline: outlineStyles(mode),
},
})
})(props)
);

const shadowVariants = variant({
prop: 'shadow',
base: {
position: 'relative',
boxShadow: `0px 0px 0 ${theme.colors.navy}`,
transition: 'box-shadow 200ms ease, transform 200ms ease',
},
variants: {
small: {
'&:hover': {
transform: 'translate(2px, -2px)',
boxShadow: `-4px 4px 0 ${theme.colors.navy}`,
},
const shadowVariants = (mode: ColorModes) =>
variant({
prop: 'shadow',
base: {
position: 'relative',
boxShadow: `0px 0px 0 currentColor`,
transition: 'box-shadow 200ms ease, transform 200ms ease',
},
medium: {
'&:hover': {
transform: 'translate(4px, -4px)',
boxShadow: `-8px 8px 0 ${theme.colors.navy}`,
variants: {
small: {
'&:hover': {
transform: 'translate(2px, -2px)',
boxShadow: `-4px 4px 0 ${mode === 'dark' ? 'white' : 'currentColor'}`,
},
},
medium: {
'&:hover': {
transform: 'translate(4px, -4px)',
boxShadow: `-8px 8px 0 ${mode === 'dark' ? 'white' : 'currentColor'}`,
},
},
outline: outlineStyles(mode),
},
outline: outlineStyles,
},
});
});

export interface CardProps
extends Omit<ComponentProps<typeof CardWrapper>, 'outline' | 'bg'> {
variant?: 'navy' | 'white' | 'hyper' | 'yellow' | 'beige';
}

interface CardWrapperProps extends StyleProps<typeof shadowVariants> {
interface CardWrapperProps {
outline?: boolean;
mode: ColorModes;
shadow?: 'small' | 'medium' | 'outline';
Copy link
Contributor

Choose a reason for hiding this comment

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

seeing test failures in the monorepo here related to this line -maybe happening on initial load before useColorModes has a chance to return the mode? two solutions i can think of are 1. making light the default mode before if mode doesn't exist yet or 2. making mode not required here (tho having a default fallback might be safer)

}

const CardWrapper = styled(Background)<CardWrapperProps>(
shadowVariants,
system.states({
outline: {
'&:hover': {
outline: '1px solid currentColor',
({ mode, ...props }) => ({
...shadowVariants(mode)(props),
...system.states({
outline: {
'&:hover': {
outline: '1px solid currentColor',
},
},
},
})(props),
})
);

export const Card: React.FC<CardProps> = ({ variant, ...rest }) => {
// mode neeeds to be overwritten if it isn't passed in
const [mode] = useColorModes();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { mode: _, ...otherRest } = rest;

if (!variant) {
return (
<DynamicCardWrapper
Expand All @@ -93,7 +111,8 @@ export const Card: React.FC<CardProps> = ({ variant, ...rest }) => {
bg="background"
color="text"
p={16}
{...rest}
mode={mode}
{...otherRest}
/>
);
}
Expand All @@ -105,7 +124,8 @@ export const Card: React.FC<CardProps> = ({ variant, ...rest }) => {
borderColor="navy"
p={16}
outline={variant === 'navy'}
{...rest}
mode={mode}
{...otherRest}
/>
);
};
Loading