/
UniformColor.tsx
86 lines (81 loc) · 2.2 KB
/
UniformColor.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
/** @jsx jsx */
import { FC } from 'react';
import { jsx } from 'theme-ui';
import { CopyContainer } from '@component-controls/components';
import { colorToStr } from '../utils';
import { ColorBlockProps, ColorValue } from '../../types';
import { TableContainerProps, TableContainer } from '../../components';
/**
* Color item displaying as a table row, with color block, color name and rgb value.
* Design inspired by [Uniform](http://uniform.hudl.com/guidelines/colors/brand/design).
*/
export const UniformColor: FC<ColorBlockProps> = props => (
<table>
<tbody>
<BaseUniformColor {...props} />
</tbody>
</table>
);
const BaseUniformColor: FC<ColorBlockProps> = ({ name, color }) => {
const colorObj: ColorValue =
typeof color === 'string' ? { value: color } : color;
const { value: colorValue, name: colorName = name } = colorObj;
const { hex, rgba } = colorToStr(colorValue);
return (
<tr sx={{ fontSize: 1 }}>
<td>
<div
sx={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}
>
<CopyContainer
value={hex}
name={colorName}
sx={{ bg: colorValue, minWidth: 24, width: '100%', height: 24 }}
/>
</div>
</td>
<td>
<code sx={{ bg: 'gray' }}>{colorName}</code>
</td>
<td>
<div>{`${rgba.r}, ${rgba.g}, ${rgba.b}${
rgba.a !== 1 ? `, ${rgba.a}` : ''
}`}</div>
</td>
</tr>
);
};
/**
*
* palette displayed with UniformColor items
* using a css flex display direction column
*/
export const UniformColorPalette: FC<Omit<
TableContainerProps,
'children' | 'columns'
>> = props => (
<TableContainer
columns={[
{ title: 'Color', sx: { width: '10%' } },
{ title: 'Name', sx: { width: '45%' } },
{ title: 'RGBA', sx: { width: '45%' } },
]}
sx={{
border: 'none',
'& > tbody > tr > td': {
px: 2,
py: 2,
},
'& > thead > tr > th': {
textAlign: 'left',
fontSize: 0,
px: 2,
},
}}
{...props}
>
{({ name, value }) => (
<BaseUniformColor key={`color_item_${name}}`} name={name} color={value} />
)}
</TableContainer>
);