summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorludovicm67 <ludovicmuller1@gmail.com>2020-02-26 12:05:39 +0100
committerludovicm67 <ludovicmuller1@gmail.com>2020-02-26 12:05:39 +0100
commit92e404b7469b9532b7cc226e54fb1d805cd7169d (patch)
tree436ef6ac85a3fe00a7d4c2b2264dc849bd8eba83
parent4b8f9e554ffe240632671aa214c1e669177af01e (diff)
downloadgit-bug-webui/labels.tar.gz
git-bug-webui/labels.zip
webui: create label bullets in valid labels filteringwebui/labels
-rw-r--r--webui/src/components/LabelBullet.tsx54
-rw-r--r--webui/src/components/ValidLabels.tsx17
2 files changed, 69 insertions, 2 deletions
diff --git a/webui/src/components/LabelBullet.tsx b/webui/src/components/LabelBullet.tsx
new file mode 100644
index 00000000..03cecbce
--- /dev/null
+++ b/webui/src/components/LabelBullet.tsx
@@ -0,0 +1,54 @@
+import React from 'react';
+
+import { common } from '@material-ui/core/colors';
+import { makeStyles } from '@material-ui/core/styles';
+import {
+ getContrastRatio,
+ darken,
+} from '@material-ui/core/styles/colorManipulator';
+
+import { Color } from 'src/gqlTypes';
+
+import { LabelFragment } from './fragments.generated';
+
+// 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: '8px',
+ margin: '0 8px',
+ fontSize: '0.9em',
+ borderRadius: '3px',
+ display: 'inline-block',
+ borderBottom: 'solid 1.5px',
+ verticalAlign: 'bottom',
+ },
+}));
+
+type Props = { label: LabelFragment };
+function LabelBullet({ label }: Props) {
+ const classes = useStyles();
+ return (
+ <span className={classes.label} style={createStyle(label.color)}></span>
+ );
+}
+
+export default LabelBullet;
diff --git a/webui/src/components/ValidLabels.tsx b/webui/src/components/ValidLabels.tsx
index 173e146c..4c64dd6d 100644
--- a/webui/src/components/ValidLabels.tsx
+++ b/webui/src/components/ValidLabels.tsx
@@ -1,12 +1,22 @@
import React, { useState } from 'react';
import CircularProgress from '@material-ui/core/CircularProgress';
+import { makeStyles } from '@material-ui/styles';
+import LabelBullet from './LabelBullet';
import { useValidLabelsQuery } from './ValidLabelsQuery.generated';
+const useStyles = makeStyles({
+ list: {
+ listStyleType: 'none',
+ padding: 0,
+ },
+});
+
const ValidLabels: React.FC = () => {
const { loading, error, data } = useValidLabelsQuery();
const [filter, setFilter] = useState('');
+ const classes = useStyles();
if (loading) return <CircularProgress />;
if (error) return <p>Error: {error}</p>;
const labels = data?.repository?.validLabels.nodes.filter(
@@ -22,9 +32,12 @@ const ValidLabels: React.FC = () => {
onChange={e => setFilter(e.target.value)}
value={filter}
/>
- <ul>
+ <ul className={classes.list}>
{labels?.map(l => (
- <li>{l.name}</li>
+ <li key={l.name}>
+ <LabelBullet label={l} />
+ {l.name}
+ </li>
))}
</ul>
</>