1import React from 'react';
2
3import { common } from '@material-ui/core/colors';
4import { makeStyles } from '@material-ui/core/styles';
5import {
6 getContrastRatio,
7 darken,
8} from '@material-ui/core/styles/colorManipulator';
9
10import { Color } from 'src/gqlTypes';
11
12import { LabelFragment } from './fragments.generated';
13
14// Minimum contrast between the background and the text color
15const contrastThreshold = 2.5;
16
17// Guess the text color based on the background color
18const getTextColor = (background: string) =>
19 getContrastRatio(background, common.white) >= contrastThreshold
20 ? common.white // White on dark backgrounds
21 : common.black; // And black on light ones
22
23const _rgb = (color: Color) =>
24 'rgb(' + color.R + ',' + color.G + ',' + color.B + ')';
25
26// Create a style object from the label RGB colors
27const createStyle = (color: Color) => ({
28 backgroundColor: _rgb(color),
29 color: getTextColor(_rgb(color)),
30 borderBottomColor: darken(_rgb(color), 0.2),
31});
32
33const useStyles = makeStyles((theme) => ({
34 label: {
35 ...theme.typography.body1,
36 padding: '1px 6px 0.5px',
37 fontSize: '0.9em',
38 fontWeight: 500,
39 margin: '0.05em 1px calc(-1.5px + 0.05em)',
40 borderRadius: '3px',
41 display: 'inline-block',
42 borderBottom: 'solid 1.5px',
43 verticalAlign: 'bottom',
44 },
45}));
46
47type Props = { label: LabelFragment };
48function Label({ label }: Props) {
49 const classes = useStyles();
50 return (
51 <span className={classes.label} style={createStyle(label.color)}>
52 {label.name}
53 </span>
54 );
55}
56
57export default Label;