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

Feature: Color contrast checker #131

Merged
merged 9 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
39 changes: 39 additions & 0 deletions __TESTS__/colors.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,43 @@ describe('Colors', () => {
expect(hsl).toHaveValue('333, 55, 51');
expect(hex).toHaveValue('C9397B');
});

it('reacts to foreground color change correctly', async () => {
const user = userEvent.setup();
render(<Colors />);

const fg = screen.getByLabelText('colors:foreground');

await user.clear(fg);
await user.type(fg, '232c34');
expect(fg).toHaveValue('232c34');
});

it('reacts to background color change correctly', async () => {
const user = userEvent.setup();
render(<Colors />);

const bg = screen.getByLabelText('colors:background');

await user.clear(bg);
await user.type(bg, '232c34');
expect(bg).toHaveValue('232c34');
});

it('reacts to color swap correctly', async () => {
const user = userEvent.setup();
render(<Colors />);

const bg = screen.getByLabelText('colors:background');
const fg = screen.getByLabelText('colors:foreground');
const swap = screen.getByLabelText('colors:swapColors');

await user.clear(fg);
await user.clear(bg);
await user.type(fg, '232c34');
await user.type(bg, 'c9397b');
await userEvent.click(swap);
expect(fg).toHaveValue('c9397b');
expect(bg).toHaveValue('232c34');
});
});
264 changes: 264 additions & 0 deletions components/colors/ContrastPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
import DoneIcon from '@mui/icons-material/Done';
import { Stack, Typography } from '@mui/material';
import Box from '@mui/material/Box';
import Chip from '@mui/material/Chip';
import Grid from '@mui/material/Grid';
import { styled } from '@mui/material/styles';
import React from 'react';
import { useTranslation } from 'react-i18next';

export default function ContrastPreview({
contrast,
foreground,
background,
}: {
contrast: string;
foreground: string;
background: string;
}) {
const { t } = useTranslation(['colors', 'common']);

const contrastValue = parseFloat(contrast) || 0;

const AANormal = contrastValue >= 4.5;
const AALarge = contrastValue >= 3;
const AAGui = contrastValue >= 3;
const AAANormal = contrastValue >= 7;
const AAALarge = contrastValue >= 4.5;

const TextInput = styled('input')({
border: '2px solid',
borderColor: foreground,
color: '#fff',
padding: '5px',
backgroundColor: '#121212',
});

return (
<Box
width='700px'
maxWidth='100%'
display='flex'
flexDirection={{ xs: 'column', md: 'row' }}
justifyContent='center'
alignItems={{ xs: 'center', md: 'flex-start' }}
sx={{ flexGrow: 1 }}
>
<Grid
container
spacing={4}
mb={4}
>
<Grid
item
xs={12}
>
<Typography
align='left'
variant='h5'
borderBottom='1px solid #494949'
>
{t('colors:normalText')}
</Typography>
</Grid>
<Grid
item
xs={12}
md={4}
>
<Box
display='flex'
flexDirection='column'
gap={1}
padding='2px 0px'
>
<Stack
direction='row'
justifyContent='space-between'
alignItems='center'
spacing={2}
>
<Typography align='left'>{t('colors:wcagAA')}</Typography>
<Chip
label={AANormal ? t('colors:pass') : t('colors:fail')}
color={AANormal ? 'success' : 'error'}
size='small'
/>
</Stack>
<Stack
direction='row'
alignItems='center'
justifyContent='space-between'
spacing={2}
>
<Typography align='left'>
{t('colors:wcagAAA')}
</Typography>
<Chip
label={AAANormal ? t('colors:pass') : t('colors:fail')}
color={AAANormal ? 'success' : 'error'}
size='small'
/>
</Stack>
</Box>
</Grid>
<Grid
item
xs={12}
md={8}
>
<Box
border='1px solid #494949'
borderRadius='4px'
bgcolor={background}
color={foreground}
display='flex'
justifyContent='center'
gap={1}
padding='22px'
>
<Typography>{t('colors:contrastTestString')}</Typography>
</Box>
</Grid>
<Grid
item
xs={12}
>
<Typography
align='left'
variant='h5'
borderBottom='1px solid #494949'
>
{t('colors:largeText')}
</Typography>
</Grid>
<Grid
item
xs={12}
md={4}
>
<Box
display='flex'
flexDirection='column'
gap={1}
padding='2px 0px'
>
<Stack
direction='row'
justifyContent='space-between'
alignItems='center'
spacing={2}
>
<Typography align='left'>{t('colors:wcagAA')}</Typography>
<Chip
label={AALarge ? t('colors:pass') : t('colors:fail')}
color={AALarge ? 'success' : 'error'}
size='small'
/>
</Stack>
<Stack
direction='row'
alignItems='center'
justifyContent='space-between'
spacing={2}
>
<Typography align='left'>
{t('colors:wcagAAA')}
</Typography>
<Chip
label={AAALarge ? t('colors:pass') : t('colors:fail')}
color={AAALarge ? 'success' : 'error'}
size='small'
/>
</Stack>
</Box>
</Grid>
<Grid
item
xs={12}
md={8}
>
<Box
border='1px solid #494949'
borderRadius='4px'
bgcolor={background}
color={foreground}
display='flex'
justifyContent='center'
gap={1}
padding='22px'
>
<Typography
fontSize={18}
fontWeight='bold'
>
{t('colors:contrastTestString')}
</Typography>
</Box>
</Grid>
<Grid
item
xs={12}
>
<Typography
align='left'
variant='h5'
borderBottom='1px solid #494949'
>
{t('colors:gui')}
</Typography>
</Grid>
<Grid
item
xs={12}
md={4}
>
<Box
display='flex'
flexDirection='column'
gap={1}
padding='2px 0px'
>
<Stack
direction='row'
justifyContent='space-between'
alignItems='center'
spacing={2}
>
<Typography align='left'>{t('colors:wcagAA')}</Typography>
<Chip
label={AAGui ? t('colors:pass') : t('colors:fail')}
color={AAGui ? 'success' : 'error'}
size='small'
/>
</Stack>
</Box>
</Grid>
<Grid
item
xs={12}
md={8}
>
<Box
border='1px solid #494949'
borderRadius='4px'
bgcolor={background}
color={foreground}
display='flex'
flexDirection='column'
justifyContent='center'
alignItems='center'
gap={1}
padding='12px 22px 22px'
>
<DoneIcon />
<TextInput
name='uiCheck'
defaultValue={t('colors:textInput')}
/>
</Box>
</Grid>
</Grid>
</Box>
);
}
Loading