Skip to content

Commit

Permalink
Add custom accessible color generator based on contrast
Browse files Browse the repository at this point in the history
  • Loading branch information
juliewongbandue committed Sep 20, 2023
1 parent 349a12a commit 96fe364
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/components/Button/Button.style.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import styled, { css } from 'styled-components';
import { rgba, rem, tint, shade, em, readableColor } from 'polished';
import { rgba, rem, tint, shade, em } from 'polished';

import { borderRadii } from './Button.config';
import { FeaturedIcon } from './FeaturedIcon';

import { a11yColor } from '../../themes';
import { a11yColor, generateAccessibleTextColor } from '../../themes';
import { white, black, blue, violet } from '../../color';
import { core } from '../../tokens';

Expand Down Expand Up @@ -203,13 +203,12 @@ function deriveButtonColor(customColor, format, theme) {
let hoverColor: string;
let activeColor: string;
let textColor: string;

if (customColor) {
if (typeof customColor === 'string') {
color = customColor;
hoverColor = tint(0.15, color);
activeColor = shade(0.15, color);
textColor = readableColor(color);
textColor = generateAccessibleTextColor(color);
} else if (customColor.color) {
color = customColor.color;
hoverColor = customColor.hover
Expand All @@ -220,7 +219,7 @@ function deriveButtonColor(customColor, format, theme) {
: shade(0.15, color);
textColor = customColor.textColor
? customColor.textColor
: readableColor(color);
: generateAccessibleTextColor(customColor.color);
}
} else {
color = theme.formats[format];
Expand Down
12 changes: 11 additions & 1 deletion src/themes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rgba } from 'polished';
import { rgba, getContrast, parseToRgb, toColorString } from 'polished';

Check failure on line 1 in src/themes/index.ts

View workflow job for this annotation

GitHub Actions / typecheck

'parseToRgb' is declared but its value is never read.

Check failure on line 1 in src/themes/index.ts

View workflow job for this annotation

GitHub Actions / typecheck

'toColorString' is declared but its value is never read.

Check failure on line 1 in src/themes/index.ts

View workflow job for this annotation

GitHub Actions / publish

'parseToRgb' is declared but its value is never read.

Check failure on line 1 in src/themes/index.ts

View workflow job for this annotation

GitHub Actions / publish

'toColorString' is declared but its value is never read.
import { grayscale, white, black, slate, blue, red, green } from '../color';
import { readableColor, lighten, darken, getLuminance, saturate } from 'polished';

Expand Down Expand Up @@ -104,6 +104,16 @@ export const themes: {
},
};

export function generateAccessibleTextColor(backgroundColor) {
// Determine the contrast ratio
const contrastRatio = getContrast(backgroundColor, readableColor(backgroundColor));

// Choose a suitable text color based on the contrast ratio
const textColor = contrastRatio >= 4.5 ? black : white;

return textColor;
}

export function a11yColor(color) {
return ({ theme }) => (theme.a11y.contrast ? readableColor(lighten(0.1, color)) : readableColor(darken(0.3, color)));
}
Expand Down

0 comments on commit 96fe364

Please sign in to comment.