Skip to content

Commit 12b6050

Browse files
committed
Add color theme to highlighted labels
1 parent 6e806fb commit 12b6050

File tree

8 files changed

+290
-72
lines changed

8 files changed

+290
-72
lines changed

css/widget.css

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@
1010
margin: 5px 10px 5px 0;
1111
cursor: pointer;
1212
display: block;
13-
padding: 1px 10px;
13+
padding: 1px 8px 1px 5px;
1414
position: relative;
1515
border-radius: 4px;
1616
font-size: 16px;
1717

18+
white-space: nowrap;
19+
1820
background-color: #D1D5DB;
1921
}
2022
.selected {
2123
background: white;
2224
}
2325
.labelContainer {
2426
display: flex;
27+
max-width: 800px;
28+
overflow-x: auto;
2529
flex-grow: 1;
2630
font-weight: bold;
2731
font-family: "Roboto Condensed", "Arial Narrow", sans-serif;
@@ -33,6 +37,7 @@
3337
.topBar {
3438
display: flex;
3539
align-items: center;
40+
justify-content: space-between;
3641
color: #1F2937;
3742
width: 100%;
3843
padding: 12px;
@@ -53,12 +58,16 @@
5358
.span {
5459
font-size: 18px;
5560
padding: 0.25em 0.4em;
56-
background: #ffe184;
61+
padding-bottom: 2px;
5762
font-weight: bold;
5863
line-height: 1;
5964
cursor: pointer;
6065
}
6166

67+
.span:hover {
68+
border-bottom: solid 1px;
69+
}
70+
6271
.spanLabel {
6372
font-size: 12px;
6473
padding-left: 8px;

examples/introduction.ipynb

+184-24
Large diffs are not rendered by default.

src/annotate.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
export interface ColorLabel {
2+
text: string;
3+
color: string;
4+
}
5+
16
export interface Span {
27
start: number;
38
end: number;
49
text: string;
5-
label: string;
10+
label: ColorLabel;
611
}

src/components/Annotate.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { h, VNode } from "preact";
22
import { useMemo, useState } from "preact/hooks";
3-
import { Span } from "../annotate";
3+
import { ColorLabel, Span } from "../annotate";
44

55
import TopBar from "./TopBar";
66
import Highlightable from "./Highlightable";
7+
import { HIGHLIGHT_COLORS } from "./colors";
78

89
interface Props {
910
docs: string[];
@@ -19,7 +20,7 @@ export default function Annotate({
1920
onUpdateSpans,
2021
}: Props): VNode {
2122
const totalDocs = docs.length;
22-
const [selectedLabel, setSelectedLabel] = useState<string>("");
23+
const [selectedLabel, setSelectedLabel] = useState<ColorLabel>();
2324
const [docIndex, setDocIndex] = useState<number>(0);
2425
const [docSpans, setDocSpans] = useState<Span[][]>(
2526
initialSpans.length
@@ -31,7 +32,7 @@ export default function Annotate({
3132
return docs[docIndex];
3233
}, [docIndex, docs]);
3334

34-
const onChangeLabel = (label: string) => {
35+
const onChangeLabel = (label: ColorLabel) => {
3536
setSelectedLabel(label);
3637
};
3738

@@ -48,15 +49,25 @@ export default function Annotate({
4849

4950
const spans = docSpans[docIndex] || [];
5051

52+
const coloredLabels = useMemo(() => {
53+
const colors = Object.keys(HIGHLIGHT_COLORS);
54+
return labels.map((text, index) => ({
55+
text,
56+
color: colors[index % colors.length],
57+
}));
58+
}, [labels]);
59+
60+
const activeLabel = selectedLabel || coloredLabels[0];
61+
5162
return h("div", null, [
5263
h(TopBar, {
53-
selectedLabel,
54-
labels,
64+
selectedLabel: activeLabel,
65+
labels: coloredLabels,
5566
totalDocs,
5667
docIndex,
5768
onChangeLabel,
5869
onChangeNav,
5970
}),
60-
h(Highlightable, { text, selectedLabel, spans, onUpdate }),
71+
h(Highlightable, { text, selectedLabel: activeLabel, spans, onUpdate }),
6172
]);
6273
}

src/components/Highlightable.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import { h, VNode } from "preact";
22
import { useEffect, useRef } from "preact/hooks";
3-
import { Span } from "../annotate";
3+
import { ColorLabel, Span } from "../annotate";
4+
import { HIGHLIGHT_COLORS } from "./colors";
45

56
const SpanLabel = ({
67
text,
78
label,
89
onClick,
910
}: {
1011
text: string;
11-
label: string;
12+
label: ColorLabel;
1213
onClick: () => void;
1314
}): VNode => {
14-
return h("span", { className: "span", onClick }, [
15+
const color = HIGHLIGHT_COLORS[label.color] || HIGHLIGHT_COLORS.red;
16+
const style = {
17+
backgroundColor: color[50],
18+
color: color[800],
19+
borderColor: color[800],
20+
};
21+
22+
return h("span", { style, className: "span", onClick, title: label.text }, [
1523
text,
16-
h("span", { className: "spanLabel" }, label),
1724
]);
1825
};
1926

@@ -51,7 +58,7 @@ const getHighlightedText = (
5158

5259
interface Props {
5360
text: string;
54-
selectedLabel: string;
61+
selectedLabel: ColorLabel;
5562
spans: Span[];
5663
onUpdate: (span: Span[]) => void;
5764
}

src/components/Labels.ts

+43-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
11
import { h, VNode } from "preact";
2+
import { ColorLabel } from "../annotate";
3+
import { HIGHLIGHT_COLORS } from "./colors";
24

3-
const Labels = ({
4-
labels,
5-
selectedLabel,
6-
onChangeLabel,
5+
const Label = ({
6+
label,
7+
isSelected,
8+
onClick,
79
}: {
8-
labels: string[];
9-
selectedLabel: string;
10-
onChangeLabel: (label: string) => void;
11-
}): VNode => {
12-
const labelNodes = labels.map((label) => {
13-
const className = label === selectedLabel ? "label selected" : "label";
10+
label: ColorLabel;
11+
isSelected: boolean;
12+
onClick: (label: ColorLabel) => void;
13+
}) => {
14+
const className = isSelected ? "label selected" : "label";
15+
const color = HIGHLIGHT_COLORS[label.color] || HIGHLIGHT_COLORS.red;
1416

15-
return h("div", { className, onClick: () => onChangeLabel(label) }, label);
16-
});
17-
return h("div", { className: "labelContainer" }, labelNodes);
17+
const borderColor = isSelected ? color[300] : color[800];
18+
19+
const style = {
20+
borderLeft: `solid 4px ${borderColor}`,
21+
};
22+
23+
return h(
24+
"div",
25+
{ style, className, onClick: () => onClick(label) },
26+
label.text
27+
);
28+
};
29+
30+
interface Props {
31+
labels: ColorLabel[];
32+
selectedLabel: ColorLabel;
33+
onChangeLabel: (label: ColorLabel) => void;
34+
}
35+
36+
const Labels = ({ labels, selectedLabel, onChangeLabel }: Props): VNode => {
37+
return h(
38+
"div",
39+
{ className: "labelContainer" },
40+
labels.map((label) =>
41+
h(Label, {
42+
label,
43+
isSelected: label.text === selectedLabel.text,
44+
onClick: onChangeLabel,
45+
})
46+
)
47+
);
1848
};
1949

2050
export default Labels;

src/components/TopBar.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { h, VNode } from "preact";
2+
import { ColorLabel } from "../annotate";
23
import Labels from "./Labels";
34
import Nav from "./Nav";
45

56
interface Props {
6-
labels: string[];
7-
selectedLabel: string;
7+
labels: ColorLabel[];
8+
selectedLabel: ColorLabel;
89
docIndex: number;
910
totalDocs: number;
10-
onChangeLabel: (label: string) => void;
11+
onChangeLabel: (label: ColorLabel) => void;
1112
onChangeNav: (docIndex: number) => void;
1213
}
1314

src/components/colors.ts

+13-18
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,21 @@ export const HIGHLIGHT_COLORS: { [key: string]: any } = {
3131
300: "#FCA5A5",
3232
800: "#991B1B",
3333
},
34-
orange: {
35-
50: "#FFF7ED",
36-
300: "#FDBA74",
37-
800: "#9A3412",
34+
cyan: {
35+
50: "#ECFEFF",
36+
300: "#67E8F9",
37+
800: "#155E75",
3838
},
3939
amber: {
4040
50: "#FFFBEB",
4141
300: "#FCD34D",
4242
800: "#92400E",
4343
},
44+
violet: {
45+
50: "#F5F3FF",
46+
300: "#C4B5FD",
47+
800: "#5B21B6",
48+
},
4449
yellow: {
4550
50: "#FEFCE8",
4651
300: "#FDE047",
@@ -51,11 +56,6 @@ export const HIGHLIGHT_COLORS: { [key: string]: any } = {
5156
300: "#BEF264",
5257
800: "#3F6212",
5358
},
54-
green: {
55-
50: "#F0FDF4",
56-
300: "#86EFAC",
57-
800: "#166534",
58-
},
5959
emerald: {
6060
50: "#ECFDF5",
6161
300: "#6EE7B7",
@@ -66,10 +66,10 @@ export const HIGHLIGHT_COLORS: { [key: string]: any } = {
6666
300: "#5EEAD4",
6767
800: "#115E59",
6868
},
69-
cyan: {
70-
50: "#ECFEFF",
71-
300: "#67E8F9",
72-
800: "#155E75",
69+
orange: {
70+
50: "#FFF7ED",
71+
300: "#FDBA74",
72+
800: "#9A3412",
7373
},
7474
sky: {
7575
50: "#F0F9FF",
@@ -86,11 +86,6 @@ export const HIGHLIGHT_COLORS: { [key: string]: any } = {
8686
300: "#A5B4FC",
8787
800: "#3730A3",
8888
},
89-
violet: {
90-
50: "#F5F3FF",
91-
300: "#C4B5FD",
92-
800: "#5B21B6",
93-
},
9489
purple: {
9590
50: "#FAF5FF",
9691
300: "#D8B4FE",

0 commit comments

Comments
 (0)