/
DuetColor.tsx
137 lines (133 loc) · 3.61 KB
/
DuetColor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/** @jsx jsx */
import { FC, ReactNode } from 'react';
import { jsx } from 'theme-ui';
import { CopyContainer } from '@component-controls/components';
import tinycolor from 'tinycolor2';
import {
CheckIcon,
XIcon,
CheckCircleFillIcon,
AlertIcon,
XCircleFillIcon,
} from '@primer/octicons-react';
import { colorToStr } from '../utils';
import {
ColorBlockProps,
ColorValue,
colorContrast,
TokenStatus,
} from '../../types';
import { TableContainerProps, TableContainer } from '../../components';
/**
* Color item displaying as a row, with color, name, description, var, and sass variables as well as the contrast ratio ad status.
* Design inspired by [Duet](https://www.duetds.com/tokens/#color).
*/
export const DuetColor: FC<ColorBlockProps> = props => (
<table>
<tbody>
<BaseDuetColor {...props} />
</tbody>
</table>
);
const BaseDuetColor: FC<ColorBlockProps> = ({ name, color }) => {
const colorObj: ColorValue =
typeof color === 'string' ? { value: color } : color;
const {
value: colorValue,
name: colorName = name,
description,
sass,
varName,
status,
} = colorObj;
const statusNode = (s?: TokenStatus): ReactNode => {
switch (s) {
case 'ok':
return <CheckCircleFillIcon sx={{ color: '#00875a' }} />;
case 'error':
return <XCircleFillIcon sx={{ color: '#de2362' }} />;
case 'warning':
return <AlertIcon sx={{ color: '#e9c46a' }} />;
default:
return null;
}
};
const { hex, rgba } = colorToStr(colorValue);
const contrast = tinycolor.readability(hex, '#ffffff');
return (
<tr>
<td>
<CopyContainer value={hex} name={colorName} sx={{ width: '100%' }}>
<div
sx={{
height: 50,
bg: colorValue,
mr: 3,
}}
/>
</CopyContainer>
<div sx={{ mr: 3 }}>{hex}</div>
<div sx={{ mr: 3 }}>{`rgb${rgba.a !== 1 ? 'a' : ''}(${rgba.r}, ${
rgba.g
}, ${rgba.b}${rgba.a !== 1 ? `, ${rgba.a}` : ''})`}</div>
</td>
<td>
<div sx={{ fontWeight: 'bold', fontSize: 2 }}>{colorName}</div>
<p> {description}</p>
</td>
<td>
<code sx={{ my: 1, bg: 'gray' }}>{sass}</code>
<code sx={{ m: 0, bg: 'gray' }}>{varName}</code>
</td>
<td sx={{ textAlign: 'center' }}>
<span sx={{ mr: 1 }}>{`${contrast.toFixed(2)}:1`}</span>
{contrast < colorContrast.small.ratio ? (
<XIcon sx={{ color: '#de2362' }} />
) : (
<CheckIcon sx={{ color: '#00875a' }} />
)}
</td>
<td sx={{ textAlign: 'center' }}>{statusNode(status)}</td>
</tr>
);
};
/**
*
* palette displayed with DuetColor items
* using a css flex display direction column
*/
export const DuetColorPalette: FC<Omit<
TableContainerProps,
'children' | 'columns'
>> = props => (
<TableContainer
columns={[
{ title: 'Example', sx: { width: '25%' } },
{ title: 'Description', sx: { width: '28%' } },
{ title: 'Token', sx: { width: '25%' } },
{ title: 'Contrast', sx: { width: '10%' } },
{ title: 'Status', sx: { width: '7%' } },
]}
sx={{
border: 'none',
'& > tbody > tr': {
fontSize: 0,
transition: 'background .3s ease',
':hover': {
bg: 'gray',
},
},
'& > thead > tr > th': {
textAlign: 'left',
fontSize: 2,
fontWeight: 'normal',
pb: 2,
},
}}
{...props}
>
{({ name, value }) => (
<BaseDuetColor key={`color_item_${name}}`} name={name} color={value} />
)}
</TableContainer>
);