diff options
author | Michael Muré <batolettre@gmail.com> | 2020-02-13 00:05:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-13 00:05:04 +0100 |
commit | 0066f3d8c278558eeac70d3cd7ca21c360014346 (patch) | |
tree | ca40e95496b90837fb0e7a5ff4ac5b966ceba6ec /webui/src/Label.tsx | |
parent | 269036bdf25af8fefbca24b7455c4e0b7d1d72b5 (diff) | |
parent | ab09c03a1e55d5c2e35f332f0e5f6335c1670427 (diff) | |
download | git-bug-0066f3d8c278558eeac70d3cd7ca21c360014346.tar.gz |
Merge pull request #323 from MichaelMure/webui/typescript
Webui/typescript
Diffstat (limited to 'webui/src/Label.tsx')
-rw-r--r-- | webui/src/Label.tsx | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/webui/src/Label.tsx b/webui/src/Label.tsx new file mode 100644 index 00000000..a33b4c2c --- /dev/null +++ b/webui/src/Label.tsx @@ -0,0 +1,55 @@ +import { common } from '@material-ui/core/colors'; +import { makeStyles } from '@material-ui/core/styles'; +import { + getContrastRatio, + darken, +} from '@material-ui/core/styles/colorManipulator'; +import React from 'react'; + +import { LabelFragment } from './Label.generated'; +import { Color } from './gqlTypes'; + +// Minimum contrast between the background and the text color +const contrastThreshold = 2.5; + +// Guess the text color based on the background color +const getTextColor = (background: string) => + getContrastRatio(background, common.white) >= contrastThreshold + ? common.white // White on dark backgrounds + : common.black; // And black on light ones + +const _rgb = (color: Color) => + 'rgb(' + color.R + ',' + color.G + ',' + color.B + ')'; + +// Create a style object from the label RGB colors +const createStyle = (color: Color) => ({ + backgroundColor: _rgb(color), + color: getTextColor(_rgb(color)), + borderBottomColor: darken(_rgb(color), 0.2), +}); + +const useStyles = makeStyles(theme => ({ + label: { + ...theme.typography.body1, + padding: '1px 6px 0.5px', + fontSize: '0.9em', + fontWeight: 500, + margin: '0.05em 1px calc(-1.5px + 0.05em)', + borderRadius: '3px', + display: 'inline-block', + borderBottom: 'solid 1.5px', + verticalAlign: 'bottom', + }, +})); + +type Props = { label: LabelFragment }; +function Label({ label }: Props) { + const classes = useStyles(); + return ( + <span className={classes.label} style={createStyle(label.color)}> + {label.name} + </span> + ); +} + +export default Label; |