Skip to content
This repository has been archived by the owner on Mar 14, 2021. It is now read-only.

Display wcag accessibility rating for colors #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"clipboard-copy": "^3.0.0",
"colorable": "^1.0.5",
"lodash": "^4.17.11",
"polished": "^3.4.0",
"prop-types": "^15.7.2",
Expand Down
40 changes: 38 additions & 2 deletions src/components/ColorSwatch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,44 @@
import { jsx } from "theme-ui";
import React from "react";
import { readableColor } from "polished";
import { Swatch, SwatchToken, SwatchValue } from "../";
import colorable from "colorable";
import { Swatch, SwatchToken, SwatchValue, SwatchDetails } from "../";
import { tokenPropType, valuePropType } from "../propTypes";

const getBestAccessibility = access => {
if (access.aaa) {
return "AAA";
}
if (access.aa) {
return "AA";
}
if (access.aaaLarge) {
return "AAA Large";
}
if (access.aaLarge) {
return "AA Large";
}

return undefined;
};

export const ColorSwatch = ({ value, token }) => {
const color = readableColor(value, "black", "white");
const black = "black";
const white = "white";
const color = readableColor(value, black, white);
const accessibility = colorable({
main: value,
black,
white
});
const { values, combinations } = accessibility.find(
result => result.name === "main"
);
const access = combinations
.map(combination => getBestAccessibility(combination.accessibility))
.filter(Boolean)
.join(", ");

return (
<Swatch token={token} value={value}>
<div
Expand All @@ -19,6 +52,9 @@ export const ColorSwatch = ({ value, token }) => {
>
<SwatchToken color={color}>{token}</SwatchToken>
<SwatchValue color={color}>{value}</SwatchValue>
<SwatchDetails color={color} css={{ textAlign: "right" }}>
{access}
</SwatchDetails>
</div>
</Swatch>
);
Expand Down
21 changes: 21 additions & 0 deletions src/components/SwatchDetails.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* @jsx jsx */
import { jsx } from "theme-ui";
import React from "react";

function SwatchDetails({ color = "text", css: componentCSS, ...rest }) {
return (
<p
sx={{
m: 0,
fontFamily: "body",
fontSize: "xs",
fontWeight: "normal",
color,
...componentCSS
}}
{...rest}
/>
);
}

export default SwatchDetails;
26 changes: 26 additions & 0 deletions src/components/SwatchDetails.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
A primitive to render the token additional details.

```jsx harmony
<SwatchDetails>AAA</SwatchDetails>
```

Color can be customized using `color` prop:

```jsx harmony
<SwatchDetails color="primary">Primary details</SwatchDetails>
```

For more customization use `css` and `style` props

```jsx harmony
<SwatchDetails
css={{
fontSize: "24px"
}}
style={{
color: "blue"
}}
>
Custom style
</SwatchDetails>
```
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { default as Swatches } from "./components/Swatches";
export { default as Swatch } from "./components/Swatch";
export { default as SwatchToken } from "./components/SwatchToken";
export { default as SwatchValue } from "./components/SwatchValue";
export { default as SwatchDetails } from "./components/SwatchDetails";
export { default as ColorSwatch } from "./components/ColorSwatch";
export { default as TextStyleSwatch } from "./components/TextStyleSwatch";
export { default as PaletteSwatch } from "./components/PaletteSwatch";
Expand Down
Loading