LabelBullet.tsx

 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: '8px',
37    margin: '0 8px',
38    fontSize: '0.9em',
39    borderRadius: '3px',
40    display: 'inline-block',
41    borderBottom: 'solid 1.5px',
42    verticalAlign: 'bottom',
43  },
44}));
45
46type Props = { label: LabelFragment };
47function LabelBullet({ label }: Props) {
48  const classes = useStyles();
49  return (
50    <span className={classes.label} style={createStyle(label.color)}></span>
51  );
52}
53
54export default LabelBullet;